{% load i18n %}


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

    #tb .btn-generate{
        margin-left:5px;
    }
</style>



<script type="text/javascript">
    $(document).ready(function(){     
        var table=$('#tb').DataTable({
            dom: 'Bfrtip',
            autoWidth: true,
            pageLength: 10,
            buttons: [{extend:'excelHtml5',text: '导出到Excel'}],   
            language:lng,	
            columns:{{clmns|safe}},			
            data:{{data|safe}},
            //如果业务通知单码数据未从内网同步到云平台，显示同步按钮;zip文件生成按钮 
            createdRow: function( nRow, aData, iDataIndex ) {   
                // 2024-11-09
                $('td:eq(15)', nRow).html(
                    aData.factory+
                    '<button class="btn btn-primary btn-xs btn-inform-edit" '+                            
                    'href="/bottlecap/inform/edit/?inform_id='+aData.inform_id+'">修改</button>' 
                );     

                if ( aData.qty_sync==0 ) {
                    $('td:eq(10)', nRow).html( 
                        '<button class="btn btn-primary btn-xs btn-sync"  '+
                            'href="/bottlecap/ajax/codes/sync/?inform_id='+aData.inform_id+'">同步</button>' 
                    );                    
                }else{                    
                    

                    if(aData.zipfile==''){                        

                        if(aData.codetype=='盖外码'){
                            $('td:eq(12)', nRow).html(
                                '<label>'+
                                '<input name="clmns'+iDataIndex+'" type="radio" value="1" checked/>1列</label>'+ 
                                '<label>'+
                                '<input name="clmns'+iDataIndex+'" type="radio" value="2" />2列</label>'+
                                '<button class="btn btn-primary btn-xs btn-generate"  '+
                                'href="/bottlecap/ajax/zip/generate/?inform_id='+aData.inform_id+'">生成</button>'
                            )
                        }else{
                            $('td:eq(12)', nRow).html(
                                '<button class="btn btn-primary btn-xs btn-generate"  '+
                                'href="/bottlecap/ajax/zip/generate/?inform_id='+aData.inform_id+'">生成</button>' 
                            )
                        }                           
                    //2024-08-19
                    }else{
                        $('td:eq(12)', nRow).html( 
                            '<button class="btn btn-primary btn-xs btn-download" '+
                            'fn="'+aData.zipfile+' "'+
                            'href="/bottlecap/ajax/zip/download/?inform_id='+aData.inform_id+'">下载</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();
                }
            });
        });    
        
        // 隐藏id
        $('#tb').DataTable().column(0).visible(false);
        //隐藏企业信息
        $('#tb').DataTable().column(13).visible(false);

        $('#tb').on("click","button",function(event){
            event.preventDefault();
            var url=$(this).attr('href');
            var act=url.split("/")[4];  
            var me=$(this);

            if (act=='generate'){
                var clmns=$(this).closest('td').find('input[type="radio"]:checked').val();   
                if(clmns=='2'){
                    url=url+"&clmns=2"
                };         
            
                $.ajax({
                    type:'GET',                           
                    url:url,                
                    dataType:'json',
                    success: function(d) {
                        if (d.e==''){
                            if(act=='sync'){
                                toastr.success(d.cnt+" 盖码数据同步完成。",
                                    {timeOut: 2000, positionClass: "toast-center-center"}            
                                );
                                $(me).hide();

                            }else{
                                toastr.success(d.cnt+" 码文件已生成,  密码: "+d.password,
                                    {timeOut: 2000, positionClass: "toast-center-center"}            
                                );     
                                $.RefreshContent('{{request.path}}'); 
                            }
                            //$.RefreshContent('{{request.path}}'); 

                        }else{                        
                            toastr.error(
                                d.e,
                                "操作错误：",
                                {timeOut: 800, positionClass: "toast-center-center"}
                            );
                        }
                    }
                });
            }else{
                var download_url=$(this).attr('href');  
                //var tr=$(this).closest('tr');    
                var fn=$(this).attr('fn');  
                $.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);                    
                    }               
                });              
            }
            

        });   

        //内网取相关业务通知单信息
        $('.btn-inform-sync').click(function(){
            $.ajax({
                type:'GET',                           
                url:"/bottlecap/ajax/inform/sync/",                
                dataType:'json',
                success: function(d) {
                    if (d.e==''){
                       
                        toastr.success(d.cnt+" 业务通知单已同步。",
                                {timeOut: 2000, positionClass: "toast-center-center"}            
                        );                            
                        
                        $.RefreshContent('{{request.path}}'); 
                    }else{                        
                        toastr.error(
                            d.e,
                            "操作错误：",
                            {timeOut: 800, positionClass: "toast-center-center"}
                        );
                    }
                }
            });                      
            
        
        });


        //2024-11-09 修改业务单加工企业
        //$('.btn-inform-edit').click(function(event){
        $('#tb').on("click",'.btn-inform-edit',function(event){
            let url=$(this).attr('href');  
            $.ajax({
                type:'GET',
                processData: false,
                url:url,
                success:function(data){
                    $('#inform_id').val(data.id);
                    $('#inform_dt').text(data.dt);
                    $('#inform_code').text(data.code);
                    $('#product').text(data.product);
                    let html='';
                    for (var i in data.factories){
                        html=html+"<option value='"+data.factories[i].id+"'>"+data.factories[i].name+"</option>"                        
                    };
                    $('#sel-factories').html(html);
                    $("#dlg-inform").modal('show');  
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert('连接服务器失败');
                    return;
                }
            });            
        });

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

        $('.add-fcty').click(function(){
            var id=$('#sel-factory').val();
            var label=$('#sel-factory').find("option:selected").text();           

            if(id==''){
                toastr.error("请先选择加工企业！", "操作错误：",
                    {timeOut: 800, positionClass: "toast-center-center"}
                );
                return;
            }else{
                var e=false;
                $("#sel-factories 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-factories').append("<option value='"+id+"'>"+label+"</option>");
                }
            };
        });

        $('.btn-save').click(function(){
            var a=[];
            $("#sel-factories option").each(function (){
                a.push({
                    "id":$(this).val(),
                    "name":$(this).text()
                });
            });           
            
            
            $.ajax({
                type:'POST',
                data:JSON.stringify({
                    "inform_id":$("#inform_id").val(),
                    'factories':a
                }),                
                url:"/bottlecap/inform/edit/",
                success:function(data){
                    $("#dlg-inform").modal('hide');
                    $.RefreshContent('/bottlecap/inform/list/');
                
                }          
            })
        });

    });
</script>



<ol class="breadcrumb">
        <li>瓶盖码数据</li>
        <li>业务单信息</li>     
</ol>




<div class='clear-fix'></div>
   
   
       
<div class="x_panel">
    <button class='btn btn-primary btn-inform-sync'>同步业务通知单信息</button>

    <div  class="table-responsive"><table id='tb'  class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
        <thead>
            {% for c in clmns %}
                <th>{{c.title}}</th>
            {% endfor %}
                
        </thead>
        <tbody></tbody>
        <tfoot>
            {% for c in clmns %}
                <th><input type="text" placeholder="{{c.title}}"></th>            
            {% endfor %}            
        </tfoot>
    </table></div> 
</div>



<div class="modal fade" tabindex="-1" role="dialog" id="dlg-inform">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">修改业务单</h4>
            </div>
            <div class="modal-body" >



                <div class='row' style='width:100%'>
                    <div class="row">
                        <input type='hidden' id='inform_id' />

                        <label class="col-sm-2 control-label">业务单信息</label>
                        <div class='col-sm-10'>
                            <span id='inform_code'></span>
                            <span id='inform_dt'></span>
                            <span id='product'></span>            
                        </div>
                    </div>

                    
                    
                    <div class="form-group ">
                        <label class="col-sm-2 control-label">加工企业</label>
                        <div class='col-sm-10'>
                            <div class="input-group">
                                <select class="form-control" id="sel-factory">
                                    {% for f in factories %}
                                        <option value='{{f.id}}'>{{f.name}}</option>                    
                                    {% endfor %}            
                                </select>
                                <span class="input-group-btn"><button type="button" class="btn btn-success add-fcty">
                                    <span class='glyphicon glyphicon-arrow-down'></span></button>
                                </span>
                            </div>
                        </div>
                    </div>   
                    <div class="form-group ">
                        <div class='col-sm-10 col-md-offset-2'>
                            <select class="select2_multiple form-control" id='sel-factories' multiple="multiple">
                                                              
                            </select>        
                        </div>
                    </div>            
                </div>                   
                    
                            
                
            </div>

           
            
            <div class="modal-footer">
                <button type="button" class="btn btn-default btn-primary btn-save"
                    data-dismiss="modal">保存</button>
                <button type="button" class="btn btn-default"
                    data-dismiss="modal">取消</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

