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

{% block css %}
    {{ block.super }}
    #obj_form .datepicker{
        width:100px
    }
    #obj_form select.adm{
        min-width:100px;
        margin-left:5px;
    }
    #product_label{
        min-width:380px;
        margin-left:15px;
        margin-right:7px;
    }
    #obj_form .btn-del{
        float:right;
    }
{% endblock %}



{% block js %}
    {{ block.super }}
    var dt1,dt2;
    dt1=new Date();
    dt2=new Date();
    dt2.setFullYear(dt1.getFullYear(),11,31);
    $('.datepicker').datepicker({dateFormat: 'yy-mm-dd'});
    $('#dt1,#dt1_product').datepicker('setDate',dt1);
    $('#dt2,#dt2_product').datepicker('setDate',dt2);
    setDatePickerZh();


    $('#product_label').autocomplete({
            source: {{products|safe}},
            select: function (event, ui) {
                $("#product_id").val(ui.item.value);
                $("#product_label").val(ui.item.label);
                return false;
            }
    });

    
    {% ifequal request.user.company.level.sn 0 %}
        $('#parent_label').autocomplete({
            source: {{children|safe}},
            select: function (event, ui) {
                var pid=ui.item.value;
                $("#id_parent").val(pid);
                $("#parent_label").val(ui.item.label);
                $.get(
                    '/company/child/level/'+pid+'/',
                    function(obj){    
                        if (obj.level.id<0){
                            //bootbox.alert(obj.level.e);
                            toastr.error(
                                obj.level.e,
                                "数据错误：",
                                {timeOut: 800, positionClass: "toast-center-center"}
                            );

                        }else{
                            $('#lv').text(obj.level.name);
                        }                         
                    }        
                );    
                return false;
            }
        });

    {% endifequal %}
    



    //被删除的授权ID
    var auth_del_ids=[];
    var auth_del_ids_product=[];
    $("#province").change(function() {
        var province_id = $(this).val();
        getCity(province_id);
    });
    $("#city").change(function() {
        var city_id = $(this).val();
        getArea(city_id);
    });
    function getCity(province_id) {
        var me=$("#city");
        $.ajax({
            url: "http://117.34.70.23:32176/gis/adm/2/"+province_id+"/",
            success: function(data) {
                $(me).empty();
                $('#area').empty();
                $.each(data,function(i,d){$(me).append("<option value='"+d[0]+"'>"+d[1]+"</option>")});
                $(me).append("<option value='' selected='selected'>---------</option>");
            },
            dataType: 'jsonp',
            jsonp: 'callback'
        });
    };
    function getArea(city_id) {
        var me=$('#area');
        $.ajax({
            url: "http://117.34.70.23:32176/gis/adm/3/"+city_id+"/",
            success: function(data) {
                $(me).empty();
                $.each(data,function(i,d){$(me).append("<option value='"+d[0]+"'>"+d[1]+"</option>")});
                $(me).append("<option value='' selected='selected'>---------</option>");
            },
            dataType: 'jsonp',
            jsonp: 'callback'
        });
    };
    $('#tb_auth input[type="checkbox"]').removeClass('form-control');

    // 列表中是否已有指定 adm_id
    function has_adm(id){
        var b=0;
        $(".adm_id").each(function(i,el){
            if($(el).val()==id){
                b=1;
                return;
            };
        });
        return b;
    };
    $("#tb_auth").on("click",".btn-del",function(){
        var id=$(this).closest('tr').find('.auth_id').val();
        if (id!=''){
            auth_del_ids.push(parseInt(id));
        };
        $(this).closest('tr').remove();
    });
    // 列表中是否已有指定 product_id
    function has_product(id){
        var b=0;
        $(".product_id").each(function(i,el){
            if($(el).val()==id){
                b=1;
                return;
            };
        });
        return b;
    };
    $("#tb_auth_product").on("click",".btn-del",function(){
        var id=$(this).closest('tr').find('.auth_id').val();
        if (id!=''){
            auth_del_ids_product.push(id);
        };
        $(this).closest('tr').remove();
    });
    //添加销售区域授权
    $('.btn-add').click(function(){
        var id='';
        if(!$.isEmpty($('#area').val())){
            id=$('#area').val();
        } else {
            if (!$.isEmpty($('#city').val())){
                id=$('#city').val();
            }else{
                if(!$.isEmpty($('#province').val())){
                    id=$('#province').val();
                }
            }
        }
        if(id==''){
            //bootbox.alert("{% trans '请先选择授权区域!' %}");
            toastr.error(
                "请先选择授权区域!",
                "操作错误：",
                {timeOut: 800, positionClass: "toast-center-center"}
            );



            return;
        };
        if (has_adm(id)){
            //bootbox.alert("{% trans '当前授权区域已在列表中!' %}");
            toastr.error(
                "当前授权区域已在列表中!",
                "操作错误：",
                {timeOut: 800, positionClass: "toast-center-center"}
            );


            return;
        };
        var html='<tr>'+
                '<input type="hidden" class="auth_id"  value="">'+
                '<input type="hidden" class="adm_id"  value="'+id+'">'+
                '<input type="hidden" class="dt_fr" value="'+$('#dt1').val()+'">'+
                '<input type="hidden" class="dt_to" value="'+$('#dt2').val()+'">'+
                '<td>'+$('#province').find("option:selected").text()+'</td>'+
                '<td>'+$('#city').find("option:selected").text()+'</td>'+
                '<td>'+$('#area').find("option:selected").text()+'</td>'+
                '<td>'+$('#dt1').val()+'</td><td>'+$('#dt2').val()+'</td>'+
                '<td><a class="btn btn-primary btn-sm btn-warning btn-del">{% trans '删除' %}</a> </td>'+
            '</tr>';
        $('#tb_auth tbody').append(html);
    });
    //添加授权产品
    $('.btn-add-product').click(function(){
        var id=$('#product_id').val();
        if(id==''){
            //bootbox.alert("{% trans '请先选择要授权的产品!' %}");
            toastr.error(
                "请先选择要授权的产品!",
                "操作错误：",
                {timeOut: 800, positionClass: "toast-center-center"}
            );
            return;
        };
        if (has_product(id)){
            //bootbox.alert("{% trans '当前授权的产品已在列表中!' %}");
            toastr.error(
                "当前授权的产品已在列表中!",
                "操作错误：",
                {timeOut: 800, positionClass: "toast-center-center"}
            );

            return;
        };
        var html='<tr>'+
                '<input type="hidden" class="auth_id"  value="">'+
                '<input type="hidden" class="product_id" value="'+id+'">'+
                '<input type="hidden" class="dt_fr"  value="'+$('#dt1_product').val()+'">'+
                '<input type="hidden" class="dt_to"  value="'+$('#dt2_product').val()+'">'+
                '<td>'+$('#product_label').val()+'</td>'+
                '<td>'+$('#dt1_product').val()+'</td><td>'+$('#dt2_product').val()+'</td>'+
                '<td><a class="btn btn-primary btn-sm btn-warning btn-del">{% trans '删除' %}</a> </td>'+
            '</tr>';
        $('#tb_auth_product tbody').append(html);
    });

    $('.btn-save').click(function(){
        $('.btn').hide();
        //保存删除的区域授权ID
        var cnt=$('#tb_auth tbody').find("tr").length;
        var s='';
        if (auth_del_ids.length>0){
            s=auth_del_ids.join(',');
        };
        $('#obj_form form').append('<input type="hidden" value="'+s+'" name="auth_del_ids">');
        $('#obj_form form').append('<input type="hidden" value="'+cnt+'" name="auth_cnt">');
        //保存删除的产品授权ID
        var cnt_product=$('#tb_auth_product tbody').find("tr").length;
        var s='';
        if (auth_del_ids_product.length>0){
            s=auth_del_ids_product.join(',');
        };
        $('#obj_form form').append('<input type="hidden" value="'+s+'" name="auth_del_ids_product">');
        $('#obj_form form').append('<input type="hidden" value="'+cnt_product+'" name="auth_cnt_product">');

        //修改授权区域 name后缀
        $('#tb_auth tbody tr').each(function(i,d){
            $(this).find('.auth_id').attr('name','auth_id_'+i);
            $(this).find('.adm_id').attr('name','adm_id_'+i);
            $(this).find('.dt_fr').attr('name','dt_fr_'+i);
            $(this).find('.dt_to').attr('name','dt_to_'+i);
        });

        //修改授权品 name后缀
        $('#tb_auth_product tbody tr').each(function(i,d){
            $(this).find('.auth_id').attr('name','cp_auth_id_'+i);
            $(this).find('.product_id').attr('name','cp_product_id_'+i);
            $(this).find('.dt_fr').attr('name','cp_dt_fr_'+i);
            $(this).find('.dt_to').attr('name','cp_dt_to_'+i);
        });


        $.SubmitForm('#obj_form form','{{request.path}}');
    });

    //经销商可以设置其下经销商为终端店
    $('#is_terminal').change(function(){
        if($(this).prop("checked") == true){
            $('#id_level').val('3');
        }else{
            $('#id_level').val('');
        }
    });
    {% ifequal level.id 3 %}
        $('#is_terminal').prop('checked',true);
    {% endifequal %}   
   


    $('#id_division').change(function(){
        var me=$('#id_sub_division');
        var d=$(this).val();
        $('#id_sub_division').empty();
        if (d!=''){
            $.ajax({
                url: "/company/json/subdivision/"+d+"/",
                success: function(data) {
                    data=data.details;
                    $.each(data,function(i,d){$(me).append("<option value='"+d[0]+"'>"+d[1]+"</option>")});
                    $(me).append("<option value='' selected='selected'>---------</option>");
                }
            });
        }
    });


    {% if jg_ty %}
        $('[name="sync_ty"]').closest('label').removeClass('active');
        {% ifequal sync_ty '1' %}
            $("#ty_yes")
                .prop('checked', true)
                .closest('label').addClass('active');
        {% else %}
            $('#ty_no')
                .prop('checked', false)
                .closest('label').addClass('active');
        {% endifequal %}
    {% endif %}
    {% if form.instance %}
        {% if form.instance.parent %}
            $('#id_parent').val({{form.instance.parent_id}});
            $('#parent_label').val('{{form.instance.parent.name}}');
        {% else %}
            $('#id_parent').val({{parent.id}});
            $('#parent_label').val('{{parent.name}}');
        {% endif %}
    {% endif %}
   
{% endblock %}

 
{% block subtitle-1 %}
    {% trans '经销商管理' %}
{% endblock %}
{% block subtitle-2 %}
    {% trans '经销商信息' %}
{% endblock %}   

    


{% block form_content %}
    {{ block.super }}

    {% if parent.has_external_code %}
        <div class="form-group ">
            <label class="col-sm-2 control-label ">{% trans '经销商编码:' %}</label>
            <div class="col-sm-10">{{ form.external_code }}</div>
        </div>       
    {% endif %}
    <div class="form-group ">
        <label class="col-sm-2 control-label ">{% trans '经销商名称:' %} </label>
        <div class="col-sm-10">{{ form.name }}</div>
    </div>    
    {% if request.user.company.is_microboss %}
    {% else %}
        {% ifequal request.user.company.level.sn 0 %}            
            <div class="form-group ">
                <label class="col-sm-2 control-label ">{% trans '所属公司:' %} </label>
                <div class="col-sm-10"><input id="parent_label"></div>
            </div>
            <div class="form-group ">
                <label class="col-sm-2 control-label ">{% trans '经销商层级:' %} </label>
                <!--
                <div class="col-sm-10">{{ form.level }}</div>
                -->                
                <div class="col-sm-10"><span id='lv'>
                    {% if form.instance.id %}
                        {{ form.instance.level.name }}
                    {% else %}   
                        {{parent.child_level.name}}
                    {% endif %}
                </span></div>
            </div>
            <div class="form-group ">
                <label class="col-sm-2 control-label ">{% trans '所属战区:' %} </label>
                <div class="col-sm-10">{{ form.division }}</div>
            </div>
            <div class="form-group ">
                <label class="col-sm-2 control-label ">{% trans '所属分区:' %} </label>
                <div class="col-sm-10">{{ form.sub_division }}</div>
            </div>
        {% else %}
            <div class="form-group ">
                <label class="col-sm-2 control-label ">{% trans '经销商层级:' %} </label>
                <div class="col-sm-10">
                    <label class="form-inline">
                        <input style='margin-top:-3px;margin-right:5px;' type="checkbox" id="is_terminal"/>
                        {% trans '此经销商为销售终端店' %}
                    </label>
                    <input type="hidden" name="level" id="id_level"  />
                </div>
            </div>
        {% endifequal %}     
    
    {% endif %}


    <div class="form-group ">
        <label class="col-sm-2 control-label">{% trans '联系人电话:' %}</label>
        <div class="col-sm-10">{{ form.cell_phone }} </div>
    </div>
    <div class="form-group ">
        <label class="col-sm-2 control-label">{% trans '经销商地址:' %}</label>
        <div class="col-sm-10">{{ form.address }} </div>
    </div>


    <!--

    {% if request.user.company.is_microboss %}
        <div class="pull-right">
            <a type="button" class='btn btn-primary btn-save'>
                    <span class='glyphicon glyphicon-floppy-disk' style='padding-right:5px;'></span>{% trans '保存' %}
            </a>
        </div>
    {% else %}
        {% ifequal request.user.company.level.id 1 %}
        <div class="form-inline form-group" >
            <label class="col-sm-2 control-label g">{% trans '授权销售区域:' %} </label>
            <select id="province" class="form-control adm col-sm-1" style="margin-left:15px;">
                {% for p in prs %}
                    <option value="{{p.0}}">{{p.1}}</option>
                {% endfor %}
                <option value="" selected="selected">----------------</option>
            </select>
            <select id="city" class="form-control adm"></select>
            <select id="area" class="form-control adm" style="margin-right:10px;"></select>
            <input class="datepicker form-control" id="dt1">
            <label>{% trans "到" %} </label>
            <input class="datepicker form-control" id="dt2">
            <span><a type="button" class='btn btn-success btn-add'>
                <span class='glyphicon glyphicon-arrow-down' style='padding-right:5px;'></span>{% trans '添加' %}
            </a></span>
            <span class="pull-right" style="margin-right:15px;">
                <a type="button" class='btn btn-primary btn-save'>
                    <span class='glyphicon glyphicon-floppy-disk' style='padding-right:5px;'></span>{% trans '保存' %}
            </a></span>
        </div><div class="clearfix"></div>
        <div class='table-responsive'>
            <table id='tb_auth' class="table table-condensed  table-bordered table-hover">
                <thead><tr></tr>
                    <th>{% trans '省、直辖市' %}</th><th>{% trans '市、自治州、直辖区' %}</th><th>{% trans '县、区、县级市' %}</th>
                    <th>{% trans '授权开始日期' %}</th><th>{% trans '授权结束日期' %}</th><th>{% trans '操作' %}</th>
                </tr></thead>
                <tbody>
                    {% for a in auth_areas %}
                        <tr>
                            <input type="hidden" class="auth_id"  value="{{a.5}}">
                            <input type="hidden" class="adm_id"  value="{{a.0}}">
                            <input type="hidden" class="dt_fr" value="{{a.3}}">
                            <input type="hidden" class="dt_to" value="{{a.4}}">
                            <td>{{a.6.0}}</td><td>{{a.6.1}}</td><td>{{a.6.2}}</td><td>{{a.3}}</td><td>{{a.4}}</td>
                            <td><a class="btn btn-primary btn-sm btn-warning btn-del">{% trans '删除' %}</a> </td>
                        </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        <div class="form-inline form-group" >
            <label class="col-sm-2 control-label g">{% trans '授权销售产品:' %} </label>
            <input id="product_id" type="hidden"/>
            <input id="product_label" placeholder="{% trans '请输入产品名称' %}">
            <input class="datepicker form-control" id="dt1_product">
            <label>{% trans "到" %} </label>
            <input class="datepicker form-control" id="dt2_product">
            <span><a type="button" class='btn btn-success btn-add-product'>
                <span class='glyphicon glyphicon-arrow-down' style='padding-right:5px;'></span>{% trans '添加' %}
            </a></span>
        </div><div class="clearfix"></div>
        <div class='table-responsive'>
            <table id='tb_auth_product' class="table table-condensed table-bordered table-hover">
                <thead><tr></tr>
                    <th>{% trans '产品名称 ' %}</th>
                    <th>{% trans '授权开始日期' %}</th><th>{% trans '授权结束日期' %}</th><th>{% trans '操作' %}</th>
                </tr></thead>
                <tbody>
                    {% for a in auth_products %}
                        <tr>
                            <input type="hidden" class="auth_id" value="{{a.id}}"/>
                            <input type="hidden" class="product_id"  value="{{a.product_id}}"/>
                            <input type="hidden" class="dt_fr"  value="{{a.dt_from}}"/>
                            <input type="hidden" class="dt_to"  value="{{a.dt_to}}"/>
                            <td>{{a.product.name}}</td><td>{{a.dt_from|date:"Y-m-d"}}</td><td>{{a.dt_to|date:"Y-m-d"}}</td>
                            <td><a class="btn btn-primary btn-sm btn-warning btn-del">{% trans '删除' %}</a> </td>
                        </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        {% else %}
            
        {% endifequal %}    
    {% endif %}
    -->
    <div class="pull-right">
                <a type="button" class='btn btn-primary btn-save'>
                    <span class='glyphicon glyphicon-floppy-disk' style='padding-right:5px;'></span>{% trans '保存' %}
                </a>
    </div>

{% endblock %}
