{% load i18n %}


<style>
    #infor span.g{
        color:green;
        display:inline-block;        
        min-width:20px;
        margin-right:10px;
    }
    #tb{
     'margin-top:20px;
    }

</style>


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

        var company_id={{request.user.ancestor_id}};        
        //先只限定酒鬼酒
        company_id='1';



        $('.btn-add').click(function(){
            var data={
                tb_lbl: $('#tb_lbl').val(),
                cap_label: $('#cap_label').val(),
                cap_code: $('#cap_code').val(),
                bottle: $('#lbl_bottle').val(),
                product_id: $('#product_id').val(),
                store_id: $('#store_id').val(),
                pk_id: $('#label_pk_id').val(),
                pk_code: $('#label_pk_code').val(),
                product_name: $('#product').text(),
                line: $('#ln').text(),
                bottle_cnt: $('#bottle_cnt').text(),
                batch_no: $('#batch_no').text(),
                qc: $('#qc').text(),
                tm:$('#tm').val(),
            };
            /*
            g=r['cap']
            c=r['code']
            lbl=r['bottle']
            product=r['product_name']
            pid=str(r['product_id'])
            ln=r['line']
            pk_id=r['pk_id']
            pk_code=r['pk_code']
            cnt=r['bottle_cnt']
            batch_no=r['batch_no']
            qc=r['qc']
            store_id=str(r['store_id'])
            */

            $.ajax({
                type:'POST',
                data:JSON.stringify(data),
                url:'/mes/bottlecap/makeup/'+company_id+'/binding/',                
                dataType:'json',
                success: function(d) {
                    if(d.e){
                        toastr.error(
                            d.e,
                            "操作错误：",
                            {timeOut: 800, positionClass: "toast-center-center"}
                        );                        
                    }else{
                        toastr.success(
                            "数据已写入",
                            "操作成功：",
                            {timeOut: 800, positionClass: "toast-center-center"}
                        );                        
                    }
                }
            });           
        });

        
        
        $('.btn-qry').click(function(){
            //所有隐藏input 置空
            $('#infor input[type="hidden"]').val('');
            //相关span 置空
            $('#infor span.g').text('');
            var b=$('#lbl_bottle').val();
            var l=$('#lbl_cap').val();
            var x=$('#lbl_carton').val();
            if (b!='' && l!='' && x!=''){
                $.ajax({
                    type:'GET',                           
                    url:'/mes/bottlecap/makeup/'+company_id+'/retrieve/?label='+l+'&bottle='+b+'&carton='+x,
                    dataType:'json',
                    success: function(d) {  
                        if(d.e!=''){
                            toastr.error(
                                d.e,
                                "操作错误：",
                                {timeOut: 800, positionClass: "toast-center-center"}
                            );                           
                        }else{
                            $('#tb_lbl').val(d.tb_lbl);
                            $('#pk_id').val(d.label_pk.id);
                            $('#product_id').val(d.prd.product_id);
                            $('#store_id').val(d.prd.store.id);
                            $('#tm').val(d.prd.tm);
                            $('#tb_lbl').val(d.tb_lbl);
                            $('#label_pk_id').val(d.label_pk.id);
                            $('#label_pk_code').val(d.label_pk.code);
                            $('#cap_label').val(d.cap_label);
                            $('#cap_code').val(d.cap_code);                            
                    
                            $('#cap_type').text(d.cap_type);
                            $('#product').text(d.prd.product_name);
                            $('#bottle_cnt').text(d.prd.bottle_cnt);
                            $('#ln').text(d.prd.line);
                            $('#batch_no').text(d.prd.batch_no);
                            $('#qc').text(d.prd.qc);
                            $('#store').text(d.prd.store.name);

                            //绑定
                            var html='';
                            var b;
                            html='<tr><td>箱标</td><td>'+d.label_pk.code+'</td><td colspan=2>'+d.cartons+'</td></tr>';
                            for(var i in d.bottles){
                                b=d.bottles[i];
                                html=html+'<tr>'+
                                    '<td>'+b.code+'</td>'+
                                    '<td>'+b.label+'</td>'+
                                    '<td>'+b.bottle+'</td>'+
                                    '<td>'+b.tm+'</td>'+
                                    '</tr>';                           
                            };
                            $('#tb tbody').html(html);                            

                        };                        
                        return false;
                    }      
                });
            }else{
                toastr.error(
                    "盖码和瓶标不能为空！",
                    "操作错误：",
                    {timeOut: 800, positionClass: "toast-center-center"}
                );       
            
            }
        });

    });
</script>


<ol class="breadcrumb">
        <li>{% trans "瓶盖码绑定" %}</li>
        <li>{% trans "数据补录" %}</li>     
</ol>
<form role="form"  class="form-inline">

    <div class="col-sm-4 input-group">
        <span class="input-group-addon">{% trans "瓶标："%}</span>
        <input class="form-control" id="lbl_bottle" />
    </div>

    <div class="col-sm-4 input-group">
        <span class="input-group-addon">{% trans "盖码："%}</span>
        <input class="form-control" id="lbl_cap"/>
    </div>
    <div class="col-sm-4 input-group">
        <span class="input-group-addon">{% trans "箱码："%}</span>
        <input class="form-control" id="lbl_carton"/>
    </div>
    

    <div class="col-sm-2 input-group pull-right">
        <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>  

</form>

<div class="x_panel">

            <input type='hidden' id='tb_lbl' />        
            <input type='hidden' id='product_id' />
            <input type='hidden' id='store_id' />
            <input type='hidden' id='label_pk_id' />
            <input type='hidden' id='label_pk_code' />
            <input type='hidden' id='tb_lbl' />
            <input type='hidden' id='cap_code' />
            <input type='hidden' id='cap_label' />
            <input type='hidden' id='tm' />
       
        <div id='infor' >
            <span>瓶盖码类型:</span><span class='g' id='cap_type'>未知</span>
            <span>产品名称:</span><span class='g' id='product'>未知</span>
            <span>每箱:</span><span class='g' id='bottle_cnt'>未知</span>瓶
            <span>产线:</span><span class='g' id='ln'>未知</span>
            <span>生产批次:</span><span class='g' id='batch_no'>未知</span>
            <span>质检员:</span><span class='g' id='qc'>未知</span>
            <span>仓库:</span><span class='g' id='store'>未知</span>

             

        </div>   

        <div style='margin-top:20px;'>
            <span class='input-group-btn'>
                <a type='button' class='btn btn-primary btn-add'>
                    <span class='glyphicon glyphicon-arrow-down' style='padding-right:5px;'>添加到当前箱</span>
                </a> 
            </span> 
        </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>

