{% load i18n %}


<ol class="breadcrumb">
    <li> {% trans "在线绑定" %}</li> 
    <li> {% trans "待入库产品列表" %}</li> 
</ol>



<script type="text/javascript">
    
    $(document).ready(function(){           
        function removeHTMLTag(str) {
            str=str.toString();
            str = str.replace(/<\/?[^>]*>/g,''); //去除HTML tag
            str = str.replace(/[ | ]*\n/g,'\n'); //去除行尾空白
            //str = str.replace(/\n[\s| | ]*\r/g,'\n'); //去除多余空行
            str=str.replace(/ /ig,'');//去掉 
            return str;
        };

        $('.datepicker').datepicker({dateFormat: 'yy-mm-dd'});
        $('.datepicker').datepicker('setDate',new Date());
        setDatePickerZh();  

        $('#dt_fr').val('{{dt_fr}}');      
        $('#dt_to').val('{{dt_to}}');

        $('.btn-qry').click(function(){       
            var dt_fr=$('#dt_fr').val();
            var dt_to=$('#dt_to').val();            
            var params=[];    
            if(!$.isEmpty(dt_fr)){params.push('dt_fr='+dt_fr)};
            if(!$.isEmpty(dt_to)){params.push('dt_to='+dt_to)};            
            if(params.length==0){
                bootbox.alert("{% trans '请先输入查询条件！'%}");
            }else{
                $.RefreshContent('{{request.path}}?'+params.join('&'));
            }
        });   

        $('#tb').on("click","td a",function(event){
            var url=$(this).attr('href');
            event.preventDefault();  
            $.OpenDlg(url,'在线绑定箱标明细:',1000,700);

        });          


        var table=$('#tb').DataTable({
            //"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
            //"bLengthChange":true,
            dom: 'Bfrtip',
            autoWidth: false,
            buttons: [{extend:'excelHtml5',text:'导出到Excel'}],           
            language:lng,           
            data:{{data|safe}},
            drawCallback: function () {
                var api = this.api();      
                $('#tb tfoot tr .tt-ok').text(
                    api.column(5,{page:'current'}).data().reduce(function(a,b){
                        a=($.isNumber(removeHTMLTag(a))?parseInt(removeHTMLTag(a)):0)*1;
                        b=($.isNumber(removeHTMLTag(b))?parseInt(removeHTMLTag(b)):0)*1;                        
                        return a + b                      
                    },0)
                );
                $('#tb tfoot tr .tt-error').text(
                    api.column(6,{page:'current'}).data().reduce(function(a,b){                        
                        a=($.isNumber(removeHTMLTag(a))?parseInt(removeHTMLTag(a)):0)*1;
                        b=($.isNumber(removeHTMLTag(b))?parseInt(removeHTMLTag(b)):0)*1;                        
                        return a + b
                    },0)
                );
                $('#tb tfoot tr .qty-inbound').text(
                    api.column(7,{page:'current'}).data().reduce(function(a,b){
                        a=($.isNumber(removeHTMLTag(a))?parseInt(removeHTMLTag(a)):0)*1;
                        b=($.isNumber(removeHTMLTag(b))?parseInt(removeHTMLTag(b)):0)*1;                        
                        return a + b
                    },0)
                );     
            },              
        });    

        $('.btn-inbound').click(function(){            
            var a=[];
            $('#tb tbody tr').each(function(idx,el){                
                if($(el).find('td input[type="checkbox"]').prop("checked")){
                    a.push({
                        'product_id':$(el).find('a').attr('product_id'),
                        'store_id':$(el).find('td:nth-child(3) span').attr('store_id'),
                        'batch_no':$(el).find('td:nth-child(4) span').attr('batch_no'),
                        'period':$(el).find('td:nth-child(1) span').attr('dt'),
                        'dt':$(el).find('td:nth-child(1) span').attr('dt'),
                        'tm':$(el).find('td:nth-child(1) span').attr('tm'),    
                        //2021-11-15 同一生产批次，可能有不同质检员 
                        'qc': $(el).find('td:nth-child(5) span').attr('qc')  
                    })
                };                  
            });                 
            
            if(a.length==0){
                toastr.error('没有勾选任何要入库的数据！');                     
            }else{                
                $.ajax({
                    type:'POST',   
                    data:JSON.stringify({                        
                        'dt_fr':$('#dt_fr').val(),
                        'dt_to':$('#dt_to').val(),
                        'details':a
                    }),                    
                    url:'/inventory/online/inbound/',
                    success:function(html){ 
                        $('#data-content').html(html);                                  
                    }
                })                
            };        
        });

    });
</script>

<form role="form"  class="form-inline" style="margin-top:0px;">
    <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-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>
    <a class='btn btn-primary btn-inbound pull-right' >入库</a>
</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 nowrap" width="100%" >
        <thead>
            <tr>
            {% for c in clmns %}
                <th>{{c}}</th>
            {% endfor %}                
            </tr>            
        </thead>
        <tbody></tbody>
        <tfoot>
            <tr class='fnd'>
                {% for c in clmns %}                       
                    <th><input type="text" placeholder="{{c}}"></th>   
                {% endfor %}
            </tr>  
            <tr>             
                <th>合计：</th><th/><th/><th/><th/>
                <th class='tt-ok'/>
                <th class='tt-error'/>
                <th class='qty-inbound'/>
            </tr>
               
    </table>   
    </div>
</div>
