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

  

    {% block js %}
        {{ block.super }}       
        
        $('#user_label').autocomplete({   
            source: function(request, response) {
                $.ajax({
                    url: '/obj/ajax/autocomplete/salesman_lastname/',
                    dataType: "json",
                    data: {
                        term:   $("#user_label").val() ,//搜索栏里的内容
                        company_id:  '{{request.user.company.id}}' ,//额外参数
                    },
                    success: function(data) {
                        response(data);
                    }
                });
            },            
            select: function (event, ui) {
                $("#user_id").val(ui.item.value);
                $("#user_label").val(ui.item.label);
                return false;
            }
        });     

        $('.add-user').click(function(){
            var id=$('#user_id').val();
            var label=$('#user_label').val();
            if(id==''){
                toastr.error("请先选择经销商！", "操作错误：",
                    {timeOut: 800, positionClass: "toast-center-center"}
                );
                return;
            }else{
                var e=false;
                $("#sel-users 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-users').append("<option value='"+id+"'>"+label+"</option>");
                }
            };
        });       


        $('.select2_multiple').on('dblclick','option',function(){
            $(this).remove();
        });


        $('.btn-save').click(function(){    
            event.preventDefault();   
            var a=[];
            $("#sel-users option").each(function (){
                a.push($(this).val());
            });  
            if(a.length>0){
                $('#id_user_ids').val(a.join(','));
            }else{
                $('#id_user_ids').val('');
            };    

            $("#obj_form form").ajaxSubmit({
                type: 'post',
                url: '/company/saleman/manage/?obj=sharegroup',    
                success: function(html) {
                    // 在saleman.html里面定义
                    refresh_tb('sharegroup')
                    $('#bg-modal').modal('hide')
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    $('.btn-save').show();
                    alert('连接服务器失败');                        
                }
            });


        });       

        $('.btn-delete').click(function(){    
            event.preventDefault();  
            $.ajax({
                url: "/company/edit/staffdiv/{{form.instance.id}}/delete/",
                type: 'POST',
                headers:{'X-CSRFToken': $('input[name="csrfmiddlewaretoken"]').val()},                
                success:function(data){
                    jQuery.RefreshContent('/company/list/staffdiv/');
                }
            });
        });

    {% endblock %}

    {% block subtitle-1 %}
        {% trans "业务员管理" %}
    {% endblock %}
    {% block subtitle-2 %}
        {% trans "共享小组" %}
    {% endblock %}

{% block form_content %}
    {{ block.super }}    
 
   
    <div class="form-group ">      
        <label class="col-sm-2 control-label ">{% trans "共享小组名称" %} </label>
        <div class="col-sm-8">
            <input id="inputName" name='name' placeholder="输入共享小组名称" value="{{name}}"/>            
        </div>        
    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label">{% trans "共享业务员" %}</label>
        <div class='col-sm-6 mrgnbttm0'>
            <div class="input-group">
                <input type="text" class="form-control" id="user_label">
                <input type="hidden" id="user_id" />
                <span class="input-group-btn"><button type="button" class="btn btn-success add-user">
                <span class='glyphicon glyphicon-arrow-down'></span></button></span>
            </div>        
        </div>     
    </div>


    <div class="form-group" >
        <div class='col-sm-6 col-md-offset-2' style="top:-20px;">
            <select class="select2_multiple form-control" id="sel-users" multiple="multiple" style="height: 180px;">
                {% for o in users %}
                    <option value='{{o.0}}'>{{o.1}}</option>
                {% endfor %}                                     
            </select>        
        </div>     
    </div>    
    <div class="form-group ">      
        <label class="col-sm-2 control-label ">{% trans "备注" %} </label>
        <div class="col-sm-8">
            <input name='remark' placeholder="备注" value="{{remark}}"/>            
        </div>        
    </div>

    <input id="id_user_ids" type="hidden" name="user_ids" />
    <input type="hidden" name="id" value="{{id}}" />
    <div>
        <button class="btn btn-primary btn-save"    style="float:right;">{% trans '保存' %}</button>
    </div>

{% endblock %}
