{% extends 'obj_form.html' %}
{% load i18n %}

    {% block subtitle-1 %}
        {% trans "公司管理" %}
    {% endblock %}
    {% block subtitle-2 %}
        {% if form.instance.id %}
            {% trans "修改帐号" %}
        {% else %}
            {% trans "添加帐号" %}
        {% endif %}
    {% endblock %}
        
{% block form_content %}
    {{ block.super }}
    <div class="form-group">
        <label class="col-sm-2 control-label ">{% trans '公司名称:' %} </label>
        <div class="col-sm-10">
            
            <input id='id_company_name' />
        
        </div>
    </div>    
    <div class="form-group">
        <label class="col-sm-2 control-label ">{% trans '用户帐号:' %}</label>
        <div class="col-sm-10">{{ form.username }}</div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label ">{% trans '用户姓名:' %}</label>
        <div class="col-sm-10">{{ form.last_name }}</div>
    </div>


    <div class="form-group">
        <label class="col-sm-2 control-label ">{{ form.phone.label_tag }} </label>
        <div class="col-sm-10">{{ form.phone }}</div>
    </div>



    <!--2024-05-31-->
    <div class="form-group">
        <label class="col-sm-2 control-label ">{% trans '帐号状态:' %}</label>
        <div class="col-sm-10"><select id="sel_is_active">
            <option value='True'>正常</option>
            <option value='False'>停用</option>
        </select></div>
    </div>
    

    <div class="form-group" id="div_pwd">
        {% if form.instance.id %}
            <div class="col-sm-2"> <button class="btn btn-default btn-showpassword pull-right">修改密码</button></div> 
            <div class="col-sm-10"><input id="password" type="password" name='password' style="display:none"></div>            
        {% else %}
            <label class="col-sm-2 control-label ">{{ form.password.label_tag }} </label>            
            <div class="col-sm-10">{{ form.password }}</div>
        {% endif %}
    </div>
    <div class="form-group">        
        <input id='group_ids' name='group_ids' value='' type='hidden'/>
        <label class="col-sm-2 control-label ">{% trans '功能组别:' %} </label>
        <div class="form-group col-sm10" >
            {% for g in groups %}
                <label class="form-inline">
                    <input type="checkbox"  value="{{g.id}}">{{g.name}}
                </label>
            {% endfor %}   
        </div>
    </div>
	
	<div class="form-group">
        <label class="col-md-2 control-label">产品系列授权</label>
			<div class='col-sm-4'>
                <select id="ySelectSeries" multiple="multiple">
                    {% for s in series %}
						{% ifequal s.2 1 %}
							<option value='{{s.0}}' selected="selected">{{s.1}}</option>
						{% else %}
							<option value='{{s.0}}'>{{s.1}}</option>
						{% endifequal %}
                    {% endfor %}
                </select>
            </div>
	</div>
	
	<div class="form-group">
        <label class="col-md-2 control-label">产品授权</label>
        <div class='col-sm-4'>
            <div class="input-group">
                <input type="text" class="form-control" id="product_label">
                <input type="hidden" id="product_id" />
                <span class="input-group-btn"><button type="button" class="btn btn-success add-product">
                <span class='glyphicon glyphicon-arrow-down'></span></button></span>
            </div>
            <select class="select2_multiple form-control" id='sel-product' multiple="multiple">
                {% for o in products %}
                    <option value='{{o.0}}'>{{o.1}}</option>
                {% endfor %}                                    
            </select>        
        </div>
	</div>
	
	
    <div>
        <a class="btn btn-primary btn-save" type="submit" style="float:right;">{% trans '保存' %}</a>
    </div>
{% endblock %}

{% if message %}
    <script>
        {% for msg in message %}
            alert('{{ msg.message }}')
        {% endfor %}
    </script>
{% endif %}






{% block js %}
    {{ block.super }}
    $('.btn-save').click(function(){
        $(this).hide();
        var g_ids=[];
        $("#obj_form input[type='checkbox']:checked").each(function (index, item) {
            g_ids.push($(this).val());            
        });
        if (g_ids.length>0){
            $('#group_ids').val(g_ids.join(','));
        };
		
		if($("#ySelectSeries")){
			var series= $("#ySelectSeries").ySelectedValues(",");
			$("#id_product_series").val(series);
		};

        //2024-05-31
        $('#id_is_active').val($('#sel_is_active').val());
        
        var products = []
        $("#sel-product option").each(function (){
            products.push($(this).val());
        });
        $('#id_product_ids').val(products.join(','))
       
        //$.SubmitForm('#obj_form form','/obj/list/user/');
        $.SubmitForm('#obj_form form','{{request.path}}');
    });


    /*
    $('#id_company_name').autocomplete({
        source: {{company_src|safe}},
        select: function (event, ui) {
            $("#id_company").val(ui.item.value);
            $("#id_company_name").val(ui.item.label);
            return false;
        }
    });

    $("#id_company").val('{{form.instance.company.id}}');
    $("#id_company_name").val('{{form.instance.company.name}}');  
    */


    //2022-10-31
    $('#id_company_name').autocomplete({   
            source: function(request, response) {
                $.ajax({
                    url: '/obj/ajax/autocomplete/agent/',
                    dataType: "json",
                    data: {
                        term:   $("#id_company_name").val() ,//搜索栏里的内容
                        company_id:  {{ request.user.company_id }}//额外参数
                    },
                    success: function(data) {
                        response(data);
                    }
                });
            },            
            select: function (event, ui) {
                $("#id_company").val(ui.item.value);
                $("#id_company_name").val(ui.item.label);
                return false;
            }
    });
    



    $("#obj_form input[type='checkbox']").each(function (index, item) {
        {% for g in mygroups %}
            if ($(this).val()=={{g}}){
                $(this).attr('checked','checked');
            };
        {% endfor %}
    });


    $('#obj_form .btn-showpassword').click(function(){
        event.preventDefault();
        $('#password').show();
    });

    //2024-05-31
    $('#sel_is_active').val('{{form.instance.is_active}}');
    $('#id_company_name').val('{{form.instance.company.name}}');
    


    {% ifequal user.company.id 19257 %}  
        var chb = document.querySelectorAll('#obj_form .form-inline');
        for (let i = 0; i < chb.length; i++) {
            var ele = chb[i];
            if (
                ele.innerText!=='业务员'&&
                ele.innerText!=='系统管理员'&&
                ele.innerText!=='财务'&&
                ele.innerText!=='成品入库组'&&
                ele.innerText!=='经销商'&&
                ele.innerText!=='成品出库组'&&
                ele.innerText!=='市场稽查组'&&
                ele.innerText!=='内勤管理组'&&
                ele.innerText!=='销售助理'&&
                ele.innerText!=='出纳'&&
                ele.innerText!=='分销商'
                ){
                ele.style.display = 'none';
            }
            
        }

    {% endifequal%}
	
	 
	$('#ySelectSeries').ySelect(
          {
            placeholder: '选择系列',
            searchText: '搜索',
            showSearch: true,
            numDisplayed: 5,
            overflowText: '已选中 {n}项',
            isCheck:false
          }
    );
    

    $('#product_label').autocomplete({   
        source: function(request, response) {
            $.ajax({
                url: '/obj/ajax/autocomplete/product/',
                dataType: "json",
                data: {
                    term:   $("#product_label").val() ,//搜索栏里的内容
                    company_id: {{ request.user.company_id }}//额外参数
                },
                success: function(data) {
                    response(data);
                }
            });
        },            
        select: function (event, ui) {
            $("#product_id").val(ui.item.value);
            $("#product_label").val(ui.item.label);
            return false;
        }
    });
    
    $('.add-product').click(function(){
        var id=$('#product_id').val();
        var label=$('#product_label').val();
        if(id==''){
            toastr.error("请先选择产品！", "操作错误：",
                {timeOut: 800, positionClass: "toast-center-center"}
            );
            return;
        }else{
            var e=false;
            $("#sel-product option").each(function(){
                if($(this).val() == id){
                    e=true;
                    return false;          
                }
            });
            if(e){
                toastr.error(label+"已在列表中！", "操作错误：",
                    {timeOut: 800, positionClass: "toast-center-center"}
                );
                return;
            }else{
                $('#sel-product').append("<option value='"+id+"'>"+label+"</option>");
            }
        };
    });
    
    $('.select2_multiple').on('dblclick','option',function(){
        $(this).remove();
    });


{% endblock %}
