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




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

        {% if not perms.company.sys_admin %}            
            $('#data-content input,#data-content .btn,#data-content select').attr("disabled",true);            
        {% endif %}

        {% ifnotequal request.user.company.level_id 1 %}
            $('#data-content input,#data-content .btn,#data-content select').attr("disabled",true);
        {% endifnotequal %}


        $.datetimepicker.setLocale('zh');

        

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

        

        $('.add-vendor').click(function(){            
            var b=false;
            var id=$('#vendor_id').val();
            var name=$('#vendor_label').val();
            var company=$('#id_company').val()
            <!-- var unit=$('#product_unit').val(); -->
            
            var unit_price=$('#vendor_base_unit').val();

            $('#packaging-vendor tbody tr').each(function(idx,val){
                if($(this).find('.pid').val()==$('#vendor_id').val()){
                  b=true;
                  return;
                }
            });

            if($.isEmpty(id) || $.isEmpty(name) || $.isEmpty(unit_price)){
                toastr.error("供应商/单价不能为空！", "操作错误：",
                    {timeOut: 800, positionClass: "toast-center-center"}
                );                
            }else{
                if (b){
                    toastr.error("已在列表中！", "操作错误：",
                        {timeOut: 800, positionClass: "toast-center-center"}
                    );                
                }else{     
                    var prefix='#id_packagingvendor_set-';   
                    $(prefix+'__prefix__').find('.vendor').text(name); 
                    var count = $('#packaging-vendor tbody tr').length; 
                    var tmplMarkup = '<tr class="p">'+$(prefix+'__prefix__').html()+'</tr>';    
                    var compiledTmpl = tmplMarkup.replace(/__prefix__/g, count);     
                    $('#packaging-vendor tbody').append(compiledTmpl); 
                    $(prefix+count+'-vendor').val(id);   
                    $(prefix+count+'-unit_price').val(unit_price); 
                    $(prefix+count+'-company').val(company);                   
                    $(prefix+'TOTAL_FORMS').attr('value', (count+1));  
                }
            }
        });
        
    $('#id_means').change(function(){
        if($(this).val()==1){
            $('#id_tm_app_latest').val('').attr("disabled",true); 
        }else{
            $('#id_tm_app_latest').attr("disabled",false);
        }
    });

    $('.btn-save').click(function(){
        a=[];
        $("#sel-div option").each(function (){
                a.push($(this).val());
        });  
        if(a.length>0){
                $('#id_div_ids').val(a.join(','));
        };
        a=[];
        $("#sel-subdiv option").each(function (){
                a.push($(this).val());
        });  
        if(a.length>0){
                $('#id_subdiv_ids').val(a.join(','));
        };


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


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




{% 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-5">{{ form.code }}</div>
    
        <label class="col-sm-1 control-label ">{% trans '物料名称' %} </label>
        <div class="col-sm-5">{{ form.name }}</div>
        
    </div>
    
    <div class="form-group ">
      <label class="col-sm-1 control-label ">{% trans '计量类别' %} </label>
      <div class="col-sm-2">{{ form.measure }}</div>
    </div>

    <div class="form-group ">
      <label class="col-sm-1 control-label ">{% trans '基础单位' %} </label>
      <div class="col-sm-2">{{ form.unit }}</div>
    </div>


    
    <!-- <div class="form-group " style="margin-bottom:0px;padding-bottom:0px;">
        <label class="col-sm-1 control-label ">{% trans '指定战区' %} </label>
        <div class="col-sm-5"> 
            <div class="input-group">  
            <select id='div_id'>
                    <option value=''>所有战区</option>
                    {% for o in saldivs %}
                        <option value='{{o.0}}'>{{o.1}}</option>
                    {% endfor %}                        
            </select>                
            <span class="input-group-btn"><button type="button" class="btn btn-success add-div">
                <span class='glyphicon glyphicon-arrow-down'></span></button></span> 
            </div>
        </div>
        
        <label class="col-sm-1 control-label ">{% trans '指定分区' %} </label>
        <div class="col-sm-5">
            <div class="input-group">                 
                <select  id="subdiv_id"><option>所有分区</option></select>
                <span class="input-group-btn"><button type="button" class="btn btn-success add-subdiv">
                    <span class='glyphicon glyphicon-arrow-down'></span></button></span>
            </div>
        </div>
    </div>   
    <div class="form-group" style='padding-top:0px;margin-top:0px;'>
        <div class='col-sm-5 col-md-offset-1'>
            <select class="select2_multiple form-control" id="sel-div" multiple="multiple">
                {% for o in div_ids %}
                    <option value='{{o.0}}'>{{o.1}}</option>
                {% endfor %}                       
            </select>        
        </div>
        <div class='col-sm-5 col-md-offset-1'>
            <select class="select2_multiple form-control" id="sel-subdiv" multiple="multiple">
                {% for o in subdiv_ids %}
                    <option value='{{o.0}}'>{{o.1}}</option>
                {% endfor %}        
            </select>   
        </div>
    </div> -->
    

{% endblock %}


 {% block fmset %}  
        <table  style="display:none;">
            <tr id="id_packagingvendor_set-__prefix__" >  
                <input type="hidden" value="{{company}}" name="company" /> 
                <td>{{formset_v.empty_form.DELETE}}{{formset_v.empty_form.id}}{{formset_v.empty_form.status}}{{formset_v.empty_form.company}}</td>      
                <td>{{formset_v.empty_form.vendor}}<span class='vendor'></span></td> 
                <td>{{formset_v.empty_form.unit_price}}</td>
                <!-- <td>{{formset_v.empty_form.package_id}}</td> -->
                <td>{{formset_v.empty_form.remark}}</td>
            </tr>
        </table>
        {{ formset_v.management_form }}       

        <div class="x_panel" id='packaging-vendor'>
            <div class="x_title">
                <h2 style='font-size:15px;'><i class="fa fa-gift"></i> 供应商  </h2>
                <ul class="nav navbar-right panel_toolbox">
                      <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
                      </li>
                      <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
                        <ul class="dropdown-menu" role="menu">
                          <li><a href="#">Settings 1</a>
                          </li>
                          <li><a href="#">Settings 2</a>
                          </li>
                        </ul>
                      </li>
                      <li><a class="close-link"><i class="fa fa-close"></i></a>
                      </li>
                </ul>
                <div class="clearfix"></div>
            </div>
            <div class="x_content">                
                <div class="form-group ">
                    <div class='col-sm-6'><input id='vendor_label' placeholder='供应商名称'><input type='hidden' id='vendor_id'></div>
                    <div class='col-sm-1'><input id='vendor_base_unit' placeholder='单价'></div>
                    <!-- <div class='col-sm-1'><select id='product_unit'>
                        <option value='3'>箱</option>
                        <option value='1'>瓶</option>
                    </select></div> -->
                    <span class="input-group-btn"><button type="button"  class="btn btn-success add-vendor">
                    <span class='glyphicon glyphicon-arrow-down'></span></button></span>                
                </div>
                 
                <div class='table-responsive' style='padding:10px;'>
                    <table class="table table-condensed table-bordered table-hover">
                        <thead>
                            <th style='width:5%;'>删除</th>
                            <th style='width:35%;'>供应商名称</th>
                            <th style='width:10%;'>单价</th>
                            <!-- <th style='width:10%;'>单位</th> -->
                            <th style='width:45%;'>备注</th>
                        </thead>
                        <tbody>
                            {% for f in formset_v %}
                                <tr>      
                                    <td>{{f.DELETE}}{{f.id}}{{f.status}}{{f.company}}</td>                                    
                                    <td>{{f.vendor}}<span class='vendor'>{{f.instance.vendor.name}}</span></td> 
                                    <td>{{f.unit_price}}</td> 
                                    <!-- <td>{{f.package_id}}</td>     -->
                                    <td>{{f.remark}}</td>
                                </tr>
                            {% endfor %}                        
                        </tbody>
                    </table>
                </div>
            </div>
        </div>   

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

    {% endblock %}
   

