{% load i18n %}


<style> 
    #tb .btn{
        margin-top:0px;
        margin-bottom:0px;
    }
    form div{
        margin-top:5px;
    }
    
</style>



<script type="text/javascript">
    $(document).ready(function(){ 
        var table;
        var clmns=[                 
            {'title':'inform_id', 'data':'inform_id'},  
            {'title':'单据日期', 'data':'tm_inform'},               
            {'title':'客户名称', 'data':'company'},
            {'title':'产品名', 'data':'product'},  
            {'title':'码类型', 'data':'codetype'},
            {'title':'数量', 'data':'qty'},                                
            {'title':'码文件', 'data':'zipfile'},     
            {'title':'大小', 'data':'sz'},
            {'title':'最后下载', 'data':'dt_download'},            
            {'title':'已上传', 'data':'qty_upload'},               
        ];
        var header='',footer='';
        for (var i in clmns){
            header=header+ '<th>'+clmns[i].title+'</th>';
            footer=footer+ '<th><input type="text" placeholder="'+clmns[i].title+'"></th> ';        
        };
        $('#tb heaer').html(header);
        $('#tb footer').html(footer);

        function tbRefresh(){
            var cid=$('#sel-company').val();
            $.ajax({
                type:'GET',                           
                url:"/bottlecap/ajax/zip/list/?company_id="+cid,                
                dataType:'json',                
                success: function(d) {
                    if (d.e==''){
                        table=$('#tb').DataTable({
                            dom: 'Bfrtip',
                            autoWidth: true,
                            pageLength: 10,
                            buttons: [{extend:'excelHtml5',text: '导出到Excel'}],   
                            language:lng,	
                            columns:clmns,			
                            data:d.data,  
                            destroy:true,
                            createdRow: function( nRow, aData, iDataIndex ) {
                                var cpt='';
                                if(aData.dt_download==''){
                                    cpt='下载'
                                }else{
                                    cpt='再次下载'
                                };
                                if(aData.zipfile!=''){
                                    $('td:eq(8)', nRow).html(aData.dt_download+ 
                                        '<button class="btn btn-primary btn-xs btn-download" '+
                                        'href="/bottlecap/ajax/zip/download/?inform_id='+aData.inform_id+'">'+cpt+'</button>' 
                                    );
                                };
                            }
                        });                         
    
                        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();
                                    }
                                });
                        }); 


                        // 隐藏inform_id
                        $('#tb').DataTable().column(0).visible(false);
                        
                    }else{                        
                        toastr.error(
                            d.e,
                            "操作错误：",
                            {timeOut: 800, positionClass: "toast-center-center"}
                        );
                    }
                }
            });        
        };
        tbRefresh();
        $('.btn-refresh').click(function(){
            tbRefresh();        
        });
        

        
        $('#tb').on("click",".btn-download",function(event){              
            var download_url=$(this).attr('href');  
            var tr=$(this).closest('tr');    
            var fn=$(tr).find('td:eq(5)').text();   

            $.ajax({
                type:'GET',                           
                url:download_url,               
                data: {filename: fn},
                xhrFields: {
                    responseType: 'blob'
                },
                success: function (data) {
                    var url = window.URL.createObjectURL(data);
                    var a = document.createElement('a');
                    a.href = url;
                    a.download = fn;
                    a.click();
                    window.URL.revokeObjectURL(url);                    
                }               
            });              

        });
        

       
        
       
    });
</script>



<ol class="breadcrumb">
        <li>瓶盖码数据管理</li> 
        <li>盖码数据下载</li>            
</ol>




<div class='clear-fix'></div> 
<div class="x_panel">
    
        <div class="form-group" >
            <label class="col-sm-1 control-label ">所属企业 </label>
            <div class="col-sm-4">
                <select class='form-control' id='sel-company'>                    
                    {% for f in companys %}
                        <option value='{{f.id}}'>{{f.name}}</option>
                    {% endfor %}                    
                </select>
            </div>
            <div class="col-sm-1">
                <button class='btn btn-primary btn-refresh'>刷新</button>
            </div>            
            
        </div> 
        
    </div>

    <div  class="table-responsive">
    <table id='tb'  class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
        <thead></thead>
        <tbody></tbody>
        <tfoot></tfoot>
    </table>
    </div>
    
</div>











