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

{% block css %}
    {{ block.super }}    
    #obj_form .my-lb{
        margin-left:0px;
        padding-left:0px;
    }
   
{% endblock %}



{% block js %}
    {{ block.super }}
    $('.btn-save').click(function(){
        $.SubmitForm('#obj_form form','{{request.path}}');
    });

    {% if form.instance.id %}
        $('#id_product').val('{{form.instance.product.id}}');
        $('#label_product').val('{{form.instance.product.name}}');
    {% endif %}


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


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

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





    $('.add-mat').click(function(){
        var id=$('#mat_id').val();
        var label=$('#mat_label').val();
        if(id==''){
            toastr.error("请先选择原材料！", "操作错误：",
                {timeOut: 800, positionClass: "toast-center-center"}
            );
            return;
        }else{                
            var e=false;
            $("#tb_mat tbody tr").each(function(){
                if($(this).attr('mat_id') == id){
                    e=true;
                    return false;          
                }
            });
            if(e){
                toastr.error(label+"已在原材料配比列表中！", "操作错误：",
                    {timeOut: 800, positionClass: "toast-center-center"}
                );
                return true; 
            }else{ 
                var count = $('#tb_mat tbody tr').length;
                var prefix='#id_bommaterials_bom-';                     
                html='<tr id="id_bommaterials_bom-'+count+'"'+' mat_id="'+id+'">'+                                                 
                    '<input type="hidden" value="'+$('#mat_unit').val()+'" '+
                        'id="id_bommaterials_bom-'+count+'-unit"         name="bommaterials_bom-'+count+'-unit"  />'+    
                    '<input type="hidden"  value="'+id+'" '+
                        'id="id_bommaterials_bom-'+count+'-materials"    name="bommaterials_bom-'+count+'-materials" />'+    
                    '<td><input type="checkbox"  class="form-control" '+
                        'id="id_bommaterials_bom-'+count+'-DELETE"       name="bommaterials_bom-'+count+'-DELETE"></td>'+               
                    '<td><span class="mat_name">'+label+'</span></td>'+  
                    '<td><input type="number" step="0.01" value="'+$('#mat_qty').val()+'"  class="form-control" '+
                        'id="id_bommaterials_bom-'+count+'-qty"          name="bommaterials_bom-'+count+'-qty" /></td>'+
                    '<td>'+$('#mat_unit').find("option:selected").text()+'</td>'+   
                    '</tr>';                             
                $('#tb_mat tbody').append(html); 
                $(prefix+'TOTAL_FORMS').attr('value', (count+1));     
            }                
        };
    });


    $('.add-sem').click(function(){
        var id=$('#sem_id').val();
        var label=$('#sem_label').val();
        if(id==''){
            toastr.error("请先选择半成品材料！", "操作错误：",
                {timeOut: 800, positionClass: "toast-center-center"}
            );
            return;
        }else{                
            var e=false;
            $("#tb_sem tbody tr").each(function(){
                if($(this).attr('sem_id') == id){
                    e=true;
                    return false;          
                }
            });
            if(e){
                toastr.error(label+"已在半成品材料配比列表中！", "操作错误：",
                    {timeOut: 800, positionClass: "toast-center-center"}
                );
                return true;
            }else{ 
                var count = $('#tb_sem tbody tr').length;
                var prefix='#id_bomsemifinishedgoods_bom-';     
                //bomsemifinishedgoods_bom
                html='<tr id="id_bomsemifinishedgoods_bom-'+count+'"'+' sem_id="'+id+'">'+                                                 
                    '<input type="hidden" value="'+$('#sem_unit').val()+'" '+
                        'id="id_bomsemifinishedgoods_bom-'+count+'-unit"                 name="bomsemifinishedgoods_bom-'+count+'-unit"  />'+    
                    '<input type="hidden"  value="'+id+'" '+
                        'id="id_bomsemifinishedgoods_bom-'+count+'-semifinishedgoods"    name="bomsemifinishedgoods_bom-'+count+'-semifinishedgoods" />'+    
                    '<td><input type="checkbox"  class="form-control" '+
                        'id="id_bomsemifinishedgoods_bom-'+count+'-DELETE"               name="bomsemifinishedgoods_bom-'+count+'-DELETE"></td>'+               
                    '<td><span class="sem_name">'+label+'</span></td>'+  
                    '<td><input type="number" step="0.01" value="'+$('#sem_qty').val()+'"  class="form-control" '+
                        'id="id_bomsemifinishedgoods_bom-'+count+'-qty"                  name="bomsemifinishedgoods_bom-'+count+'-qty" /></td>'+
                    '<td>'+$('#sem_unit').find("option:selected").text()+'</td>'+   
                    '</tr>';                             
                $('#tb_sem tbody').append(html); 
                $(prefix+'TOTAL_FORMS').attr('value', (count+1));     
            }                
        };
    });


    $('.add-pkg').click(function(){
        var id=$('#pkg_id').val();
        var label=$('#pkg_label').val();
        if(id==''){
            toastr.error("请先选择包装材料！", "操作错误：",
                {timeOut: 800, positionClass: "toast-center-center"}
            );
            return;
        }else{                
            var e=false;
            $("#tb_pkg tbody tr").each(function(){
                if($(this).attr('pkg_id') == id){
                    e=true;
                    return false;          
                }
            });
            if(e){
                toastr.error(label+"已在包装材料配比列表中！", "操作错误：",
                    {timeOut: 800, positionClass: "toast-center-center"}
                );
                return true;
            }else{ 
                var count = $('#tb_pkg tbody tr').length;
                var prefix='#id_bompackaging_bom-';  
                html='<tr id="id_bompackaging_bom-'+count+'"'+' pkg_id="'+id+'">'+                                                 
                    '<input type="hidden" value="'+$('#pkg_unit').val()+'" '+
                        'id="id_bompackaging_bom-'+count+'-unit"         name="bompackaging_bom-'+count+'-unit"  />'+    
                    '<input type="hidden"  value="'+id+'" '+
                        'id="id_bompackaging_bom-'+count+'-packaging"    name="bompackaging_bom-'+count+'-packaging" />'+    
                    '<td><input type="checkbox"  class="form-control" '+
                        'id="id_bompackaging_bom-'+count+'-DELETE"       name="bompackaging_bom-'+count+'-DELETE"></td>'+               
                    '<td><span class="mat_name">'+label+'</span></td>'+  
                    '<td><input type="number" step="0.01" value="'+$('#pkg_qty').val()+'"  class="form-control" '+
                        'id="id_bompackaging_bom-'+count+'-qty"          name="bompackaging_bom-'+count+'-qty" /></td>'+
                    '<td><input type="sting" value="'+$('#pkg_qty').val()+'"  class="form-control" '+
                      'id="id_bompackaging_bom-'+count+'-specification"          name="bompackaging_bom-'+count+'-specification" /></td>'+
                    '<td>'+$('#pkg_unit').find("option:selected").text()+'</td>'+ 
                    '<td><input type="number" value="'+$('#pkg_qty').val()+'"  class="form-control" '+
                      'id="id_bompackaging_bom-'+count+'-forecast"          name="bompackaging_bom-'+count+'-forecast" /></td>'+  
                    '</tr>';                             
                $('#tb_pkg tbody').append(html); 
                $(prefix+'TOTAL_FORMS').attr('value', (count+1));     
            }                
        };
    });





{% 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.code }}</div>
    </div>      
    <div class="form-group ">
        <label class="col-sm-2 control-label ">{% trans '产成品名称:' %} </label>        
        <div class="col-sm-10"><input id='product_label' value='{{form.instance.product.name}}'/></div>
    </div>   


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

{% endblock %}



{% block fmset %}   
        <div class='form-group'>            
            <label class="col-sm-1 control-label" style='margin-left:0px;padding-left:0px;text-align:left;color:green;'>
                {% trans '原材料配比:' %}</label>
            <div class='col-sm-2'>
                <input type='hidden' id='mat_id' />
                <input type="text" id='mat_label' class="form-control" placeholder="{% trans '原材名称' %}">
            </div>   
            <div class='col-md-2'>               
                <input type="text" id='mat_qty' class="form-control"  placeholder="{% trans '数量' %}">
            </div>
            <div class='col-md-2'>                
                <select id="mat_unit" type="text" class="form-control" >
                    {% for u in units_mat %}
                        <option value='{{u.id}}'>{{u.name}}</option>
                    {% endfor %}
                </select>
            </div>  
            <span><a type="button" class='btn btn-success add-mat'>
                <span class='glyphicon glyphicon-arrow-down' style='padding-right:5px;'></span>{% trans '添加' %}
            </a></span>
        </div>
        <div class='table-responsive'>
        <table id='tb_mat' class="table table-condensed table-bordered table-hover">
            <thead><tr > 
                <th>{% trans '删除' %}</th>
                <th>{% trans '原材名称 ' %}</th>
                <th>{% trans '数量 ' %}</th>
                <th>{% trans '单位' %}</th>     
            </tr></thead>
            <tbody>
                {% for f in formset_m %}
                <tr  class='p' mat_id='{{f.instance.materials.id}}'>
                    {{f.id}}{{f.unit}}{{f.materials}}                    
                    <td>{{f.DELETE}}</td>      
                    <td>{{ f.instance.materials.name }}</td>
                    <td>{{ f.qty }}</td>
                    <td>{{ f.instance.unit.name }}</td>
                </tr>
                {% endfor %}
            </tbody>
            {{ formset_m.management_form }}   
        </table>
        </div>


        <div class='form-group' style='margin-top:20px;'>            
            <label class="col-sm-1 control-label" style='margin-left:0px;padding-left:0px;text-align:left;color:green;'>
                {% trans '半成品配比:' %}</label>
            <div class='col-sm-2'>
                <input type='hidden' id='sem_id' />
                <input type="text" id='sem_label' class="form-control" placeholder="{% trans '半成品' %}">
            </div>   
            <div class='col-md-2'>               
                <input type="text" id='sem_qty' class="form-control"  placeholder="{% trans '数量' %}">
            </div>
            <div class='col-md-2'>                
                <select id="sem_unit" type="text" class="form-control" >
                    {% for u in units_sem %}
                        <option value='{{u.id}}'>{{u.name}}</option>
                    {% endfor %}
                </select>
            </div>  
            <span><a type="button" class='btn btn-success add-sem'>
                <span class='glyphicon glyphicon-arrow-down' style='padding-right:5px;'></span>{% trans '添加' %}
            </a></span>
        </div>
        <div class='table-responsive'>
        <table id='tb_sem' class="table table-condensed table-bordered table-hover">
            <thead><tr>
                <th>{% trans '删除' %}</th>
                <th>{% trans '半成品名称 ' %}</th>
                <th>{% trans '数量 ' %}</th>
                <th>{% trans '单位' %}</th>     
            </tr></thead>
            <tbody>
                {% for f in formset_s %}
                <tr  class='p' sem_id='{{f.instance.semifinishedgoods.id}}'>
                    {{f.id}}{{f.unit}}{{f.semifinishedgoods}}                    
                    <td>{{f.DELETE}}</td>      
                    <td>{{ f.instance.semifinishedgoods.name }}</td>
                    <td>{{ f.qty }}</td>
                    <td>{{ f.instance.unit.name }}</td>
                </tr>
                {% endfor %}
            </tbody>
            {{ formset_s.management_form }}   
        </table>
        </div>

        <div class='form-group'  style='margin-top:20px;'>            
            <label class="col-sm-1 control-label" style='margin-left:0px;padding-left:0px;text-align:left;;color:green;'>
                {% trans '包装材料耗用:' %}</label>
            <div class='col-sm-2'>
                <input type='hidden' id='pkg_id' />
                <input type="text" id='pkg_label' class="form-control" placeholder="{% trans '包装材料' %}">
            </div>   
            <div class='col-md-2'>               
                <input type="text" id='pkg_qty' class="form-control"  placeholder="{% trans '数量' %}">
            </div>
            <div class='col-md-2'>                
                <select id="pkg_unit" type="text" class="form-control" >
                    {% for u in units_pkg %}
                        <option value='{{u.id}}'>{{u.name}}</option>
                    {% endfor %}
                </select>
            </div>  
            <span><a type="button" class='btn btn-success add-pkg'>
                <span class='glyphicon glyphicon-arrow-down' style='padding-right:5px;'></span>{% trans '添加' %}
            </a></span>
        </div>
        <div class='table-responsive'>
        <table id='tb_pkg' class="table table-condensed table-bordered table-hover">
            <thead><tr>
                <th>{% trans '删除' %}</th>
                <th>{% trans '包装材料名称 ' %}</th>
                <th>{% trans '数量 ' %}</th>
                <th>{% trans '规格' %}</th>  
                <th>{% trans '单位' %}</th>
                <th>{% trans '预计单价' %}</th>       
            </tr></thead>
            <tbody>
                {% for f in formset_p %}
                <tr  class='p' pkg_id='{{f.instance.packaging.id}}'>
                    {{f.id}}{{f.unit}}{{f.packaging}}                    
                    <td>{{f.DELETE}}</td>      
                    <td>{{ f.instance.packaging.name }}</td>
                    <td>{{ f.qty }}</td>
                    <td>{{ f.instance.specification.name }}</td>
                    <td>{{ f.instance.unit.name }}</td>
                    <td>{{ f.instance.forecast.name }}</td>
                </tr>
                {% endfor %}
            </tbody>
            {{ formset_p.management_form }}   
        </table>
        </div>

{% endblock %}
