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

{% block css %}
    {{ block.super }}
    
    #obj_form select.unit{
        min-width:100px;
        margin-left:5px;
    }
    
{% endblock %}



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

   

    var my_adm_1,my_adm_2,my_adm_3;
    my_adm_1={'id':'{{myadms.province.0}}','name':'{{myadms.province.1}}'};
    my_adm_2={'id':'{{myadms.city.0}}','name':'{{myadms.city.1}}'};
    my_adm_3={'id':'{{myadms.county.0}}','name':'{{myadms.county.1}}'};

  

    var phtml='';
    $('#id_province_gis_id').append("<option value=''>-----------------</option>");
    {% for p in provinces %}
        phtml=phtml+"<option value='{{p.0}}'>{{p.1}}</option>";
    {% endfor %}
    $('#id_province_gis_id').append(phtml);

    
    


    
    
  


    $('.add-unit').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>");
                }
            };
    });

   



    $('.add-unit').click(function(){
            var me=$(this);
            var id=$(me).parent().parent().find('select').val();
            
            var adm='';

           
    });


    $("#province,#id_province_gis_id").change(function() {
        var sel;
        if($(this).attr('id')=='province'){
            sel=$('#city');        
        }else{
            sel=$('#id_city_gis_id');
        }        
        var province_id = $(this).val();
        getCity(province_id,sel);
    });
   


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

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

        var a=[];
        $("#sel-adm option").each(function (){ a.push($(this).val()); });
        if(a.length>0){ 
            $('#id_adm_ids').val(a.join(','));
        }else{
            $('#id_adm_ids').val('');
        };
        
        a=[]
        if(a.length>0){   
            $('#id_lng_lat').val(a.join(',')); 
        }else{
            $('#id_lng_lat').val('');
        };
        
        $.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 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 %}

    //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/");                   
                };
            },

        });       
    
    });

   
{% 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-10">{{ form.name }}</div>
    </div>   
    <div class="form-group ">
        <label class="col-sm-2 control-label ">{% trans '材料编码:' %} </label>
        <div class="col-sm-10">{{ form.code }}</div>
    </div>   
    <div class="form-group ">
        <label class="col-sm-2 control-label ">{% trans '计量单位:' %} </label>       
        <div class='col-md-10'>    
            <div class="input-group"> 
                <select id="unit" class="form-control unit">
                    {% for u in units %}
                        <option value="{{u.id}}" {% if u.is_default %}selected="selected" {% endif %}>{{u.name}}</option>
                    {% endfor %}
                </select>
                <span class="input-group-btn"><button type="button" class="btn btn-success add-unit">
                    <span class='glyphicon glyphicon-arrow-down'></span></button>
                </span>
            </div>
        </div>  
    </div> 
    <div class='col-sm-10 col-md-offset-2'>
            <select class="select2_multiple form-control" id="sel-units" multiple="multiple">
                {% for o in form.instance.units %}
                    <option value='{{o.id}}'>{{o.name}}</option>
                {% endfor %}        
            </select>        
    </div>
    <div class="form-group ">
        <label class="col-sm-2 control-label ">{% trans '备注说明:' %} </label>
        <div class="col-sm-10">{{ form.remark }}</div>
    </div>   
   

{% endblock %}
