{% 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>

    <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>
        <a class="btn btn-primary btn-save" type="submit" style="float:right;">{% trans '保存' %}</a>
    </div>
{% endblock %}






{% 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);
		}

        //$.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();
    });
    {% 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.style.display = 'none';
            }
            
        }

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