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

{% block css %}
    {{ block.super }}    
   
   
{% endblock %}



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

    var cid='{{request.user.company_id}}';
    
    $('.btn-save').click(function(){
        $('#tb tr .company_id').val(cid);
        
        var store_fr_id=$('#id_store_fr').val();
        var store_to_id=$('#id_store_to').val();
        if(!$.isEmpty(store_fr_id)){
            $('#tb tr .store_fr_id').val(store_fr_id);        
        };
        if(!$.isEmpty(store_to_id)){
            $('#tb tr .store_to_id').val(store_to_id);        
        };       

        $("#tb tr").each(function(){
            var me=this;
            $(me).find('.vtype_id').val('{{form.instance.vtype_id}}');

            var qty=$(me).find('.qty').val();
            var unit=$(me).find('.unit').val();

            if($.isEmpty(qty) || $.isEmpty(unit)){
                $(me).find("[id$='-DELETE']").attr('checked', 'true');
            };
        });
        {% if voucher.vendor_needed %}
            var vs=[];
            $("#sel-vendor option").each(function (){
                vs.push('{"id":'+$(this).val()+',"name":"'+$(this).text()+'"}');
            });              
            $('#id_vendors').val('['+vs.join(',')+']');            
        {% endif %}

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

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



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

    {% if voucher.customer_needed %}
        $('#id_customer').val('{{form.instance.customer_id}}');
        $('#label_customer').val('{{form.instance.customer.name|default_if_none:""}}');
        $('#label_customer').autocomplete({                
            source: function(request, response) {
                $.ajax({
                    url: '/obj/ajax/autocomplete/mes_customer/',
                    dataType: "json",
                    data: {
                        term:   $("#label_customer").val() ,//搜索栏里的内容
                        company_id:  {{request.user.company_id}} //额外参数
                    },
                    success: function(data) {
                        response(data);
                    }
                });
            },   
            select: function (event, ui) {
                $("#id_customer").val(ui.item.value);
                $("#label_customer").val(ui.item.label);
                return false;
            }
        });
    {% endif %}


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

        $('.add-vendor').click(function(){
            var id=$('#id_vendor').val();
            var label=$('#label_vendor').val();
            if(id==''){
                toastr.error("请先选择供应商！", "操作错误：",
                    {timeOut: 800, positionClass: "toast-center-center"}
                );
                return;
            }else{
                var e=false;
                $("#sel-vendor 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-vendor').append("<option value='"+id+"'>"+label+"</option>");
                }
            };
        });
    {% endif %}


    $('.datepicker').datepicker({dateFormat: 'yy-mm-dd'});    
    setDatePickerZh();
    $('#id_dt_voucher').val('{{form.instance.dt_voucher|date:"Y-m-d"}}');
    
    {% if form.instance.order %}
        $('#label_order').val('{{form.instance.order.code}}');
    {% endif %}

    
    window.v_lb='';
    window.v_id='';    

    $("#tb").on("focus",".mat_label", function (event) {
        $(this).autocomplete({               
            source: function(request, response) {            
                window.v_lb=$(this.element)[0];
                window.v_id=$(window.v_lb).closest('td').find('.mat_id');   
                $.ajax({
                    url: '/obj/ajax/autocomplete/materials/',
                    dataType: "json",
                    data: {
                        term: $(v_lb).val() ,//搜索栏里的内容
                        company_id:  cid ,//额外参数
                    },
                    success: function(data) {
                        response(data);
                    }
                });
            },
            select: function (event, ui) {
                $(window.v_id).val(ui.item.value);
                $(window.v_lb).val(ui.item.label);
                return false;
            }
        }); 
    });


    $("#tb").on("focus",".pkg_label", function (event) {
        $(this).autocomplete({               
            source: function(request, response) {            
                window.v_lb=$(this.element)[0];
                window.v_id=$(window.v_lb).closest('td').find('.pkg_id');   
                $.ajax({
                    url: '/obj/ajax/autocomplete/packaging/',
                    dataType: "json",
                    data: {
                        term: $(v_lb).val() ,//搜索栏里的内容
                        company_id:  cid ,//额外参数
                    },
                    success: function(data) {
                        response(data);
                    }
                });
            },
            select: function (event, ui) {
                $(window.v_id).val(ui.item.value);
                $(window.v_lb).val(ui.item.label);
                return false;
            }
        }); 
    });

    $("#tb").on("focus",".sem_label", function (event) {
        $(this).autocomplete({               
            source: function(request, response) {            
                window.v_lb=$(this.element)[0];
                window.v_id=$(window.v_lb).closest('td').find('.sem_id');   
                $.ajax({
                    url: '/obj/ajax/autocomplete/semifinishedgoods/',
                    dataType: "json",
                    data: {
                        term: $(v_lb).val() ,//搜索栏里的内容
                        company_id:  cid ,//额外参数
                    },
                    success: function(data) {
                        response(data);
                    }
                });
            },
            select: function (event, ui) {
                $(window.v_id).val(ui.item.value);
                $(window.v_lb).val(ui.item.label);
                return false;
            }
        }); 
    });


    $("#tb").on("focus",".product_label", function (event) {
        $(this).autocomplete({               
            source: function(request, response) {            
                window.v_lb=$(this.element)[0];
                window.v_id=$(window.v_lb).closest('td').find('.fg_id');   
                $.ajax({
                    url: '/obj/ajax/autocomplete/product/',
                    dataType: "json",
                    data: {
                        term: $(v_lb).val() ,//搜索栏里的内容
                        company_id:  cid ,//额外参数
                    },
                    success: function(data) {
                        response(data);
                    }
                });
            },
            select: function (event, ui) {
                $(window.v_id).val(ui.item.value);
                $(window.v_lb).val(ui.item.label);
                return false;
            }
        }); 
    });

    $('.btn-add').click(function(){   
        var prefix='#id_stkdetails_set-';              
        var count = $('#tb tbody tr').length; 
        var tmplMarkup = '<tr>'+$(prefix+'__prefix__').html()+'</tr>';    
        var compiledTmpl = tmplMarkup.replace(/__prefix__/g, count);     
        $('#tb tbody').append(compiledTmpl);   
        $(prefix+'TOTAL_FORMS').attr('value', (count+1));           
    });
    

{% endblock %}

 
{% block subtitle-1 %}
    {{ subtt.0 }}
{% endblock %}
{% block subtitle-2 %}
    {{ subtt.1 }}
{% endblock %}      


{% block form_content %}
    {{ block.super }}    
    <input id='previous_url' name='previous_url' type='hidden' value='{{previous_url}}' />
    
    {% if voucher.vendor_needed %}
        <div class="form-group ">
            <label class="col-sm-1 control-label ">{% trans '单据编码:' %} </label>
            <div class="col-sm-2">{{form.code}}</div>
   
            <label class="col-sm-1 control-label ">{% trans '生产订单:' %} </label>
            <div class="col-sm-2">             
                 <input id='label_order' class='form-control'  />
            </div>    
            <label class="col-sm-1 control-label ">{% trans '仓库名称:' %} </label>        
            <div class="col-sm-2">{{form.store_fr}}</div>
    
            <label class="col-sm-1 control-label ">{% trans '单据日期:' %} </label>        
            <div class="col-sm-2">{{ form.dt_voucher }}</div>
        </div>   
        <div class='form-group'>
            <label class="col-sm-1 control-label ">{% trans '供应商:' %} </label>   
            <input type="hidden" id="id_vendor" />
            <div class="col-sm-11">                
                <div class="input-group"> 
                    <input id='label_vendor' class='form-control' />            
                    <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>
        </div>
        <div class="form-group" style='margin-top:-32px;'>
            <div class='col-sm-11 col-md-offset-1'>
            <select class="select2_multiple form-control" id="sel-vendor" multiple="multiple">
                {% for o in form.instance.vendors %}
                    <option value='{{o.id}}'>{{o.name}}</option>
                {% endfor %}                                     
            </select>
            </div>
        </div>
        <div class="form-group ">
            <label class="col-sm-1 control-label ">{% trans '备注说明:' %} </label>        
            <div class="col-sm-11">{{form.remark}}</div>
        </div>        
    {% endif %}
    {% if voucher.customer_needed %}
        <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">             
                <input id='label_order' class='form-control'  />
            </div>
        </div> 
        <div class="form-group ">
            <label class="col-sm-1 control-label ">{% trans '仓库名称:' %} </label>        
            <div class="col-sm-5">{{form.store_fr}}</div>
    
            <label class="col-sm-1 control-label ">{% trans '单据日期:' %} </label>        
            <div class="col-sm-5">{{ form.dt_voucher }}</div>
        </div>   
        <div class="form-group ">
            <label class="col-sm-1 control-label ">{% trans '代工客户:' %} </label>        
            <div class="col-sm-11"><input class='form-control' id='label_customer' /></div>  
        </div>   


        <div class="form-group ">
            <label class="col-sm-1 control-label ">{% trans '备注说明:' %} </label>        
            <div class="col-sm-11">{{form.remark}}</div>
        </div>  
    {% endif %}
    {% if normal %}
        <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">             
                <input id='label_order' class='form-control'  />
            </div>
        </div> 
        <div class="form-group ">
            <label class="col-sm-1 control-label ">{% trans '仓库名称:' %} </label>        
            <div class="col-sm-5">{{form.store_fr}}</div>
    
            <label class="col-sm-1 control-label ">{% trans '单据日期:' %} </label>        
            <div class="col-sm-5">{{ form.dt_voucher }}</div>
        </div>   
        <div class="form-group ">
            <label class="col-sm-1 control-label ">{% trans '备注说明:' %} </label>        
            <div class="col-sm-11">{{form.remark}}</div>
        </div>  
    {% endif %}



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

{% endblock %}



{% block fmset %}   

    <table style="display:none;">
            <tr id="id_stkdetails_set-__prefix__" >                
                {{formset.empty_form.id}}    
                {{formset.empty_form.company}}
                {{formset.empty_form.store_fr}}
                {{formset.empty_form.store_to}}
                {{formset.empty_form.vtype}}
                <td>{{ formset.empty_form.DELETE }}</td>               
                {% ifequal voucher.invtype_id 2 %}
                    <td>{{ formset.empty_form.materials }}<input class='form-control mat_label' value='' /></td>
                {% endifequal %}
                {% ifequal voucher.invtype_id 3 %}
                    <td>{{ formset.empty_form.packaging }}<input class='form-control pkg_label' value='' /></td>
                {% endifequal %}
                {% ifequal voucher.invtype_id 4 %}
                    <td>{{ formset.empty_form.semifinishedgoods }}<input class='form-control sem_label' value='' /></td>
                {% endifequal %}   
                {% ifequal voucher.invtype_id 1 %}
                    <td>{{ formset.empty_form.product }}<input class='form-control product_label' value='' /></td>
                {% endifequal %}  
                <td>{{formset.empty_form.batch_no}}</td>  
                <td>{{formset.empty_form.qty}}</td> 
                <td><select name="stkdetails_set-__prefix__-unit" id="id_stkdetails_set-__prefix__-unit" class="form-control">
                    <option value="" selected="selected">---------</option>
                    {% for u in units %}
                        <option value="{{u.0}}">{{u.1}}</option>
                    {% endfor %}
                </select></td>                    
            </tr>
    </table>

    <div>            
        {{ subtt.1 }}明细:     
        <span><a type="button" class='btn btn-sm btn-success btn-add'>
            <span class='glyphicon glyphicon-plus'></span>{% trans '添加' %}
        </a></span>
    </div>
        <div class='table-responsive'>
        <table id='tb' class="table table-condensed table-bordered table-hover">
            <thead><tr>
                <th>{% trans '删除' %}</th>
                <th>{{voucher.invtype_name}}名称</th>
                <th>{% trans '批次' %}</th>
                <th>{% trans '数量 ' %}</th>
                <th>{% trans '单位' %}</th>     
            </tr></thead>
            <tbody>
                {% for f in formset %}
                <tr  class='p'>
                    {{f.id}}{{f.company}}{{f.store_fr}}{{f.store_to}}{{f.vtype}}                 
                    <td>{{f.DELETE}}</td>    
                    {% ifequal voucher.invtype_id 2 %}
                        <td>{{ f.materials }}<input class='form-control mat_label' value='{{f.instance.materials.name}}' /></td>
                    {% endifequal %}
                    {% ifequal voucher.invtype_id 3 %}
                        <td>{{ f.packaging }}<input class='form-control pkg_label' value='{{f.instance.packaging.name}}' /></td>
                    {% endifequal %}
                    {% ifequal voucher.invtype_id 4 %}
                        <td>{{ f.semifinishedgoods }}<input class='form-control sem_label' value='{{f.instance.semifinishedgoods.name}}' /></td>
                    {% endifequal %}
                    {% ifequal voucher.invtype_id 1 %}
                        <td>{{ f.product }}<input class='form-control product_label' value='{{f.instance.product.name}}' /></td>
                    {% endifequal %}
                    <td>{{ f.batch_no }}</td>
                    <td>{{ f.qty }}</td>  
                    <td>{{ f.unit }}</td>   
                </tr>
                {% endfor %}
            </tbody>
            {{ formset.management_form }}   
        </table>
        </div>        

{% endblock %}
