var Frm;
var F_CONTRGRPNO = "";
var F_NOS = "";
var bootstrap = function ($, learun) {
    Frm = learun;
    function httpHeaders() {
        var headers = {
            token: top.$.lcoreUser.token
        }
        return headers;
    }
    var page = {
        init: function () {
            page.bind();
            page.getLine('WTP');
        },
        bind: function () {
            // 仓库与区域多选
            $('#locWareHouse').lrselect({
                url: top.$.rootUrl + '/BaseManager/BaseWarehouse/GetCheckWWTreeByLocation',
                maxHeight: 180,
                allowSearch: true
            });
            $('#locWareHouse').lrselectSet("WTP");
            $('#lr_refresh').on('click', function () {
                var houseCode = $("#locWareHouse").lrselectGet();
                page.getChartData(houseCode, $("#lineNum").val());
            });
            //滑动条滑动事件,改变数据框的显示值
            $("#lineNum").change(function () {
                $("#lineValue").val($(this).val());
            });
            //输入的数值只能在范围内
            $("#lineValue").keyup(function () {
                let curValue = Number($(this).val());
                if (curValue > Number($("#maxValue").text())) {
                    curValue = Number($("#maxValue").text());
                    $(this).val(curValue);
                }
                if (curValue < Number($("#minValue").text())) {
                    curValue = Number($("#minValue").text());
                    $(this).val(curValue);
                }
                $("#lineNum").val($(this).val());
            });
            //数字框选择事件,改变滑动条(点击上下选择有效)
            $("#lineValue").change(function () {
                $("#lineNum").val($(this).val());
                var houseCode = $("#locWareHouse").val();
                houseCode = houseCode.substring(0, 4);
                getChartData(houseCode, $("#lineNum").val());
            });
            //仓库选择事件,改变行数据(最大值和最小值)
            $("#locWareHouse").change(function () {
                var houseCode = $(this).lrselectGet();
                page.getLine(houseCode);
            });
            // 行数值改变事件
            $("#lineNum").change(function () {
                var houseCode = $("#locWareHouse").lrselectGet();
                page.getChartData(houseCode, $("#lineNum").val());
            });
            //弹框关闭事件
            $("#closeModal").click(function () {
                for (let i = 2; i < 4; i++) {
                    $("#loc" + i).css("display", "none");
                }
                // 关闭时将选中的托盘号清空
                F_CONTRGRPNO = "";
                F_NOS = "";
                $("#myModal").hide();
            });
            $(".locCode").click(function () {
                let type = $("#locType").text();
                var code = $(this).text();
                var prm = { conCode: ' ', barCode: ' ' };
                if (type == "0") {
                    prm.conCode = code;
                }
                else {
                    prm.barCode = code;
                }
                var color = $(this).css("color");
                var bgColor = $(this).css("background-color");
                $(".locCode").css("background-color", bgColor);
                $(".locCode").css("color", color);
                $(this).css("background-color", "#2F94EE");
                $(this).css("color", "#FEFEFF");
                //$.post("GetLocDetailList", prm, function (response) {
                //    page.showTable(eval("(" + response + ")").data);
                //})
            });
        },
        getLine: function (houseNo) {
            $("#lineNum").val("");//清空行数
            $("#lineValue").val("");
            $("#minValue").text("0");
            $("#maxValue").text("0");
            $.ajax({
                type: 'post',
                headers: httpHeaders(),
                dataType: 'json',
                url: top.$.rootUrl + '/CPManager/QueryCell/GetMaxLine?houseNo=' + houseNo,
                
                success: function (response) {
                    var res = eval(response);
                    // 如果行数存在,则行数的当前值设置为1,可选最小值设置为1
                    if (res.data > 0) {
                        $("#lineNum").val(1);
                        $("#lineValue").val(1);
                        $("#lineNum").attr("min", 1);
                        $("#lineValue").attr("min", 1);
                        $("#minValue").text("1");
                    }
                    // 如果行数不存在,则行数设置为0,可选最小值设置为0
                    if (res.data < 1) {
                        $("#lineNum").attr("min", 0);
                        $("#lineValue").attr("min", 0);
                        $("#minValue").text("0");
                    }
                    $("#maxValue").text(res.data); //设置可选最大值
                    $("#lineNum").attr("max", res.data);
                    $("#lineValue").attr("max", res.data);
                    var houseCode = "WTP";// $("#locWareHouse").val();
                    page.getChartData(houseCode, $("#lineNum").val());//加载储位展示图
                    // houseCode = houseCode.substring(0, 4);
                }
            });
            //$.get("GetMaxLine", { houseNo: houseNo }, function (response) {//获取最大行数
            //    var res = eval("(" + response + ")");
            //    if (res.data > 0) { //如果行数存在,则行数的当前值设置为1,可选最小值设置为1
            //        $("#lineNum").val(1);
            //        $("#lineValue").val(1);
            //        $("#lineNum").attr("min", 1);
            //        $("#lineValue").attr("min", 1);
            //        $("#minValue").text("1");
            //    }
            //    if (res.data < 1) { //如果行数不存在,则行数设置为0,可选最小值设置为0
            //        $("#lineNum").attr("min", 0);
            //        $("#lineValue").attr("min", 0);
            //        $("#minValue").text("0");
            //    }
            //    $("#maxValue").text(res.data); //设置可选最大值
            //    $("#lineNum").attr("max", res.data);
            //    $("#lineValue").attr("max", res.data);
            //    var houseCode = $("#locWareHouse").val();
            //    houseCode = houseCode.substring(0, 4);
            //    getChartData(houseCode, $("#lineNum").val());//加载储位展示图
            //})
        },
        getChartData: function (houseNo, line) {
            houseNo='hjhouse';
            $.ajax({
                type: 'post',
                headers: httpHeaders(),
               
                dataType: 'json',
                url: top.$.rootUrl + '/CPManager/QueryCell/GetLocList?houserNo=' + houseNo + "&line=" + line,
             
                success: function (response) {
                    var res = eval(response);
                    page.showChart(eval(res.data.json), res.data.y + 1, res.data.x + 1); //加载储位展示图(坐标加1,显示需要)
                }
            });
            //$.post("GetLocList", { houserNo: houseNo, line: line }, function (response) {
            //    var res = eval("(" + response + ")");
            //    showChart(eval("(" + res.data.json + ")"), res.data.y + 1, res.data.x + 1); //加载储位展示图(坐标加1,显示需要)
            //})
        },
       
        showChart: function (data, layerList, slotCnt) {
            var chart = echarts.init(document.getElementById("echart"));
            if (layerList > 1 && slotCnt > 1) {
                page.setChart(data, layerList, slotCnt);//Echart所需要的设置
                chart.setOption(option);//Echart所需要的设置
                chart.off('click');//点击事件之前,要释放之前的点击事件
                chart.on('click', function (params) {//点击事件
                    /// 获取选中的货位号
                    /// alert(params.data.fNo);
                    if (params.data.F_CNTRGRPNO != ' ') {
                        $(".locaddrto").remove();
                        var houseCode = $("#locWareHouse").val();
                        houseCode = houseCode.substring(0, 4);
                        //页面初始加载,添加目标地址信息
                        //$.post("GetAddrtoList", { WarehouseCode: houseCode }, function (response) {
                        //    var res = eval("(" + response + ")");
                        //    var houseSelect = $("#locAddrto");
                        //    for (let house in res.data) {
                        //        houseSelect.append($(""));
                        //    }
                        //})
                        F_CONTRGRPNO = params.data.F_CNTRGRPNO;
                        F_NOS = params.data.fNo;
                        Frm.layerForm({
                            id: 'form',
                            title: '货位明细',
                            url: top.$.rootUrl + '/CPManager/QueryCell/Form?WarehouseCode=' + houseCode + "&contrgrpno=" + params.data.F_CNTRGRPNO,
                            width: 1100,
                            height: 500,
                            btn: ['关闭'],
                            //callBack: function (id) {
                            //    return top[id].acceptClick(refreshGirdData);
                            //}
                        });
                    }
                });
            }
            else {
                //没有图标数据的时候,默认展示一个空的表格
                var dataHide = [
                    {
                        name: "b1",
                        id: 31,
                        hashMode: "设备",
                        value: [0, 0, 0, 0],
                        itemStyle: { normal: { color: "#715881" } }
                    }];
                page.setChart(dataHide, 0, 0);
                chart.setOption(option);
            }
        },
        setChart: function (data, layerList, slotCnt) {
            function renderItem(params, api) {
                var start = api.coord([api.value(2), api.value(1)]);
                var rectShape = echarts.graphic.clipRectByRect({
                    x: start[0],
                    y: start[1],
                    width: api.size([100, 1])[0] * ((api.value(3) - api.value(2)) / 100),
                    height: api.size([100, 1])[1] * (api.value(1) - api.value(0))
                }, {
                    x: params.coordSys.x,
                    y: params.coordSys.y,
                    width: params.coordSys.width,
                    height: params.coordSys.height
                });
                return (
                    rectShape && {
                        type: "rect",
                        shape: rectShape,
                        style: api.style({
                            //text: `${data[params.dataIndex].name}`
                        })
                    }
                );
            }
            option = {
                tooltip: {
                    formatter: function (params) {
                        return (
                            // params.marker +
                            // params.name +
                            //  ": " +
                            //  params.value[2] +
                            // "~" +
                            // params.value[3] + '
' + params.marker + `hashMode:${params.data.hashMode}`
                            params.marker + params.data.STATE + "
"
                            + params.marker + params.data.fNo + "
"
                            + params.marker + params.data.F_COL + "行" + params.data.F_LAYER + "列" + params.data.F_DEPTH + "层" //光标悬浮显示内容
                        );
                    }
                },
                title: {
                    //   text: "Profile",
                    left: "center"
                },
                grid: {
                    // height: chartHeight
                },
                xAxis: {
                    min: 0,
                    splitNumber: slotCnt,
                    max: slotCnt
                },
                yAxis: [{
                    position: "left",
                    max: layerList,
                    min: 0,
                    splitNumber: layerList
                }
                    //,
                    //{
                    //    position: "left",
                    //    data: layerList
                    //}
                ],
                series: [{
                    type: "custom",
                    itemStyle: {
                        //normal: {
                        //    opacity: 1
                        //},
                        //borderColor: 'red',
                        //borderWidth: 3
                    },
                    renderItem: renderItem,
                    encode: {
                        x: [1, 2],
                        y: 0
                    },
                    data: data,
                }]
            };
        }
    };
    page.init();
};