{% load i18n %}
<style>
    #data-content .div_x .sn, #data-content .sn-s{
        width:210px;
    }
    #data-content .div_detail .sn{
        width:100px;
    }

    #label_info{
        max-height: 500px;
        overflow-y: auto;
    }
    
    .g{
        color:green;
        font-weight:bold;
    }
    .b{
        color:black;
    }
    .r{
        color:red;
        font-weight:bold;
    }
    #stk_dev thead td{
        color:black;
    }

    #qry_code{
        padding-bottom:0px;
        margin-bottom:0px;    
    }
    #relations td .replace{
        margin-left:10px;
        font-style:italic;
        color:red;
        font-weight:bold;
    }
    #product{
        margin-bottom:20px;
    }
    
    #div_cartons{
        max-height:400px;
    }
    
    #div_boxes{
        max-height:400px;
    }


    td.unprocessed .unprocessed div {
        color:gray;
        font-style:italic;

    }


   .tooltip-inner{            

        max-width: 600px;
   }

   #bg-modal{
    z-index:10000;
   }
   




   table .cap-label{
        float:right;
        margin-top:1px;
        color:white;
        font-weight:normal;
        font-size:14px;
        background-color:black;    
   }
   
   
   table .cap-label-lv2{
        margin-left:5px;
        font-size:12px;   
   }

   
   #relations a{
        margin-left:8px;
   }
   #relations .tooltip{
    font-size:14px;
   }

</style>


<ol class="breadcrumb">
        <li> {% trans "物流查询" %}</li>
        <li> {% trans "标签查询" %}</li>
</ol>


<script type="text/javascript">
    $(document).ready(function(){

        //2022-11-24
        //var v= $('input[name="名称"]:checked').val();
        {% if default_show_related %}
            $('input[name="show_related"][value="1"]').attr('checked','true');
        {% else %}
            $('input[name="show_related"][value="0"]').attr('checked','false');
        {% endif %}

        $('#stk_dev').on('click','a',function(event){      
             event.preventDefault();
             $.OpenDlg($(this).attr('href'),$(this).attr('v')+'-明细列表',1000,700);
            
        });   

        $('#no_form').submit(function(){
            return false;
        });        
        $('#code').keydown(function (event) {
            if (event.keyCode == 13) {
                var code=$.trim($('#code').val());            
                if(code.length<12){                
                    toastr.error(
                        "{% trans '请先输入您要查询的标签号码!' %}",
                        "操作错误：",
                        {timeOut: 800, positionClass: "toast-center-center"}
                    );
                    return;
                };
                refresh(code);
            }
        });    

        $('.btn-qry').click(function(){
            var code=$.trim($('#code').val());            
            if(code.length<10){                
                toastr.error(
                    "{% trans '请先输入您要查询的标签号码!' %}",
                    "操作错误：",
                    {timeOut: 800, positionClass: "toast-center-center"}
                );
                return;
            };
            refresh(code);
        });
        function refresh(code){
            //清空数据
            $('#div_cartons tbody,#div_boxes tbody,#stk_dev tbody,#product').html('').hide();                
            $('#qry_code').html('');           

            $.ajax({
                type:'GET',                           
                url:'/inventory/get/label/infor/?code='+code+"&show_related="+$('input[name="show_related"]:checked').val(),                
                dataType:'json',
                success: function(d) {   

                    //console.log(d);

                    if(d.e!=''){                        
                        toastr.error(
                            d.e,
                            "错误：",
                            {timeOut: 800, positionClass: "toast-center-center"}
                        );
                        return ;
                    };    

                    

                    var s='';                   
                    for (var i in d.product.property){                        
                        s=s+'<span style="margin-left:30px;">'+d.product.property[i].desc_cn+':<span class="g">'+
                            d.product.property[i].value+'</span>'+d.product.property[i].unit+'</span>';
                    };       
                    
                    $('#product').html(
                        '<div class="row">'+
                            '<div class="col-md-3"><span>{% trans "标签号码:" %}</span><span class="g">'+d.infor.code+'</span>'+
                                 '<span> (物流码:'+d.infor.wl+')</span>'+   
                                 (d.infor.is_thirdparty?'<span style=\"margin-left:10px;font-style:italic\">第三方标签</span>':'')+
                            '</div>'+
                            '<div class="col-md-1"><span>{% trans "类型:" %}</span><span class="g">'+d.package.name+'</span></div>'+                            
                            '<div class="col-md-8">'+
                                '<span>{% trans "产品名称:" %}</span><span class="g">'+d.product.name+'</span>'+
                                '<span style="margin-left:20px;">{% trans "价格:" %}</span><span class="g">'+d.product.price+'</span>'+s+
                            '</div>'+
                        '</div>'
                    );

                    var cartons='',boxes='',stk_dev='',r,s='',cls='',sFw='';  
                    var aLbls=[];
                    var cls='',lbl='';
                    var ct;
                    if (d.relations.level==2){
                        if(d.relations.cartons.length>0){
                            cartons='<tr style="color:black;"><td colspan=2>箱标</td></tr>';   
                        }else{
                            cartons='';
                        };
                        for (var i in d.relations.cartons){
                            cls='';
                            lbl='';
                            
                            //2024-03-03
                            rep='';
                            if(d.relations.cartons[i].replace.length>0){   
                                rep='<span class="replace">标签替换:</span><span>';
                                d.relations.cartons[i].replace.forEach(function(val,index){                                    
                                    rep=rep+
                                         '<a href="#" data-toggle="tooltip" data-placcement="top" title="'+val.tm+'">'+
                                        val.code_fr+'->'+val.code_to+'</a>';   
                                });     
                                rep=rep+'</span>';
                            };     

                            ct=d.relations.cartons[i];                             
                            if (d.infor.ab_bindding==true){
                                if(ct.code_a==code || ct.code_b==code || ct.label==code || ct.code==code){
                                    cls=' class="g"';
                                    aLbls=aLbls.concat([ct.code_a,ct.code_b,ct.label,ct.code,ct.code0]); 
                                };
                                if(ct.code_b!=''){
                                    lbl=ct.code_b;
                                }else{
                                    if(ct.code_a!=''){
                                        lbl=ct.code_a;
                                    }else{
                                        lbl=ct.label;
                                    }                                    
                                }
                            }else{
                                if(ct.label==code || ct.code==code || ct.code0==code){
                                    cls=' class="g"';
                                    aLbls=aLbls.concat([ct.code_a,ct.code_b,ct.label,ct.code,ct.code0]); 
                                };
                                lbl=ct.label;                            
                            };     
                            cartons=cartons+'<tr>'+
                                '<td style="width:30%;">'+(1+parseInt(i))+'</td>'+
                                '<td><span '+cls+'>'+lbl+'</span>'+    
                                    //2024-03-04
                                    rep+                                    
                                '</td></tr>';       
                        };

                        r=d.relations.children;
                        if(r.length>0){
                                boxes=boxes+'<tr style="color:black;"><td colspan=2>瓶标</td></tr>';
                        };      
                        var cls_cur='';                        
                        for (var j in r){                               
                            s="";
                            cls_cur='';
                            lbl='';                            

                            ct=r[j];
                            if(r[j].is_bottlecap){
                                if(! r[j].complete){                              
                                    cls='unprocessed';
                                };                                
                            }else{
                                if(! r[j].bottlecap.complete){                                   
                                    cls='unprocessed';
                                };                            
                            };                           

                            if(ct.code_a==code || ct.code_b==code || ct.label==code || ct.code0==code || ct.code==code){
                                cls_cur=' class="g"';
                                aLbls=aLbls.concat([ct.code_a,ct.code_b,ct.label,ct.code,ct.code0]);   
                            };
                            if (d.infor.ab_bindding==true){                                
                                if(ct.code_b!=''){
                                    lbl=ct.code_b;
                                }else{
                                    if(ct.code_a!=''){
                                        lbl=ct.code_a;
                                    }else{
                                        lbl=ct.label;
                                    }                                    
                                }
                            }else{                                
                                lbl=ct.label;                            
                            };                                 

                            if(r[j].bottlecap.code!=''){
                                //s='<span class="'+cls+'">'+
                                //    '<span title="瓶盖物流码:'+r[j].bottlecap.code+'" data-placement="left" data-toggle="tooltip"></span>'+
                                //'</span>';
                                //2023-09-21
                                s='<span class="label label-default cap-label">'+'瓶盖物流码:'+r[j].bottlecap.code+'</span>';




                            };                            
                            if(r[j].fw!=''){
                                sFw='<span>'+
                                    '<span title="防伪码:'+r[j].fw+'" data-placement="left" data-toggle="tooltip"></span>'+
                                '</span>';
                            };    
                            
                            //2024-03-03
                            rep='';
                            if(ct.replace.length>0){
                                rep='<span class="replace">标签替换: </span><span>';
                                ct.replace.forEach(function(val,index){    
                                    rep=rep+
                                        '<a href="#" data-toggle="tooltip" data-placcement="top" title="'+val.tm+'">'+
                                        val.code_fr+'->'+val.code_to+'</a>';
                                });
                                rep=rep+'</span>';
                            };

                            boxes=boxes+'<tr><td style="width:30%;">'+(1+parseInt(j))+s+sFw+'</td>'+'<td class="'+cls+'">'+
                                '<span>'+                                    
                                    '<span '+cls_cur+'>'+lbl+'</span>'+
                                '</span>'+rep+                                                                        
                                '</td></tr>';
                        };  

                        
                    } else {
                        for (var i in d.relations.cartons){   
                            cls='';
                            lbl='';

                            
                            ct=d.relations.cartons[i];
                            if(ct.code_a==code || ct.code_b==code || ct.label==code || ct.code0==code || ct.code==code){
                                cls_cur=' class="g"';
                                aLbls=aLbls.concat([ct.code_a,ct.code_b,ct.label,ct.code,ct.code0]);   
                            };
                            if (d.infor.ab_bindding==true){                                
                                if(ct.code_b!=''){
                                    lbl=ct.code_b;
                                }else{
                                    if(ct.code_a!=''){
                                        lbl=ct.code_a;
                                    }else{
                                        lbl=ct.label;
                                    }                                    
                                }
                            }else{                                
                                lbl=ct.label;                            
                            };   

                            //2024-03-03
                            rep='';
                            if(ct.replace.length>0){   
                                rep='<span class="replace">标签替换:</span><span>';
                                ct.replace.forEach(function(val,index){                                    
                                    rep=rep+
                                         '<a href="#" data-toggle="tooltip" data-placcement="top" title="'+val.tm+'">'+
                                        val.code_fr+'->'+val.code_to+'</a>';   
                                });     
                                rep=rep+'</span>';
                            }; 

                            cartons=cartons+'<tr><td style="width:5%;"><span>箱标</span></td>'+
                                '<td><span'+cls+'>'+lbl+'</span>'+rep                                                            
                                '</td></tr>';


                        };
                        for (var j in d.relations.children){
                            r=d.relations.children[j];
                            cls='';
                            lbl='';
                            ct=r;
                            if(ct.code_a==code || ct.code_b==code || ct.label==code || ct.code0==code || ct.code==code){
                                cls_cur=' class="g"';
                                aLbls=aLbls.concat([ct.code_a,ct.code_b,ct.label,ct.code,ct.code0]);   
                            };
                            //if (d.infor.ab_bindding==true){                                
                                if(ct.code_b!=''){
                                    lbl=ct.code_b;
                                }else{
                                    if(ct.code_a!=''){
                                        lbl=ct.code_a;
                                    }else{
                                        lbl=ct.label;
                                    }                                    
                                }
                            //}else{                                
                            //    lbl=ct.label;                            
                            //};  

                            //2024-03-03
                            rep='';
                            if(ct.replace.length>0){   
                                rep='<span class="replace">标签替换:</span><span>';
                                ct.replace.forEach(function(val,index){                                    
                                    rep=rep+
                                         '<a href="#" data-toggle="tooltip" data-placcement="top" title="'+val.tm+'">'+
                                        val.code_fr+'->'+val.code_to+'</a>';   
                                });     
                                rep=rep+'</span>';
                            }; 
                            boxes=boxes+'<tr><td style="width:5%;"><span>'+(1+parseInt(j))+'</span></td><td style="width:510;"><span>盒标</span></td>'+
                                '<td colspan=2>'+
                                    '<span '+((d.infor.ab_bindding?r.code_b:r.label)==code?' class="g"':'')+'>'+
                                    (r.code_b?r.code_b:r.label)+'</span>'+ 
                                    (r.bottlecap.code==''?'':'<span class="label label-default cap-label-lv2">瓶盖物流码:'+r.bottlecap.code+'</span>')+                                    
                                    rep+                           
                                '</td></tr>';
                            for (var k in r.children){ 
                                cls='';
                                lbl='';
                                ct=r.children[k];
                                if(ct.code_a==code || ct.code_b==code || ct.label==code || ct.code0==code || ct.code==code){
                                    cls_cur=' class="g"'
                                    aLbls=aLbls.concat([ct.code_a,ct.code_b,ct.label,ct.code,ct.code0]);   
                                };
                                //if (d.infor.ab_bindding==true){                                
                                    if(ct.code_b!=''){
                                        lbl=ct.code_b;
                                    }else{
                                        if(ct.code_a!=''){
                                            lbl=ct.code_a;
                                        }else{
                                            lbl=ct.label;
                                        }                                    
                                    }
                                //}else{                                
                                //  lbl=ct.label;                            
                                //};  

                                //2024-03-03
                                rep='';
                                if(ct.replace.length>0){   
                                    rep='<span class="replace">标签替换:</span><span>';
                                    ct.replace.forEach(function(val,index){                                    
                                        rep=rep+
                                         '<a href="#" data-toggle="tooltip" data-placcement="top" title="'+val.tm+'">'+
                                        val.code_fr+'->'+val.code_to+'</a>';   
                                    });     
                                    rep=rep+'</span>';
                                }; 
                                boxes=boxes+'<tr><td></td><td style="width:5%;">'+
                                    '</td><td colspan=2>瓶标 <span '+cls+'>'+lbl+'</span>'+
                                    rep+
                                    '</span></td></tr>';
                            }
                        };
                                                                               
                    };

                    $('#div_cartons table tbody').append(cartons);   
                    $('#div_boxes table tbody').html(boxes);                           
                    if (d.stock_development.length>0){
                        stk_dev='<tr style="color:black;"><td>时间</td><td>单据类型</td><td>单据编号</td><td>数量</td><td>公司名称</td><td>操作员</td>'+                        
                            '<td>标签号码</td><td>类型</td><td>产品名称</td><td>仓库名称</td><td>客户名称</td></tr>';
                    };

                    for(var i in d.stock_development){                        
                        cls='';  
                        r=d.stock_development[i];
                        s=r.customer;                 
                        

                        //2023-09-25
                        if(r.vtype_id==3){
                            store=r.store_to;
                        }else{
                            store=r.store;
                        };   
                        
                        

                        if($.inArray(r.label_code, aLbls)>-1){
                            cls=' class="g"';
                        };      
                        stk_dev=stk_dev+'<tr><td>'+r.tm+'</td><td>'+r.voucher+'</td>'+
                            '<td><a href="/inventory/batch/details/'+r.batch_id+'/" v="'+r.voucher+'">'+r.bill_no+'</a></td>'+
                            '<td>'+r.qty+'</td><td>'+r.company+'</td><td>'+r.user+'</td>'+                        
                            '<td '+cls+'>'+r.label_code+'</td><td>'+r.label_pkg+'</td><td>'+r.product+'</td><td>'+store+'</td><td>'+s+'</td></tr>'; 

                    };     

                    $('#stk_dev tbody').html(stk_dev);


                    //2024-04-23 酒鬼酒要访问汉得分销终端销售信息
                    var s='';  
                    for(var n in d.dev3rd){
                        var r=d.dev3rd[n];                        
                        s=s+'<tr><td>'+r.tm+'</td><td>'+r.vtype+'</td><td></td>'+
                            '<td></td><td>'+r.company+'</td><td></td>'+                        
                            '<td>'+r.label+'</td><td>'+r.package.name+'</td><td></td><td></td><td>'+r.customer+'</td></tr>';                        
                    };
                    $('#stk_dev tbody').append(s);
                    

                    $('#div_cartons tbody,#div_boxes tbody,#stk_dev tbody,#product').show();   

                    //$('#div_boxes [data-toggle="tooltip"]').tooltip('show');                    
                    //$('#div_boxes [data-toggle="tooltip"]').unbind('mouseenter').unbind('mouseleave'); 

                    //2024-03-03
                    $("[data-toggle='tooltip']").tooltip();
                }
            });
        }
    });
</script>
 

<div class='x_panel'>
<br/>
<div class='clearfix'></div>
<div class='tt'>
    <form role="form" id='no_form' class="form-inline">
    <!--
    <div class='input-group'>
        <input id='code' class='datepicker form-control' placeholder="{% trans '请输入标签号码，点击查询按钮或按回车键查询' %}"/>
        <span class='input-group-btn'>
            <button  type="button" class='btn btn-primary btn-qry'>
                <span class='glyphicon glyphicon-search' style='padding-right:5px;'></span>{% trans "查询" %}
            </button>
        </span>
    </div>
    -->
    <div class='input-group col-sm-7'>
        <input id='code' class='datepicker form-control' placeholder="{% trans '请输入标签号码，点击查询按钮或按回车键查询' %}"/>        
    </div>
    <div class='input-group col-sm-4'>
        <span style='margin-left:30px;margin-right:5px;'>关联标物流信息：</span>
        <label style='margin-right:5px;'><input name="show_related"   type="radio"  value="1"/>显示</label>
        <label><input name="show_related" type="radio"  value="0" />不显示</label>

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

    </form>
</div>

<div class='clearfix'></div>
<div id='qry_code'></div>
<div id='lbl_replace'></div>


<div id="product" style='display:none'></div>
<div id="relations" >
    <div id='div_cartons' class='table-responsive'>
        <table class='table table-bordered table-condensed table-hover'><tbody></tbody></table>
    </div>
     <div id='div_boxes' class='table-responsive'>
        <table class='table table-bordered table-condensed table-hover'><tbody></tbody></table>
    </div>
</div>
<div id="stk_dev" class="table-responsive">
    <table class='table table-bordered table-condensed table-hover'>
        <thead></thead>
        <tbody></tbody>
    </table>
</div>
</div>







