var pageCurr;
var printMatCodeNos = [];
var ordernoo = "123";
layui.config({
    base: baseUrl + "/static/layui/lay/modules/"
}).use(['table','laydate', 'form', 'admin', 'layer'], function(){
    var table = layui.table;
    var $ = layui.jquery;
    var layer = layui.layer;
    var layDate = layui.laydate;
    var form = layui.form;
    var admin = layui.admin;
    // 数据渲染
    tableIns = table.render({
        elem: '#saasLog',
        headers: {token: localStorage.getItem('token')},
        url: baseUrl+'/ManPakOut/detailed/auth',
        page: true,
        limit: 15,
        limits: [15, 30, 50, 100, 200, 500],
        // toolbar: '#toolbar',
        cellMinWidth: 50,
        height: 'full-120',
        cols: [[
            // {type: 'checkbox'}
            {type: 'numbers'}
            ,{field: 'wrkNo', align: 'center',title: 'id', hide:true}
            ,{field: 'docNum',title: '拣货单号' }
            ,{align: 'center', title: '二维码', toolbar: '#operate', width: 200 , hide:true}
            ,{align: 'center', title: '明细', toolbar: '#tbLook', minWidth: 160, width: 160}
            ,{field: 'payment$', align: 'center',title: '状态'}
            // ,{field: 'owner$', align: 'center',title: '货主'}
            // ,{field: 'payment$', align: 'center',title: '货物形态'}
            // ,{field: 'custName', align: 'center',title: '客户名称', width: 160}
            ,{field: 'createTime$', align: 'center',title: '操作时间'}
            ,{align: 'center', title: '操作', toolbar: '#operate'}
        ]],
        request: {
            pageName: 'curr',
            pageSize: 'limit'
        },
        parseData: function (res) {
            console.log(res);
            return {
                'code': res.code,
                'msg': res.msg,
                'count': res.data.total,
                'data': res.data.records
            }
        },
        response: {
            statusCode: 200
        },
        done: function(res, curr, count) {
            if (res.code === 403) {
                top.location.href = baseUrl+"/";
            }
            pageCurr=curr;
            limit();
            var index = -1;
            $("[data-field='status$']").children().each(function () {
                index++;
                if ($(this).text() === '未出库') {
                    $('tr').eq(index).css("color", 'red');
                }
            });
        }
    });
    // 监听排序事件
    table.on('sort(saasLog)', function (obj) {
        var searchData = {};
        $.each($('#search-box [name]').serializeArray(), function() {
            searchData[this.name] = this.value;
        });
        searchData['orderByField'] = obj.field;
        searchData['orderByType'] = obj.type;
        tableIns.reload({
            where: searchData,
            page: {curr: 1}
        });
    });
    // 监听行工具事件
    table.on('tool(saasLog)', function(obj){
        var data = obj.data;
        switch (obj.event) {
            case "btnPrint":
                btnPrint(data.wrkNo, data.docNum, 4);
                break;
            case "wrkTrace":
                btnPrint(data.wrkNo, data.docNum, 4);
                break;
            case "btnPrint2":
                btnPrint2(data.wrkNo, data.docNum, 4);
                break;
            case "look":
                var $a = $(obj.tr).find('a[lay-event="look"]');
                var offset = $a.offset();
                var top = offset.top;
                var left = offset.left;
                layer.open({
                    type: 1,
                    title: false,
                    area: '1200px',
                    offset: [top + 'px', (left - 450 + $a.outerWidth()) + 'px'],
                    shade: .01,
                    shadeClose: true,
                    fixed: false,
                    content: '
',
                    success: function (layero) {
                        table.render({
                            id:'lookSSXMTable',
                            elem: '#lookSSXMTable',
                            headers: {token: localStorage.getItem('token')},
                            url: baseUrl+'/ManPakOut/list/auth',
                            where: {
                                doc_num: data.docNum,
                                uuid: data.uuid
                            },
                            page: true,
                            cellMinWidth: 100,
                            cols: [[
                                {type: 'numbers'}
                                ,{field: 'id', align: 'center',title: 'id', hide:true}
                                ,{field: 'docNum', align: 'center',title: '拣货单号'}
                                //,{field: 'uuid', align: 'center',title: 'uuid'}
                                ,{field: 'locNo', align: 'center',title: '库位'}
                                ,{field: 'matnr', align: 'center',title: '存货编码'}
                                ,{field: 'maktx', align: 'center',title: '存货名称'}
                                ,{field: 'anfme', align: 'center',title: '数量'}
                                ,{field: 'containerCode', align: 'center',title: '料箱码'}
                                ,{field: 'csocode', align: 'center',title: '销售订单号'}
                                ,{field: 'isoseq', align: 'center',title: '销售订单行号'}
                                //,{field: 'owner$', align: 'center',title: '货主'}
                                //,{field: 'payment$', align: 'center',title: '货物形态'}
                                //,{field: 'owner', align: 'center',title: '货主', hide:true}
                                //,{field: 'payment', align: 'center',title: '货物形态', hide:true}
                                ,{field: 'createTime$', align: 'center',title: '操作时间'}
                                ,{field: 'count', align: 'center',title: '完成数'}
                                ,{field: 'status$', align: 'center',title: '状态'}
                                ,{field: 'memo', align: 'center',title: '备注'}
                                ,{field: 'createBy', align: 'center',title: '', hide:true}
                            ]],
                            request: {
                                pageName: 'curr',
                                pageSize: 'limit'
                            },
                            parseData: function (res) {
                                return {
                                    'code': res.code,
                                    'msg': res.msg,
                                    'count': res.data.total,
                                    'data': res.data.records
                                }
                            },
                            response: {
                                statusCode: 200
                            },
                            done: function (res) {
                                $(layero).find('.layui-table-view').css('margin', '0');
                                var index = 1;
                                $("[data-field='status$']").children().each(function () {
                                    index++;
                                    if ($(this).text() === '未出库') {
                                        $('tr').eq(index).css("color", 'red');
                                    }
                                });
                            },
                            size: ''
                        });
                    }
                });
        }
    });
    // 搜索
    form.on('submit(search)', function (data) {
        pageCurr = 1;
        tableReload(false);
    });
    // 重置
    form.on('submit(reset)', function (data) {
        pageCurr = 1;
        clearFormVal($('#search-box'));
        tableReload(false);
    });
    // 时间选择器
    function layDateRender(data) {
        setTimeout(function () {
            layDate.render({
                elem: '#ioTime\\$',
                type: 'datetime',
                value: data!==undefined?data['ioTime\\$']:null
            });
        }, 300);
    }
    layDateRender();
    // 打印
    function btnPrint(orderId, orderNo, settle) {
        printMatCodeNos.push(orderNo)
        var templateNo = 3;
        $.ajax({
             url: baseUrl+"/pakOut/print/auth",
           // url: baseUrl+"/pakOut/show",
            headers: {'token': localStorage.getItem('token')},
             data: {param: printMatCodeNos},
            method: 'POST',
            async: false,
            success: function (res) {
                if (res.code === 200){
                    layer.closeAll();
                    for (let i=0;i'
                    layer.open({
                        type: 1,
                        title: "拣货单",
                        area: '150px',
                        content: img
                    })
                    // box.print({mediaPrint:true});
                    // box.hide();
                } else if (res.code === 403){
                    top.location.href = baseUrl+"/";
                }else {
                    layer.msg(res.msg)
                }
            }
        })
    }
         function btnPrint2(orderId, orderNo, settle) {
            console.log(orderId);
            console.log(orderNo);
            console.log(settle);
            printMatCodeNos.push(orderNo);
            var templateNo = 4;
            $.ajax({
                url: baseUrl + "/ManPakOut/print/auth",
                headers: {'token': localStorage.getItem('token')},
                data: {param: printMatCodeNos},
                method: 'POST',
                async: false,
                success: function (res) {
                    if (res.code === 200) {
                        layer.closeAll();
                         var json = res;
                        json.docNum = orderNo;
                        console.log("json"+json);
                        let sum = 0;
                        for (let i = 0; i < res.data.length; i++){
                            sum += res.data[i].anfme;
                            console.log("sum:"+res.data.anfme);
                        }
                        json.anfmeSum = sum;
                        var barcodeUrl = baseUrl + "/ManPakOut/code/auth?type=1¶m=" + res.data[0].docNum;
                        console.log("barcodeUrl:"+barcodeUrl);
                        json.barcodeUrl = barcodeUrl;
                        for (let i = 0; i < json.data.length; i++) {
                            var templateDom = $("#templatePreview" + templateNo);
                            var className = templateDom.attr("class");
                            if (className === 'template-barcode') {
                                json.data[i]["barcodeUrl"] = baseUrl + "/ManPakOut/code/auth?type=2¶m=" + (json.data[i].docNum+";"
                                    +json.data[i].matnr+";"
                                    +json.data[i].maktx+";"
                                    +json.data[i].anfme+";"
                                    +json.data[i].locNo+";"
                                    +json.data[i].containerCode+";"
                                    +json.data[i].csocode+";"
                                    +json.data[i].isoseq+";"
                                    +json.data[i].wrkNo+";");
                            } else {
                                json.data[i]["barcodeUrl"] = baseUrl + "/ManPakOut/code/auth?type=2¶m=" + (json.data[i].docNum+";"
                                    +json.data[i].matnr+";"
                                    +json.data[i].maktx+";"
                                    +json.data[i].anfme+";"
                                    +json.data[i].locNo+";"
                                    +json.data[i].containerCode+";"
                                    +json.data[i].csocode+";"
                                    +json.data[i].isoseq+";"
                                    +json.data[i].wrkNo+";");
                            }
                        }
                        var tpl = templateDom.html();
                        var template = Handlebars.compile(tpl);
                        var html = template(json);
                        var box = $("#box");
                        box.html(html);
                        box.show();
                        box.print({mediaPrint: true});
                        box.hide();
                    } else if (json.code === 403) {
                        top.location.href = baseUrl + "/";
                    } else {
                        layer.msg(json.msg)
                    }
                }
            })
        }
});
// 关闭动作
$(document).on('click','#data-detail-close', function () {
    parent.layer.closeAll();
});
function tableReload(child) {
    var searchData = {};
    $.each($('#search-box [name]').serializeArray(), function() {
        searchData[this.name] = this.value;
    });
    tableIns.reload({
        where: searchData,
        page: {curr: pageCurr}
     });
}