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

    #obj_form .checkbox { 
        padding-left:20px;
    }
    #obj_form .select2_multiple{
        max-height:80px;
    }

{% endblock %}



{% block js %}
    {{ block.super }}      

    {% if form.instance.id %}
        $('#label_company').val('{{form.instance.company.name}}');
    {% endif %}



    $.datetimepicker.setLocale('zh');
    $('.datetimepicker').datetimepicker({
        i18n:{
            zh:{
                months:['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月',],
                dayOfWeek:["一", "二", "三", "四","五", "六", "日",]
            }
        },
        //timepicker:true,
        format:'Y-m-d H:i:s',        
    });




    $.ajax({
        url: "/gis/adm/1/",
        success: function(data) { 
            var provinces="<option value=''>---------------</option>";
            $.each(data,function(i,d){
                provinces=provinces+"<option value='"+d[0]+"'>"+d[1]+"</option>";
            });
            $('#obj_form select.province').html(provinces);
            s=$('#obj_form').find('.province_gis').val();
            if (s!='{}'){
                v=JSON.parse(s).id;
                $('#obj_form').find('.province option[value="'+v+'"]').attr("selected",true);
                $.ajax({
                    url: "/gis/adm/2/"+v+"/",
                    success: function(data) {
                        var html="<option value='' selected='selected'>---------</option>";
                        $.each(data,function(i,d){
                            html=html+"<option value='"+d[0]+"'>"+d[1]+"</option>";
                        });
                        $('#obj_form').find('.city').html(html);
                        s=$('#obj_form').find('.city_gis').val();
                        if (s!='{}'){
                            v=JSON.parse(s).id;
                            $('#obj_form').find('.city option[value="'+v+'"]').attr("selected",true);
                            $.ajax({
                                url: "/gis/adm/3/"+v+"/",
                                success: function(data) {
                                    var html="<option value='' selected='selected'>---------</option>";
                                    $.each(data,function(i,d){
                                        html=html+"<option value='"+d[0]+"'>"+d[1]+"</option>";
                                    });
                                    $('#obj_form').find('.county').html(html);
                                    s=$('#obj_form').find('.county_gis').val();
                                    if (s!='{}'){
                                        v=JSON.parse(s).id;
                                        $('#obj_form').find('.county option[value="'+v+'"]').attr("selected",true);
                                    }
                                },
                                dataType: 'jsonp',
                                jsonp: 'callback'
                            });
                        }
                    },
                    dataType: 'jsonp',
                    jsonp: 'callback'
                });
            };
        },
        dataType: 'jsonp',
        jsonp: 'callback'
    });    

    $('#obj_form').on('change','.province',function(){
        var id=$(this).val();        
        $.ajax({
            url: "/gis/adm/2/"+id+"/",
            success: function(data) {
                var html="<option value='' selected='selected'>---------</option>";               
                $.each(data,function(i,d){
                    html=html+"<option value='"+d[0]+"'>"+d[1]+"</option>";
                });
                $('#obj_form').find('.city').html(html);
            },
            dataType: 'jsonp',
            jsonp: 'callback'
        }); 
    });
    $('#obj_form').on('change','.city',function(){
        var id=$(this).val();       
        $.ajax({
            url: "/gis/adm/3/"+id+"/",
            success: function(data) {
                var html="<option value='' selected='selected'>---------</option>";               
                $.each(data,function(i,d){
                    html=html+"<option value='"+d[0]+"'>"+d[1]+"</option>";
                });
                $('#obj_form').find('.county').html(html);
            },
            dataType: 'jsonp',
            jsonp: 'callback'
        }); 
    });
    
    
    
    $('.btn-save').click(function(){
        var me, v,d;
        me=$('#obj_form');
        v=$(me).find('.province').val();
        if (v!=''){
            d={
                'id':v,
                'name':$(me).find(".province option:selected").text()
            };
            $(me).find('.province_gis').val(JSON.stringify(d));
        };  
        v=$(me).find('.city').val();
        if (v!=''){
            d={
                'id':v,
                'name':$(me).find(".city option:selected").text()
            };
            $(me).find('.city_gis').val(JSON.stringify(d));
        };
        v=$(me).find('.county').val();
        if (v!=''){
            d={
                'id':v,
                'name':$(me).find(".county option:selected").text()
            };
            $(me).find('.county_gis').val(JSON.stringify(d));
        };    
            
        $.SubmitForm('#obj_form form','{{request.path}}');
    });


    
    //2020-11-24
    $('#data-content .btn-delete').click(function(){
        $.ajax({
            url:'/obj/delete/agent/{{form.instance.id}}/',                             
            success: function(data) {
                if(data.cnt>0){  
                    toastr.success('当前经销商已删除!', '操作成功：',
                        {timeOut: 800, positionClass: "toast-center-center"}
                    );
                    $.RefreshContent("/obj/list/agent/");                   
                };
            },

        });       
    
    });  




    $('#obj_form').on('click','.btn-location',function(){ 
        event.preventDefault();         
        var gis_city_id=$('#obj_form .city').val();
        var name=$('#id_name').val();
        var series_name=$('#id_series option:selected').text(); 
        var obj_id='{{form.instance.id}}';
        var province=$('#obj_form .province').find('option:selected').text();
        var city=$('#obj_form .city').find('option:selected').text();
        

        if($.isEmpty(gis_city_id) || $.isEmpty(name) || $.isEmpty(series_name)){
            toastr.error("请先设置门店名称和所在城市及授权品牌！", "操作错误：",
                {timeOut: 800, positionClass: "toast-center-center"}
            );
            return false;            
        }else{            
            var url='/company/franchise/outlet/';                
            url=url+"?lng_lat="+$('#obj_form').find('.lng_lat').val()+
                "&el_id=form"+
                "&gis_city_id="+gis_city_id+
                "&outlet_name="+name+
                "&obj_id="+obj_id+
                "&province="+province+
                "&city="+city;      
            $.OpenUrlCfmDlg(url,name+" ["+series_name+"] 地理位置：","保存","取消");
        }
    }); 
    


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


    var what='';  
    if($('#id_lng_lat').val()==''){
        what='设置';
    }else{
        what='修改';
    };
    $('#obj_form .btn-location').tooltip({
        title:'点击'+what+'门店地理位置',
		placement:'left',
		animation:'true',
		container:'#obj_form',
		trigger:'hover'        
    });




   
{% endblock %}

 
{% block subtitle-1 %}
    {% trans '加盟商管理' %}
{% endblock %}
{% block subtitle-2 %}
    {% trans '门店管理' %}
{% endblock %}       


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

    <div class="form-group ">
        <label class="col-sm-1 control-label ">{% trans '加盟商:' %}</label>
        <div class="col-sm-11">
            <input class='form-control' id='label_company' />            
        </div>                
    </div>
    <div class="form-group ">
        <label class="col-sm-1 control-label ">{% trans '门店名称:' %}</label>
        <div class="col-sm-11">{{ form.name }}</div>                
    </div>
    <div class="form-group ">
        <label class="col-sm-1 control-label ">{% trans '省份:' %}</label>
        <div class="col-sm-3">
            <select class='province'></select>
        </div>   
        <label class="col-sm-1 control-label ">{% trans '城市:' %}</label>
        <div class="col-sm-3">
            <select class='city'></select>
        </div>       
        <label class="col-sm-1 control-label ">{% trans '县区:' %}</label>
        <div class="col-sm-3">
            <select class='county'></select>
        </div>       
    </div>
    <div class="form-group">
        <label class="col-sm-1 control-label ">{% trans '详细地址:' %}</label>
        <div class='col-sm-11'>
            <div class="input-group">{{form.address}}
                <span class="input-group-btn"><a class='btn btn-success btn-location'>
                    <span class='glyphicon glyphicon-map-marker'></span></a></span>                  
            </div>               
        </div>
    </div>
    <div class="form-group ">
        <label class="col-sm-1 control-label ">{% trans '查询老师:' %}</label>
        <div class="col-sm-2">{{form.qry_teacher}}</div>   
        <label class="col-sm-1 control-label ">{% trans '首查时间:' %}</label>
        <div class="col-sm-2">{{form.qry_time}}</div>   
        <label class="col-sm-1 control-label ">{% trans '联系人:' %}</label>
        <div class="col-sm-2">{{form.contacts}}</div>   
        <label class="col-sm-1 control-label ">{% trans '职  位:' %}</label>
        <div class="col-sm-2">{{form.position}}</div>  
    </div>

    <div class="form-group ">
        <label class="col-sm-1 control-label ">{% trans '授权品牌:' %}</label>
        <div class="col-sm-2">{{form.series}}</div>   
        <label class="col-sm-1 control-label ">{% trans '门店状态:' %}</label>
        <div class="col-sm-2">{{form.status}}</div>   
        <label class="col-sm-1 control-label ">{% trans '特殊情况:' %}</label>
        <div class="col-sm-5">{{form.remark}}</div>   
    </div>

    
    

    <div  class="form-group">        
        <a type="button" class='btn btn-primary btn-save' style="float:right;">
            <span class='glyphicon glyphicon-floppy-disk' style='padding-right:5px;'></span>{% trans '保存' %}
        </a>
    </div>

{% endblock %}
