{% load i18n %}

<ol class="breadcrumb">
    <li> {% trans "库存管理" %} </li> 
    <li> {{voucher.title}} </li> 
</ol>

<script type="text/javascript">
    $(document).ready(function(){    
        var params=[];  
        $('.datepicker').datepicker({dateFormat: 'yy-mm-dd'});
        $('.datepicker').datepicker('setDate',new Date());
        setDatePickerZh();       
        $('#dt_fr').val('{{dt_fr}}');
        $('#dt_to').val('{{dt_to}}');     

        {% if obj.my %}
            $("#{{voucher.fname}}_id").val('{{obj.my.id}}');
            $("#{{voucher.fname}}_label").val('{{obj.my.name}}');            
        {% endif %}
        {% if obj.vendor %}
            $('#vendor_id').val('{{obj.vendor.id}}');
            $('#vendor_label').val('{{obj.vendor.name}}');
        {% endif %}
        {% if obj.customer %}
            $('#customer_id').val('{{obj.customer.id}}');
            $('#customer_label').val('{{obj.customer.name}}');
        {% endif %}
        
        $('#{{voucher.fname}}_label').autocomplete({
            source: function(request, response) {
                $.ajax({
                    url: '/obj/ajax/autocomplete/{{voucher.fname}}/',
                    dataType: "json",
                    data: {
                        term:$("#{{voucher.fname}}_label").val(),  //搜索栏里的内容
                        company_id:"{{request.user.company_id}}",  //额外参数
                    },
                    success: function(data) {
                        response(data);
                    }
                });
            },
            select: function (event, ui) {
                $("#{{voucher.fname}}_id").val(ui.item.value);
                $("#{{voucher.fname}}_label").val(ui.item.label);
                return false;
            }
        });
        {% if voucher.vendor_needed %}
            $('#vendor_label').autocomplete({
                source: function(request, response) {
                    $.ajax({
                        url: '/obj/ajax/autocomplete/mes_vendor/',
                        dataType: "json",
                        data: {
                            term:$("#vendor_label").val(),  //搜索栏里的内容
                            company_id:"{{request.user.company_id}}",  //额外参数
                        },
                        success: function(data) {
                            response(data);
                        }
                    });
                },
                select: function (event, ui) {
                    $("#vendor_id").val(ui.item.value);
                    $("#vendor_label").val(ui.item.label);
                    return false;
                }
            });
        {% endif %}
        {% if voucher.customer_needed %}
            $('#customer_label').autocomplete({
                source: function(request, response) {
                    $.ajax({
                        url: '/obj/ajax/autocomplete/mes_customer/',
                        dataType: "json",
                        data: {
                            term:$("#customer_label").val(),  //搜索栏里的内容
                            company_id:"{{request.user.company_id}}",  //额外参数
                        },
                        success: function(data) {
                            response(data);
                        }
                    });
                },
                select: function (event, ui) {
                    $("#customer_id").val(ui.item.value);
                    $("#customer_label").val(ui.item.label);
                    return false;
                }
            });
        {% endif %}       

        $('.btn-qry').click(function(){  
            date_fr=$('#dt_fr').val();
            date_to=$('#dt_to').val();    
            if(date_fr!=''){params.push('dt_fr='+date_fr)};
            if(date_to!=''){params.push('dt_to='+date_to)};
            var my_id,vendor_id,customer_id;
            my_id=$('#{{voucher.fname}}_id').val();
            if(!$.isEmpty(my_id)){
                params.push('{{voucher.fname}}_id='+my_id);
            };
            vendor_id=$('#vendor_id').val();
            if(!$.isEmpty(vendor_id)){
                params.push('vendor_id='+vendor_id);
            };   
            customer_id=$('#customer_id').val();
            if(!$.isEmpty(customer_id)){
                params.push('customer_id='+vendor_id);
            };   
            if(params.length==0){
                bootbox.alert("{% trans '请先输入查询条件！'%}");
            }else{
                $.RefreshContent('{{request.path}}?'+params.join('&'));
            }
        });     
       
        var table=$('#tb').DataTable({           
            dom: 'Bfrtip',
            autoWidth: false,
            buttons: [{extend:'excelHtml5',text:'导出到Excel'}],           
            language:lng,           
            data:{{data|safe}}
        });       
        
        table.columns().every( function () {                        
            var that = this;             
            $('input', this.footer() ).on( 'keyup change clear', function () {
                if ( that.search() !== this.value ) {
                    that.search( this.value ).draw();
                }
            });
        });  
        
        //var url='{{request.get_full_path|safe}}'.replace('&','____');
        $('#tb').on("click","td a",function(event){   
            event.preventDefault();                       
            $.RefreshContent($(this).attr('href')+"?previous_url="+$('#cur_url').val().replace('&','____'));  
        });     
        $(".btn-obj-add").click(function () {             
            event.preventDefault();
            $.RefreshContent($(this).attr('href')+"?previous_url="+$('#cur_url').val().replace('&','____'));        
        });
    });
</script>

<form role="form"  class="form-inline" style="margin-top:0px;">
    <input id="cur_url" value="{{request.get_full_path|safe}}" type="hidden" />
    <div class="col-sm-2 input-group">
        <span class="input-group-addon">{% trans "开始日期"%}</span>
        <input class="datepicker form-control" id="dt_fr" />
    </div>
    <div class="col-sm-2 input-group">
        <span class="input-group-addon">{% trans "结束日期"%}</span>
        <input class="datepicker form-control" id="dt_to" />
    </div>
    <div class="col-sm-3 input-group">        
        <input class="form-control" id="{{voucher.fname}}_label" placeholder='所有{{voucher.invtype_name}}' />
        <input type='hidden' id='{{voucher.fname}}_id' />
    </div>
    {% if voucher.vendor_needed %}
        <div class="col-sm-2 input-group">        
        <input class="form-control" id="vendor_label" placeholder='所有供应商' />
        <input type='hidden' id='vendor_id' />
    </div>
    {% endif %}
    {% if voucher.customer_needed %}
        <div class="col-sm-2 input-group">        
        <input class="form-control" id="customer_label" placeholder='所有代工客户' />
        <input type='hidden' id='customer_id' />
    </div>
    {% endif %}

    <div class="col-sm-1 input-group">
        <span class='input-group-btn'>
            <a type="button" class='btn btn-primary btn-qry'>
                <span class='glyphicon glyphicon-search' style='padding-right:5px;'></span>{% trans '查找' %}
            </a>
        </span>
    </div>    

    {% ifequal user.company.level_id 0 %}
        <div class="col-sm-2 input-group">        
            <input class="form-control" id="product_label"   placeholder='所有产品' />
            <input type='hidden' id='product_id' />
        </div>
        {% ifequal voucher.id 2 %}
            <div class="col-sm-2 input-group">        
                <input class="form-control" id="agent_label"   placeholder='所有经销商' />
                <input type='hidden' id='agent_id' />
            </div>
        {% endifequal %}
        {% ifequal voucher.id 1 %}
            <div class="col-sm-2 input-group">        
                <input class="form-control" id="code" placeholder='所有生产批次' />           
            </div>
        {% endifequal %}
    {% endifequal %}


    <div style="float:right;">        
        <span class='input-group'>
            <a type="button" class='btn btn-obj-add l-a create-btn' href='{{murl}}'>
                <span class='glyphicon glyphicon-plus' style='padding-right:5px;'></span>{% trans '新建' %}
            </a>
        </span>        
    </div>
</form>
<div class="clearfix"></div>
<div id="inventory-list" class="table-responsive">
    <div class='x_panel'>    
    <table id="tb" class="table table-striped table-bordered dt-responsive" width="100%" >
        <thead>
            {% for c in clmns %}
                <th>{{c}}</th>
            {% endfor %}                    
        </thead>
        <tbody></tbody>
        <tfoot>
           {% for c in clmns %}
                <th><input type="text" placeholder="{{c}}"></th>            
            {% endfor %}   
        </tfoot>
    </table>   
    </div>
</div>
