var pageCurr;
layui.config({
    base: baseUrl + "/static/layui/lay/modules/"
}).use(['table','laydate', 'form', 'admin'], function() {
    var table = layui.table;
    var $ = layui.jquery;
    var layer = layui.layer;
    var layDate = layui.laydate;
    var form = layui.form;
    var admin = layui.admin;
    // 获取出库口
    function getOutBound(){
        // $.ajax({
        //     url: baseUrl+"/agv/available/empty/put/site",
        //     headers: {'token': localStorage.getItem('token')},
        //     method: 'POST',
        //     async: false,
        //     success: function (res) {
        //         if (res.code === 200){
        //             var tpl = $("#takeSiteSelectTemplate").html();
        //             var template = Handlebars.compile(tpl);
        //             var html = template(res);
        //             $('#staNoSelect').append(html);
        //             form.render('select');
        //         } else if (res.code === 403){
        //             top.location.href = baseUrl+"/";
        //         }else {
        //             layer.msg(res.msg)
        //         }
        //     }
        // })
    }
    // 数据渲染
    tableIns = table.render({
        elem: '#locMast',
        headers: {token: localStorage.getItem('token')},
        url: baseUrl+'/agv/locMast/list/auth',
        page: true,
        limit: 16,
        limits: [16, 30, 50, 100, 200, 500],
        where: {loc_sts: "D",floor:$('#floorSelect option:selected').val()},
        even: true,
        toolbar: '#toolbar',
        defaultToolbar: ['filter'],
        cellMinWidth: 50,
        cols: [[
            {type: 'checkbox', fixed: 'left'}
            ,{field: 'locNo', align: 'center',title: '库位号',sort:true}
            ,{field: 'locSts$', align: 'center',title: '库位状态',width:200}
            // ,{field: 'whsType$', align: 'center',title: '库位类型'}
            ,{field: 'row1', align: 'center',title: '排'}
            ,{field: 'bay1', align: 'center',title: '列'}
            ,{field: 'lev1', align: 'center',title: '层'}
            // ,{field: 'fullPlt', align: 'center',title: '满板', templet:function(row){
            //         var html = "";
            //         return html;
            //     },width:80}
            ,{field: 'modiUser$', align: 'center',title: '修改人员',event: 'modiUser'}
            ,{field: 'modiTime$', align: 'center',title: '修改时间'}
        ]],
        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, curr, count) {
            if (res.code === 403) {
                top.location.href = baseUrl+"/";
            }
            pageCurr=curr;
            getOutBound();
            limit();
            form.on('checkbox(tableCheckbox)', function (data) {
                var _index = $(data.elem).attr('table-index')||0;
                if(data.elem.checked){
                    res.data[_index][data.value] = 'Y';
                }else{
                    res.data[_index][data.value] = 'N';
                }
            });
        }
    });
    // 监听头工具栏事件
    table.on('toolbar(locMast)', function (obj) {
        var data = table.checkStatus(obj.config.id).data;
        switch (obj.event) {
            case 'outbound':
                var staNo = $("#staNoSelect").val();
                if (staNo === "" || staNo === null){
                    layer.msg("请选择出库口");
                    return;
                }
                var locNos = [];
                data.forEach(function(elem) {
                    locNos.push(elem.locNo);
                });
                if (data.length === 0){
                    layer.msg('请至少选中一行数据');
                } else {
                    $.ajax({
                        url: baseUrl+"/agv/empty/plate/out/start",
                        headers: {'token': localStorage.getItem('token')},
                        data: {
                            stationCode: staNo,
                            locNos: locNos
                        },
                        method: 'POST',
                        traditional:true,
                        success: function (res) {
                            if (res.code === 200){
                                tableReload(); // 重载表格
                                getOutBound(); // 重载出库口
                                layer.msg(res.msg);
                            } else if (res.code === 403){
                                top.location.href = baseUrl+"/";
                            } else {
                                layer.msg(res.msg)
                            }
                        }
                    });
                }
                break;
        }
    });
    function changeStation(){
        var floor = $('#floorSelect option:selected').val();
        $('#staNoSelect').empty();
        if(floor == 1){
            $('#staNoSelect').append("");
            $('#staNoSelect').append("")
        }
        if(floor == 3){
            $('#staNoSelect').append("");
            $('#staNoSelect').append("")
            $('#staNoSelect').append("")
        }
        form.render();
    }
    // 搜索栏搜索事件
    form.on('submit(search)', function (data) {
        pageCurr = 1;
        $('#detlTable').css("display", 'none');
        tableReload(false);
        getOutBound();
        changeStation();
    });
    // 搜索栏重置事件
    form.on('submit(reset)', function (data) {
        pageCurr = 1;
        clearFormVal($('#search-box'));
        $('#detlTable').css("display", 'none');
        tableReload(false);
        getOutBound();
    });
})
function tableReload(child) {
    var searchData = {};
    $.each($('#search-box [name]').serializeArray(), function() {
        searchData[this.name] = this.value;
    });
    (child ? parent.tableIns : tableIns).reload({
        where: searchData,
        page: {
            curr: pageCurr
        },
        done: function (res, curr, count) {
            if (res.code === 403) {
                top.location.href = baseUrl+"/";
            }
            pageCurr=curr;
            if (res.data.length === 0 && count !== 0) {
                tableIns.reload({
                    where: searchData,
                    page: {
                        curr: pageCurr-1
                    }
                });
                pageCurr -= 1;
            }
            limit(child);
        }
    });
}