{% 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;
   }




</style>


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


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


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

        });



        $('#code').keydown(function (event) {
            if (event.keyCode == 13) {
                var code=$.trim($('#code').val());
                if(code.length>=12){
                    refresh(code);
                }
            }
        });

        $('.btn-qry').click(function(){
            var code=$.trim($('#code').val());
            if(code.length<12){
                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,
                dataType:'json',
                success: function(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>'+
                                 (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>'+
                        {% ifequal user.company.id 19267 %}
                            '<div class="col-md-8"><span>{% trans "批次信息:" %}</span><span class="g">'+d.batch_no+'</span></div>'+
                        {% endifequal %}

                        '</div>' +
                        '<div class="row">'+
                            '<div class="col-md-12">'+
                                '<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='',stacks='';
                    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>{{ package_3 }}</td></tr>';
                        }else{
                            cartons='';
                        };
                        if (d.infor.stack.details.length>0){
                            stacks=stacks+'<tr style="color:black;"><td colspan=2>垛</td></tr>';
                            for (var i in d.infor.stack.details){
                                stacks=stacks+'<tr>'+
                                '<td style="width:30%;">'+(1+parseInt(i))+'</td>'+
                                '<td> '+ d.infor.stack.details[i]+' </td></tr>';
                            }
                        }
                        for (var i in d.relations.cartons){
                            cls='';
                            lbl='';
                            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>'+
                                    (d.relations.cartons[i].dt_replace==''?'':'<span class="replace">此标签有替换记录'+
                                    d.relations.cartons[i].dt_replace+" "+d.relations.cartons[i].code0+'->'+d.relations.cartons[i].code+'</span>')+
                                '</td></tr>';
                        };

                        r=d.relations.children;
                        if(r.length>0){
                                boxes=boxes+'<tr style="color:black;"><td colspan=2>{{ package_1 }}</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){
                            // 2022-08-29
                            if(ct.color=='red'){
                                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>';
                            };
                            if(r[j].fw!=''){
                                sFw='<span>'+
                                    '<span title="防伪码:'+r[j].fw+'" data-placement="left" data-toggle="tooltip"></span>'+
                                '</span>';
                            };
                            boxes=boxes+'<tr><td style="width:30%;">'+(1+parseInt(j))+'</td>'+'<td class="'+cls+'">'+
                                '<span>'+
                                    s+sFw+
                                    '<span '+cls_cur+'>'+lbl+'</span>'+
                                '</span>'+
                                    (ct.dt_replace==''?'':'<span class="replace">此标签有替换记录 '+
                                        ct.dt_replace+' '+ct.code0+' -> '+ct.code+'</span>')+
                                '</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;
                            };
                            cartons=cartons+'<tr><td style="width:5%;">{{ package_3 }}</td>'+
                                '<td><span'+cls+'>'+lbl+'</span>'+
                                (d.relations.cartons[i].dt_replace==''?'':'<span class="replace">此标签有替换记录 '+
                                    d.relations.cartons[i].dt_replace+" "+d.relations.cartons[i].code0+'->'+d.relations.cartons[i].code+'</span>')+
                                '</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){
                            // 2022-08-29
                            if(ct.color=='red'){
                                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;
                            };
                            boxes=boxes+'<tr><td style="width:5%;">'+(1+parseInt(j))+'</td><td style="width:5%;">{{ package_2 }}</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.dt_replace==''?'':'<span class="replace"> 此标签有替换记录 '+
                                        r.dt_replace+' '+r.code0+'->'+r.code+'</span>')+
                                '</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;
                                };
                                boxes=boxes+'<tr><td></td><td style="width:5%;">'+(1+parseInt(k))+
                                    '</td><td colspan=2>{{ package_1 }} <span '+cls+'>'+lbl+'</span>'+
                                    (ct.dt_replace==''?'':'<span class="replace">此标签有替换记录 '+
                                        ct.dt_replace+' '+r.children.code0+'->'+r.children.code)+
                                    '</span></td></tr>';
                            }
                        };

                    };

                    $('#div_stacks table tbody').html(stacks);
                    $('#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>';
                    };

                    //取标签明细的地址
                    var url='';
                    var a='';
                    for(var i in d.stock_development){
                        cls='';
                        r=d.stock_development[i];
                        s=r.customer;

                        //if(r.vtype_id==3){
                        //    store=r.store_to;
                        //}else{
                        //    store=r.store;
                        //};

                        store=r.store;
                        if($.inArray(r.label_code, aLbls)>-1){
                            cls=' class="g"';
                        };


                        //2022-08-17 是否需要取本地表数据
                        if ($.isNumber(r.batch_id)){
                            url="/inventory/batch/details/"+r.batch_id+"/";
                            a='<a href="'+url+'">'+r.bill_no+'</a>';
                        }else{
                            if(r.vtype_id==2){
                                url="/inventory/batch/details/0/?code="+r.bill_no+"&vtype_id="+r.vtype_id+"&label_code="+r.label_code;
                                a='<a href="'+url+'" v="'+r.voucher+'">'+r.bill_no+'</a>';
                            }else{
                                a=r.bill_no;
                            };
                        };
                        stk_dev=stk_dev+'<tr><td>'+r.tm+'</td><td>'+r.voucher+'</td>'+
                            '<td>'+a+'</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><td>'+r.remark+'</td></tr>';

                    };

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

                    $('#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');
                }
            });
        }
    });
</script>


<div class='x_panel'>
<br/>

<div class='tt'>
    <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>

<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_stacks' class='table-responsive'>
        <table class='table table-bordered table-condensed table-hover'><tbody></tbody></table>
    </div>
    <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>






