layui.config({
    base: "/js/"
}).use(['form', 'element', 'vue', 'layer', 'laydate', 'jquery','hhweb', 'table', 'utils', 'Universal'], function () {
    var form = layui.form,
        layer = layui.layer,
        element = layui.element,
        laydate = layui.laydate,
        $ = layui.jquery,
        table = layui.table,
        Universal = layui.Universal;

    var AreaName = 'task';
    var TableName = 'TaskDetail';

    var vm = new Vue({
        el: '#PalletForm'
    });

    var PalletIn = new Vue({
        data: {}
    });


    var mainList = {
        //NoData: function () {
        //    return "<div id='background' style='width:100%;height:100%;background-image:url(../../images/NoData.png);background-repeat:no-repeat;background-size:cover;'>.</div>"
        //},
        mainList: function (vm) {
            table.reload('mainList', {
                url: "/" + "task" + "/" + "Task" + "/StationToInventory"
                , where: { stationCode: vm }
                , method: "post"
                , text: { none: "暂无数据,请扫码容器编号!" }
               // , text: { none: this.NoData() }
            });
        }
    };

    var selector = {
        'TaskType': {
            SelType: "FromDict",
            SelFrom: "taskType",
            SelModel: "TaskType",
            SelLabel: "DictLabel",
            SelValue: "DictValue",
            Dom: [$("[name='TaskType']")]
        },
        'ContainerStatus': {
            SelType: "FromDict",
            SelFrom: "containerStatus",
            SelModel: "ContainerStatus",
            SelLabel: "DictLabel",
            SelValue: "DictValue",
            Dom: [$("[name='ContainerStatus']")]
        },
        'Type': {
            SelType: "FromDict",
            SelFrom: "containerType",
            SelModel: "Type",
            SelLabel: "DictLabel",
            SelValue: "DictValue",
            Dom: [$("[name='Type']")]
        },
        //'Products': {
        //    SelType: "FromDict",
        //    SelFrom: "inventoryStatus",
        //    SelModel: "Products",
        //    SelLabel: "DictLabel",
        //    SelValue: "DictValue",
        //    Dom: [$("[name='Products']")]
        //},
        //'IsPack': {
        //    SelType: "FromDict",
        //    SelFrom: "IsPack",
        //    SelModel: "IsPack",
        //    SelLabel: "DictLabel",
        //    SelValue: "DictValue",
        //    Dom: [$("[name='IsPack']")]
        //},
        //'IsFold': {
        //    SelType: "FromDict",
        //    SelFrom: "containerType",
        //    SelModel: "IsFold",
        //    SelLabel: "DictLabel",
        //    SelValue: "DictValue",
        //    Dom: [$("[name='IsFold']")]
        //},
        
    };

    var vml = new Array({
        vm: vm,
    });

    Universal.BindSelector($, form, vml, selector);


    //空栈板出库
    $(document).on("click", "#btnEmpty", function () {
        var str = $('[name = "IsShou"]:checked').val();
        if (str == undefined) {
            layer.alert("请选择站台", { icon: 5, shadeClose: true, title: "错误信息" });
        } else if (str == "EntranceStationY01") {
            var type = "p_1";
            layer.alert("确定空板补?", { shadeClose: true, title: "提示信息" }, function (index) {
                $.ajax({
                    async: false,
                    type: "post",
                    url: "/task/Task/EmptyPalletOut",
                    data: { station: "ExitStationY01", type: type },
                    dataType: "json",
                    success: function (result) {
                        if (result.Code == 200) {
                            layer.msg("呼叫空栈板成功", { icon: 6, shade: 0.4, time: 1000 });
                        }
                        else {
                            layer.alert("失败:" + result.Message, { icon: 5, shadeClose: true, title: "错误信息" }, function (index) { palletIdFocus(); layer.close(index); });
                        }
                    },
                    error: function (errorThrown) {
                        layer.alert(errorThrown, { icon: 2, title: '提示' });
                    }
                })
            });
            
        }
        else {
            var type = "p_1";
            layer.alert("确定空板补?", { icon: 5, shadeClose: true, title: "提示信息" },function () {
                $.ajax({
                    async: false,
                    type: "post",
                    url: "/task/Task/EmptyPalletOut",
                    data: { station: str, type: type },
                    dataType: "json",
                    success: function (result) {
                        if (result.Code == 200) {
                            layer.msg("呼叫空栈板成功", { icon: 6, shade: 0.4, time: 1000 });
                        }
                        else {
                            layer.alert("失败:" + result.Message, { icon: 5, shadeClose: true, title: "错误信息" }, function (index) { palletIdFocus(); layer.close(index); });
                        }
                    },
                    error: function (errorThrown) {
                        layer.alert(errorThrown, { icon: 2, title: '提示' });
                    }
                })
            });
           
        }
    });

    //空板入库
    $(document).on("click", "#btnEmptyBack", function () {
        var b = $('[name = "IsShou"]:checked').val();
        if (b == undefined) {
            layer.alert("请选择站台", { icon: 5, shadeClose: true, title: "错误信息" });
        } else if ($('[name = "PalletId"]').val() == "") {
            layer.alert("请扫描托盘号", { icon: 5, shadeClose: true, title: "错误信息" }, function (index) { $('[name = "ContainerId"]').val(""); $('[name = "ContainerId"]').focus(); layer.close(index); });
        }
        else {
            //layer.open({
            //    title: '空板回库',
            //    area: ["300px","250px"],
            //    type: 1,
            //    content: $('#PalletEmptyIn'),
            //    btn: ['提交', '关闭'],
            //    yes: function (index) {
            //        if ($('[name = "Type"]').val() != '') {
            //            layer.close(index);
            //            $.ajax({
            //                async: false,
            //                url: "/" + "task" + "/" + "Task" + "/BulidEmptyContainerBack",
            //                type: "post",
            //                data: { station: $('[name = "IsShou"]:checked').val(), containerCode: $('[name = "PalletId"]').val(), type: $('[name = "Type"]').val() },
            //                dataType: "json",
            //                success: function (result) {
            //                    if (result.code == 200) {
            //                        layer.msg("空托回库成功", { icon: 6, shade: 0.4, time: 1000 });
            //                        InEnd();
            //                    }
            //                    else {
            //                        layer.alert("失败:" + result.msg, { icon: 5, shadeClose: true, title: "错误信息" }, function (index) { $('[name = "ContainerId"]').val(""); $('[name = "ContainerId"]').focus(); layer.close(index); });
            //                    }
            //                },
            //                error: function (errorThrown) {
            //                    layer.alert(errorThrown, { icon: 2, title: '提示' });
            //                }
            //            })
            //        } else {
            //            layer.alert("请选择托盘类型", { icon: 5, shadeClose: true, title: "错误信息" });
            //        }
            //    },
            //    btn2: function (index) {
            //        layer.close(index);
            //    }
            //});
            layer.alert("确定空板入库?", { shadeClose: true, title: "提示信息" }, function (index) {
                $.ajax({
                    async: false,
                    url: "/" + "task" + "/" + "Task" + "/BulidEmptyContainerBack",
                    type: "post",
                    data: { station: $('[name = "IsShou"]:checked').val(), containerCode: $('[name = "PalletId"]').val(), type: $('[name = "Type"]').val() },
                    dataType: "json",
                    success: function (result) {
                        if (result.code == 200) {
                            layer.msg("空板回库成功", { icon: 6, shade: 0.4, time: 1000 });
                            InEnd();
                        }
                        else {
                            layer.alert("失败:" + result.msg, { icon: 5, shadeClose: true, title: "错误信息" }, function (index) { $('[name = "ContainerId"]').val(""); $('[name = "ContainerId"]').focus(); layer.close(index); });
                        }
                    },
                    error: function (errorThrown) {
                        layer.alert(errorThrown, { icon: 2, title: '提示' });
                    }
                })
            });
            
        }
    });
    //仓库选择
    $(document).on("click", "#btnstation", function () {
        InEnd();
        layer.open({
            title: '仓库选择',
            //area: ["800px", "400px"],
            type: 1,
            content: $('#Tunnel'),
            btn: ['确认'],
            yes: function (index) {
                layer.close(index);
                palletIdFocus();
            }
        });
    });
    //选择仓库,根据仓库显示不同输入框
    $(document).on("click", ".d1", function () {
        if ($("#Juan").is(":checked")) {
            AddStation(0);
            $(".Supplier").hide();
            $(".Customer").show();
            $(".Typesetting").hide();
        }
        if ($("#Pian").is(":checked")) {
            AddStation(1);
            $(".Supplier").hide();
            $(".Customer").hide();
            $(".Typesetting").show();
        }
        if ($("#Tong").is(":checked")) {
            AddStation(2);
            $(".Supplier").show();
            $(".Customer").hide();
            $(".Typesetting").hide();
        }
    });
    //根据不同仓库显示不同弹窗
    function AddStation(data) {
        var Station
        if (data == 0) {
             Station = $("#StationJuan");
        }
        if (data == 1) {
             Station = $("#StationPian");
        }
        if (data == 2) {
             Station = $("#StationTong");
        }
        layer.open({
            title: '站台选择',
            type: 1,
            content: Station,
            btn: ['确认'],
            yes: function (index) {
                var Station = $('[name = "IsShou"]:checked').val();
                $('#btnstation').text($('[name = "IsShou"]:checked').parent().text());
                var a = $('#btnstation').text();
                $('[name = "ContainerId"]').attr("disabled", "disabled");
                $('[name = "ContainerId"]').css("background-color", "#eee");
                //如果是暂存区,请求站台数据,前端显示托盘号
                if (a.indexOf("暂存") != -1) {
                    $('[name = "PalletId"]').val("");
                    $.ajax({
                        async: false,
                        url: "/" + "task" + "/" + "Task" + "/SelStation",
                        type: "post",
                        data: { station: Station },
                        dataType: "json",
                        success: function (result) {
                            if (result.code==200) {
                                $('[name = "PalletId"]').val(result.data.Containercode);
                            }
                            else {
                                layer.alert(result.data, { icon: 2, title: '提示' });
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            layer.alert(errorThrown, { icon: 2, title: '提示' });
                        }
                    });
                    $("#btnEmpty").hide();
                    $("#btnEmptyBack").hide();
                    $("#btnStack").hide();
                    $('[name = "PalletId"]').attr("disabled", "disabled");
                    $('[name = "PalletId"]').css("background-color", "#eee");
                }
                //判断是否为裁切机
                else if (Station.indexOf("ProductStationC0") == 0 && Station != "ProductStationC10") {
                    $("#btnEmptyBack").hide();
                    $("#btnEmpty").show();
                    $("#btnStack").hide();
                }
                //判断是否为预叠区
                else if (Station == "EntranceStationY01") {
                    $("#btnStack").show();
                    $("#btnEmptyBack").show();
                    $("#btnEmpty").show();
                }
                //判断是否为叠配区
                else if (Station == "ProductStationC10" || Station == "ProductStationD01") {
                    $("#btnEmpty").hide();
                    $("#btnStack").hide();
                    $("#btnEmptyBack").show();
                }
                else {
                    $('[name = "PalletId"]').removeAttr("disabled");
                    $('[name = "PalletId"]').removeClass("background-color");
                    $('[name = "PalletId"]').val("");
                    palletIdFocus();
                    $("#btnEmpty").show();
                    $("#btnEmptyBack").show();
                    $("#btnStack").hide();
                }
                layer.close(index);
            }
        });
    }

    //托盘入库
    $(document).on("click", "#btnPalletIn", function () {
        document.getElementById("attribute").reset();
        var b = $('[name = "IsShou"]:checked').val();
        var Warehouse = $('[name = "HD"]:checked').val();
        var Zhantai = $('#btnstation').text();
        var pallet = $('[name = "PalletId"]').val();
        var isBuffer = 0;
        if (Warehouse == undefined) {
            layer.alert("请选择站台", { icon: 5, shadeClose: true, title: "错误信息" });
        }
         else if ($('[name = "PalletId"]').val() == "" && Zhantai.indexOf("暂存") == -1) {
             layer.alert("请扫描托盘号", { icon: 5, shadeClose: true, title: "错误信息" }, function (index) { $('[name = "ContainerId"]').val(""); palletIdFocus(); layer.close(index); });
        } else {
             if (Warehouse == "0") {
                $("#Export").show();
                 $("#SelfUse").hide();
            }
            else if (Warehouse == "1") {
                $("#Export").hide();
                $("#SelfUse").show();
            }
            else if (Warehouse == "2") {
                $("#Export").hide();
                $("#SelfUse").hide();
            }
            layer.open({
                title: '特殊属性',
                area: ["300px", "250px"],
                type: 1,
                content: $('#Special'),
                btn: ['确认'],
                yes: function (index) {
                        layer.close(index);
                        if (b == "ProductStationD01") {
                            layer.open({
                                title: '回库选择'
                                , content: '请选择回库地址'
                                , btn: ['立库', '暂存区']
                                , yes: function (index, layero) {
                                    isBuffer = 0;
                                    layer.close(index);
                                }
                                , btn2: function (index, layero) {
                                    isBuffer = 1;
                                    layer.close(index);
                                }, end: function () {
                                    $.ajax({
                                        async: false,
                                        url: "/" + "task" + "/" + "Task" + "/HandTaskIn",
                                        type: "post",
                                        data: {
                                            containerCode: $('[name = "PalletId"]').val(),
                                            station: b,
                                            isBuffer: isBuffer,
                                            isPack: $('[name = "IsPack"]').val(),
                                            isFold: $('[name = "IsFold"]').val(),
                                            status: $('[name = "Products"]').val(),
                                        },
                                        dataType: "json",
                                        success: function (result) {
                                            if (result.code == 200) {
                                                layer.msg("入库成功", { icon: 6, shade: 0.4, time: 1000 });
                                                InEnd();
                                            }
                                            else {
                                                layer.alert("失败:" + result.msg, { icon: 5, shadeClose: true, title: "错误信息" }, function (index) { palletIdFocus(); layer.close(index); });
                                            }
                                        },
                                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                                            layer.alert(errorThrown, { icon: 2, title: '提示' });
                                        }
                                    });
                                }
                            });
                        } else {
                            $.ajax({
                                async: false,
                                url: "/" + "task" + "/" + "Task" + "/HandTaskIn",
                                type: "post",
                                data: {
                                    containerCode: $('[name = "PalletId"]').val(),
                                    station: b,
                                    isBuffer: isBuffer,
                                    isPack: $('[name = "IsPack"]').val(),
                                    isFold: $('[name = "IsFold"]').val(),
                                    status: $('[name = "Products"]').val(),
                                },
                                dataType: "json",
                                success: function (result) {
                                    if (result.code == 200) {
                                        layer.msg("入库成功", { icon: 6, shade: 0.4, time: 1000 });
                                        InEnd();
                                    }
                                    else {
                                        layer.alert("失败:" + result.msg, { icon: 5, shadeClose: true, title: "错误信息" }, function (index) { ContainerIdFocus(); layer.close(index); });
                                    }
                                },
                                error: function (XMLHttpRequest, textStatus, errorThrown) {
                                    layer.alert(errorThrown, { icon: 2, title: '提示' });
                                }
                            });
                        }
                    }

            });

        }

    });
    //排版房到叠配区
    $(document).on("click", "#btnStack", function () {
        var str = $('[name = "IsShou"]:checked').val();
        if (str == undefined) {
            layer.alert("请选择站台", { icon: 5, shadeClose: true, title: "错误信息" });
        } 
        else {
            var type = "p_1";
            $.ajax({
                async: false,
                type: "post",
                url: "/task/Task/StationToStation",
                data: { containerCode: $('[name = "PalletId"]').val(), startStation: $('[name = "IsShou"]:checked').val(), endStation:"ProductStationC09" },
                dataType: "json",
                success: function (result) {
                    if (result.Code == 200) {
                        layer.msg("任务发送成功", { icon: 6, shade: 0.4, time: 1000 });
                        InEnd();
                    }
                    else {
                        layer.alert("失败:" + result.Message, { icon: 5, shadeClose: true, title: "错误信息" }, function (index) { palletIdFocus(); layer.close(index); });
                    }
                },
                error: function (errorThrown) {
                    layer.alert(errorThrown, { icon: 2, title: '提示' });
                }
            })
        }
    });

    //打开页面聚焦“容器管理”
    $(document).ready(function () {
        palletIdFocus();
    });

    function palletIdFocus() {
        $('[name = "PalletId"]').attr("readonly", "true");
        $('[name = "PalletId"]').val("");
        $('[name = "PalletId"]').focus();
        setTimeout(function () {
            $('[name = "PalletId"]').removeAttr("readonly");
        }, 0)
    }

    function ContainerIdFocus() {
        $('[name = "ContainerId"]').attr("readonly", "true");
        $('[name = "ContainerId"]').focus();
        setTimeout(function () {
            $('[name = "ContainerId"]').removeAttr("readonly");
        }, 0)
    }

    //“物料编号”回车监听
    $('[name = "ContainerId"]').bind("keydown", function (e) {
        if (e.which == 13) {
            AddInventory();
            e.preventDefault(); //Skip default behavior of the enter key
        }
    });
    //“托盘编号”回车监听
    $('[name = "PalletId"]').bind("keydown", function (e) {
        if (e.which == 13) {
            ContrastPallet();
            e.preventDefault(); //Skip default behavior of the enter key
        }
    });

    //手动添加物料信息
    function AddInventory() {
        var b = $('[name = "IsShou"]:checked').val();
        var C = $('[name = "HD"]:checked').val();
        if (b == undefined) {
            layer.alert("请选择站台", { icon: 5, shadeClose: true, title: "错误信息" });
        } else {
            var str = $('[name = "ContainerId"]').val();
            var strs = str.split('%');
            if (C == 0 && strs.length == "6") {
                $('[name = "MaterialNum"]').val(strs[0]);
                $('[name = "Lot"]').val(strs[1]);
                $('[name = "Num"]').val(strs[3]);
                $('[name = "Customer"]').val(strs[4]);
                $('[name = "Batch"]').val(strs[5]);
                $('[name = "ContainerId"]').val("");
            }
            else if (C == 1 && strs.length == "6") {
                $('[name = "MaterialNum"]').val(strs[0]);
                $('[name = "Lot"]').val(strs[1]);
                $('[name = "Num"]').val(strs[3]);
                $('[name = "Typesetting"]').val(strs[4]);
                $('[name = "Batch"]').val(strs[5]);
                $('[name = "ContainerId"]').val("");

            } else if (C == 2 && strs.length == "5") {
                $('[name = "MaterialNum"]').val(strs[0]);
                $('[name = "Supplier"]').val(strs[1]);
                $('[name = "Batch"]').val(strs[2]);
                $('[name = "Lot"]').val(strs[3]);
                $('[name = "Num"]').val(strs[4]);
            } else {
                layer.alert("失败:扫描错误,请重新扫描", { icon: 5, shadeClose: true, title: "错误信息" }, function (index) { $('[name = "ContainerId"]').focus(); layer.close(index); $('[name = "ContainerId"]').val(""); });
                return false;
            }

            $.ajax({
                async: false,
                url: "/" + "task" + "/" + "Task" + "/AddInventory",
                type: "post",
                data: {
                    containerCode: $('[name = "PalletId"]').val(),
                    station: b ,
                    material: $('[name = "MaterialNum"]').val(),
                    Lot: $('[name = "Lot"]').val(),
                    num: $('[name = "Num"]').val(),
                    Batch: $('[name = "Batch"]').val(),
                    Customer: $('[name = "Customer"]').val(),
                    Typesetting: $('[name = "Typesetting"]').val(),
                    SupplierL: $('[name = "SupplierL"]').val(),
                },
                dataType: "json",
                success: function (result) {
                    console.log(result);
                    if (result.code == 200 && result.msg == null) {
                        $('[name = "ContainerId"]').val("");
                        layer.msg("入库成功", { icon: 6, shade: 0.4, time: 1000 });
                    } 
                    else if (result.code == 200 && result.msg != null) {
                        $('[name = "ContainerId"]').val("");
                        layer.msg(result.msg, { icon: 6, shade: 0.4, time: 1000 });
                    }
                    else if (result.code == 400 ) {
                        $('[name = "ContainerId"]').val("");
                        layer.alert("注意:" + result.msg, { icon: 5, shadeClose: true, title: "提示信息" }, function (index) { ContainerIdFocus(); layer.close(index); });
                    }
                    else {
                        $('[name = "MaterialNum"]').val("");
                        $('[name = "Lot"]').val("");
                        $('[name = "Num"]').val("");
                        $('[name = "Customer"]').val("");
                        $('[name = "Batch"]').val("");
                        $('[name = "Supplier"]').val("");
                        $('[name = "Typesetting"]').val("");
                        $('[name = "ContainerId"]').val("");
                        layer.alert("失败:" + result.msg, { icon: 5, shadeClose: true, title: "错误信息" }, function (index) { palletIdFocus(); layer.close(index); });
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.alert(errorThrown, { icon: 2, title: '提示' });
                }
            });
        }
    }
    //根据托盘号对比托盘是否正确
    function ContrastPallet() {
        var b = $('[name = "IsShou"]:checked').val();
        var Warehouse = $('[name = "HD"]:checked').val();
        var str = $('[name = "PalletId"]').val();
        if (str.length != 7) {
            layer.alert("托盘号长度不正确,正确托盘号为7位,<br>当前托盘号为" + str.length+"位,请重新扫描", { icon: 5, shadeClose: true, title: "错误信息" }, function (index) { $('[name = "PalletId"]').val(""); $('[name = "PalletId"]').focus(); layer.close(index); });
            return false;
        }
        if (Warehouse == "0" && str.indexOf("PJ") != 0 && str.length == 7) {
            layer.alert("托盘号格式与当前站台不符,请重新扫描托盘号", { icon: 5, shadeClose: true, title: "错误信息" }, function (index) { $('[name = "PalletId"]').val(""); $('[name = "PalletId"]').focus(); layer.close(index); });
            return false;
        }
        else if (Warehouse == "1" && str.indexOf("PP") != 0) {
            layer.alert("托盘号格式与当前站台不符,请重新扫描托盘号", { icon: 5, shadeClose: true, title: "错误信息" }, function (index) { $('[name = "PalletId"]').val(""); $('[name = "PalletId"]').focus(); layer.close(index); });
            return false;
        }
        else if (Warehouse == "2" && str.indexOf("TB") != 0) {
            layer.alert("托盘号格式与当前站台不符,请重新扫描托盘号", { icon: 5, shadeClose: true, title: "错误信息" }, function (index) { $('[name = "PalletId"]').val(""); $('[name = "PalletId"]').focus(); layer.close(index); });
            return false;
        }
       
        if (b == undefined) {
            layer.alert("请选择站台", { icon: 5, shadeClose: true, title: "错误信息" });
        } else {
            $.ajax({
                async: false,
                url: "/" + "task" + "/" + "Task" + "/PalletIsNo",
                type: "post",
                data: { Pallet: str, station: b},
                dataType: "json",
                success: function (result) {
                    if (result.code == 200) {
                        layer.msg("托盘正确", { icon: 6, shade: 0.4, time: 1000 });
                        $('[name = "ContainerId"]').removeAttr("disabled");
                        $('[name = "ContainerId"]').css("background-color", "white");
                        $('[name = "PalletId"]').attr("disabled", "disabled");
                        $('[name = "PalletId"]').css("background-color", "#eee");
                        ContainerIdFocus();
                    }
                    else {
                        layer.alert(result.data, { icon: 2, title: '错误' }, function (index) { palletIdFocus(); layer.close(index); });
                    }
                 
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.alert(errorThrown, { icon: 2, title: '提示' });
                }
            });
            //mainList.mainList(b);
        }
    }
    //界面恢复原始状态
    function InEnd() {
        $('[name = "MaterialNum"]').val("");
        $('[name = "Lot"]').val("");
        $('[name = "Num"]').val("");
        $('[name = "Customer"]').val("");
        $('[name = "Batch"]').val("");
        $('[name = "Supplier"]').val("");
        $('[name = "Typesetting"]').val("");
        $('[name = "ContainerId"]').val("");

        $('[name = "PalletId"]').removeAttr("disabled");
        $('[name = "PalletId"]').removeClass("background-color");
        $('[name = "ContainerId"]').attr("disabled", "disabled");
        $('[name = "ContainerId"]').css("background-color", "#eee");
        $('[name = "PalletId"]').val("");
        $('[name = "PalletId"]').focus();
    }
});