/**
 * guoj
 * 2019.03.12
 * 集货区
 */
 import { UPCLOUD_RDC_TMS_MAINDATA_SERVICE,UPCLOUD_RDC_TMS_BUSINESS_SERVICE,UPCLOUD_RDC_TMS_INTERFACE_SERVICE} from '@/config/serviceSettings.js';
 import { getSelectGJ, querySysPicture } from '@/utils/prolog.ajax.js';
 export default () => {
    //获取下拉框
    var selectData;
    var selectcompany;
    utilSyncAjaxGJ(UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/company/customers", "post", JSON.stringify({ "pageSize": 100, "pageNum": 1, "carrierid": "%" }), function (data) {
        selectcompany = data.data.list;
        for (var key in selectcompany) {
            selectcompany[key].text = selectcompany[key].locname;
            selectcompany[key].value = selectcompany[key].companyid;
        }
        selectcompany.unshift({ "text": "", "value": "" });
    }, "application/json");
    getSelectGJ([{ "objectcode": "st_status" }, { "objectcode": "operationtype" }, { "objectcode": "stagetype" },
    { "objectcode": "pc_lineno" }, { "objectcode": "delivertype" }, { "objectcode": "stagetype1" }], function (data) {
        data.data.st_status.unshift({ "text": "", "value": "" });
        data.data.operationtype.unshift({ "text": "", "value": "" });
        data.data.stagetype.unshift({ "text": "", "value": "" });
        data.data.pc_lineno.unshift({ "text": "", "value": "" });
        data.data.delivertype.unshift({ "text": "", "value": "" });
        data.data.stagetype1.unshift({ "text": "", "value": "" });
        selectData = data.data;

    });
    //新增修改状态
    var storeGoods_change;
    var opts = {
        //terrace显示全部，card分页显示
        skin: "card",
        columns: [
            { id: "stageid", name: "id", type: "ro", width: 100, hidden: true },
            { id: "stagecode", name: "暂存区编号", type: "ro", width: 100 },
            {
                id: "stagetype", name: "类型", type: "ro", width: 100, render: function (v, record) {
                    return getSelectValue(selectData.stagetype1, record.stagetype);
                }
            },
            {
                id: "linecode", name: "配送方向", type: "ro", width: 100, render: function (v, record) {
                    return getSelectValue(selectData.pc_lineno, record.linecode);
                }
            },
            {
                id: "deliverytype", name: "提货方式", type: "ro", width: 100, render: function (v, record) {
                    return getSelectValue(selectData.delivertype, record.deliverytype);
                }
            },
            {
                id: "stageattr4", name: "单据类型", type: "ro", width: 100, render: function (v, record) {
                    return getSelectValue(selectData.operationtype, record.stageattr4);

                }
            },
            {
                id: "stageattr3", name: "贸易伙伴", type: "ro", width: 100, render: function (v, record) {
                    return getSelectValue(selectcompany, record.stageattr3);

                }
            },
            {
                id: "stageattr1", name: "客户类型", type: "ro", width: 100, render: function (v, record) {
                    return getSelectValue(selectData.stagetype, record.stageattr1);

                }
            },
            { id: "stageattr5", name: "订单信息", type: "ro", width: 100 },
            { id: "stageattr2", name: "客户数限制", type: "ro", align: "right", width: 100 },
            { id: "numlimit", name: "订单数限制", type: "ro", align: "right", width: 100 },
            { id: "numlimit", name: "件数限制", type: "ro", align: "right", width: 100 },
            { id: "tunnelno", name: "巷道", type: "ro", align: "right", width: 100 },
            {
                id: "status", name: "状态", type: "ro", width: 80, render: function (v, record) {
                    if (record["status"] == 30) {
                        return Prolog.getCircle("yellow") + "锁定";
                    }
                    if (record["status"] == 10) {
                        return Prolog.getCircle("green") + "可用";
                    }
                    if (record["status"] == 20) {
                        return Prolog.getCircle("red") + "占用";
                    }
                }
            },
            {
                id: "op", name: "操作", type: "button", align: "left", fixed: "right", width: 150, render: function (v, record) {
                    var data = record.status == 30 ? "解锁" : "锁定";
                    return "修改^storeGoods_updataClick," + data + "^storeGoods_look,详情^storeGoods_detailed";
                }
            }
        ],
        multiselect: true,
        fixedLast: true, //开启固定列，固定最后一行，倒数第二列不能隐藏
        toolbar: [
            { type: "settings", storeGoods: "label-left", labelWidth: 80 },
            {
                type: "block", id: "bk", blockOffset: 0, list: [
                    { type: "combo", readonly: true, width: 144, name: "status", label: "状态:", options: selectData.st_status },
                    { type: "newcolumn" },
                    { type: "combo", readonly: true, width: 144, name: "stageattr4", label: "业务类型:", options: selectData.operationtype },
                    { type: "newcolumn" },
                    {
                        type: "combo", readonly: true, width: 144, name: "stagetype", label: "暂存区类型:", options: selectData.stagetype1
                    },
                    { type: "newcolumn" },
                    {
                        type: "combo", readonly: true, width: 144, name: "linecode", label: "配送方向:", options: selectData.pc_lineno
                    },
                    { type: "newcolumn" },
                    {
                        type: "combo", readonly: true, width: 144, name: "deliverytype", label: "提货方式:", options: selectData.delivertype
                    },
                    { type: "newcolumn" },
                    { type: "input", name: "stagecodeStart", width: 144, label: "暂存区始:" },
                    { type: "newcolumn" },
                    { type: "input", name: "stagecodeEnd", width: 144, label: "暂存区止 :" },
                    { type: "newcolumn" },
                    { type: "input", name: "tunnelno", width: 144, label: "巷道:" },
                    { type: "newcolumn" },
                    { type: "button", name: "querypos", value: "查询" },
                    { type: "newcolumn" },
                    { type: "button", name: "Reset", className: "normal", value: "重置" },
                    { type: "newcolumn" },
                    { type: "button", name: "addBtn", offsetLeft: 10, value: "<i class='layui-icon layui-icon-add-1'></i>新增" },
                    { type: "newcolumn" },
                    { type: "button", name: "exportBtn", className: "normal", value: "<i class='layui-icon layui-icon-download-circle'></i>导出" }
                ]
            }
        ],
        imagePath: "/prologui/dist/images/PlgGrid",
        url: "/basic/store-goods/list",//数据接口
        type: "post",//数据提交方式，默认为get
        pagebar: {
            buttons: [
                { text: "锁定", name: "storeGoods_look" },
                { text: "解锁", name: "storeGoods_unnp" },
                { text: "批量修改", name: "batchUpdata" }
            ],
            type: "full" //full simple
        },
        params: {},//查询提交参数，分页参数默认为pageNum,pageSize
        page: true, //是否启用分页
        totalCount: "totalCount" //总记录数字段，默认totalCount
    };
    //分页参数 pageSize pageNum
    var grid4 = new PlgGrid(opts);
    grid4.renderTo("guojzhuans41");
    grid4.getToolBarForm().setFormData({ "systemId": "prolog-healthy-wms" });
    grid4.loadData();

    //按钮添加方法
    grid4.attachToolBarEvent("onButtonClick", function (name) {
        if (name == "querypos") {
            var fdata = grid4.getToolBarForm().getFormData();
            grid4.setParams(fdata);
            grid4.reload();
        }
        if (name == "addBtn") {
            $("#displayErr1").html("新增");
            availableGJ();
            storeGoods_from1.clear();
            initAddFrom();
            storeGoods_change = 0;
            $("#tastzhusn1").css("display", "block");
            $("#guojzhuans41").css("display", "none");
            $("#post_Template22").css("display", "none");
            $("#storeGoods_div9").css("display", "none");
        }
        if (name == "Reset") {
            grid4.getToolBarForm().clear();
        }
        if (name == "exportBtn") {
            storeGoods_export();
        }
    });

    //主界面导出事件
    function storeGoods_export() {
        let fdata = grid4.getToolBarForm().getFormData();
        let obj = JSON.parse(JSON.stringify(fdata));
        let url = "/basic/store-goods/export?status=" + encodeURI(obj.status) + "&stageattr4=" + encodeURI(obj.stageattr4) +
            "&stagetype=" + encodeURI(obj.stagetype) + "&linecode=" + encodeURI(obj.linecode) + "&deliverytype=" + encodeURI(obj.deliverytype) +
            "&stagecodeStart=" + encodeURI(obj.stagecodeStart) + "&stagecodeEnd=" + encodeURI(obj.stagecodeEnd) + "&tunnelno=" + encodeURI(obj.tunnelno);
        let token = localStorage.getItem("authorization");
        if (token == null || token == "") {
            PlgDialog.msg("登录状态异常，请重新登录！");
            window.location.href = "/menu?path=login";
            return;
        }
        let xhh = new XMLHttpRequest();
        xhh.open("get", url);
        xhh.setRequestHeader("Authorization", token);
        xhh.responseType = "blob";
        xhh.onreadystatechange = function () {
            if (xhh.readyState === 4 && xhh.status === 200) {
                let filename = "集货区" + new Date().getTime() + ".xlsx";
                let blob = new Blob([xhh.response], { type: "text/xlsx" });
                let csvUrl = URL.createObjectURL(blob);
                let link = document.createElement("a");
                link.href = csvUrl;
                link.download = filename;
                link.click();
            }
        };
        xhh.send();

    }
    //按钮添加方法
    grid4.attachPageBarEvent(function (name) {
        //锁定
        if (name == "storeGoods_look") {
            let datas = getDeleteAll(grid4);
            utilAjaxGJ("/basic/store-goods/update-status2", "post", JSON.stringify({ "stageids": datas, "status": "30" }), function (data) {
                setTimeout(() => { PlgDialog.msg(data.message == null ? "接口调用失败" : data.message); }, 300);
                grid4.reload();
            }, "application/json");
        }
        //解锁
        if (name == "storeGoods_unnp") {
            let datas = getDeleteAll(grid4);
            utilAjaxGJ("/basic/store-goods/update-status2", "post", JSON.stringify({ "stageids": datas, "status": "10" }), function (data) {
                setTimeout(() => { PlgDialog.msg(data.message == null ? "接口调用失败" : data.message); }, 300);
                grid4.reload();
            }, "application/json");
        }
        //批量修改
        if (name == "batchUpdata") {
            $("#displayErr1").html("批量修改");
            storeGoods_from1.clear();
            storeGoods_from1.disableItem("stagecode");
            storeGoods_change = 9;
            $("#tastzhusn1").css("display", "block");
            $("#guojzhuans41").css("display", "none");
            $("#post_Template22").css("display", "none");
            $("#storeGoods_div9").css("display", "none");
        }
    });

    //修改
    window.storeGoods_updataClick = function (rid) {
        $("#displayErr1").html("修改");
        storeGoods_from1.clear();
        grid4.selectRowById(rid);
        storeGoods_from1.disableItem("stagecode");
        initAddFrom(1);
        storeGoods_change = 1;
        $("#tastzhusn1").css("display", "block");
        $("#guojzhuans41").css("display", "none");
        $("#post_Template22").css("display", "none");
        $("#storeGoods_div9").css("display", "none");
    }; //详情
    window.storeGoods_detailed = function (rid, rdata) {
        $("#tastzhusn1").css("display", "none");
        $("#guojzhuans41").css("display", "none");
        $("#post_Template22").css("display", "none");
        $("#storeGoods_div9").css("display", "block");
        var dataSelect = selectData;
        new PlgPanel({
            renderer: "storeGoods_div422",
            skin: 2,
            header: {
                isShow: true,
                title: "详情"
            },
            defaultBody: {
                layoutCol: 3,
                valueBj: true,
                cols: [
                    [
                        { value: rdata.stagecode, label: "暂存区编号" },
                        { value: getSelectValue(dataSelect.stagetype1, rdata.stagetype), label: "类型" },
                        { value: getSelectValue(dataSelect.pc_lineno, rdata.linecode), label: "配送方向" },
                        { value: getSelectValue(dataSelect.delivertype, rdata.deliverytype), label: "提货方式" },
                        { value: getSelectValue(dataSelect.operationtype, rdata.stageattr4), label: "业务类型" },
                        { value: getSelectValue(selectcompany, rdata.stageattr3), label: "贸易伙伴" },
                        { value: getSelectValue(dataSelect.stagetype, rdata.stageattr1), label: "客户类型" },
                        { value: rdata.stageattr5, label: "订单信息" },
                        { value: rdata.stageattr2, label: "客户数限制" },
                        { value: rdata.numlimit, label: "订单数限制" },
                        { value: rdata.tunnelno, label: "巷道" },
                        { value: getSelectValue(dataSelect.st_status, rdata.status), label: "状态" }]
                ]
            }
        });
    };
    //解锁锁定
    window.storeGoods_look = function (rid, rdata) {
        rdata.status = rdata.status == 30 ? 10 : 30;
        utilAjaxGJ("/basic/store-goods/update-status", "post", JSON.stringify(rdata), function (data) {
            setTimeout(() => { PlgDialog.msg(data.message == null ? "接口调用失败" : data.message); }, 300);
            $("#tastzhusn1").css("display", "none");
            $("#guojzhuans41").css("display", "block");
            $("#post_Template22").css("display", "none");
            $("#storeGoods_div9").css("display", "none");
            grid4.reload();
        }, "application/json");
    };
    function setFormData(plgform, data) {
        let fromData = plgform.getFormData();
        for (var key in fromData) {
            fromData[key] = data[key];
        }
        plgform.setFormData(fromData);
    }
    //跳转页面
    var carousel = layui.carousel;
    carousel.render({
        elem: "#storeGoods_div",
        autoplay: false,
        index: 0,
        arrow: "none",
        indicator: "none",
        anim: "fade",
        width: "100%", //设置容器宽度
        height: "100%"
    });

    function initAddFrom(type) {
        if (type == 1) {
            var data = grid4.getSelectedRowData();
            setFormData(storeGoods_from1, data);
        }
    }
    storeGoods_div1 = {
        renderer: "storeGoods_div1",
        items: [
            { type: "settings", storeGoods: "label-left", labelWidth: 80 },
            {
                type: "fieldset", name: "goodsDesc", label: "属性", list: [
                    { type: "hidden", name: "stageid", label: "id:", width: 170 },
                    { type: "hidden", name: "enterpriseid", label: "企业id:", width: 170 },
                    { type: "input", name: "stagecode", label: "暂存区编号:", width: 170, validate: "NotEmpty", required: true },
                    { type: "newcolumn" },
                    { type: "combo", readonly: true, name: "stagetype", label: "类型:", width: 170, validate: "NotEmpty", required: true, options: selectData.stagetype1 },
                    { type: "newcolumn" },
                    { type: "combo", readonly: true, name: "linecode", label: "配送方向:", width: 170, options: selectData.pc_lineno },
                    { type: "newcolumn" },
                    { type: "combo", readonly: true, name: "deliverytype", label: "提货方式:", width: 170, options: selectData.delivertype },
                    { type: "newcolumn" },
                    { type: "combo", readonly: true, name: "stageattr4", label: "单据类型:", width: 170, options: selectData.operationtype },
                    { type: "newcolumn" },
                    { type: "combo", readonly: true, name: "stageattr3", label: "贸易伙伴:", width: 170, options: selectcompany },
                    { type: "newcolumn" },
                    { type: "combo", readonly: true, name: "stageattr1", label: "客户类型:", width: 170, options: selectData.stagetype },
                    { type: "newcolumn" },
                    { type: "input", name: "stageattr5", label: "订单信息:", width: 170 },
                    { type: "newcolumn" },
                    { type: "input", name: "stageattr2", label: "客户数限制:", width: 170 },
                    { type: "newcolumn" },
                    { type: "input", name: "numlimit", label: "订单数限制:", width: 170 },
                    { type: "newcolumn" },
                    { type: "input", name: "tunnelno", label: "巷道:", width: 170 },
                    { type: "newcolumn" },
                    { type: "combo", readonly: true, name: "status", label: "状态:", width: 170, validate: "NotEmpty", required: true, options: selectData.st_status }]
            }]
    };
    storeGoods_from1 = new PlgForm(storeGoods_div1);
    //保存
    document.querySelector("#storeGoods_save").onclick = function () {
        var feik1 = storeGoods_from1.validate();
        if (!feik1) {
            PlgDialog.msg("数据不能为空");
            return;
        }
        var storeGoodsData1 = storeGoods_from1.getFormData();
        storeGoodsData1.branchid = userGJ.p_companyid;
        var url = "/basic/store-goods/create";
        //修改
        if (storeGoods_change == 1) {
            url = "/basic/store-goods/update";
        }
        //批量修改数据
        if (storeGoods_change == 9) {
            var datas = getDeleteAll(grid4);
            url = "/basic/store-goods/batch-update";
            storeGoodsData1 = { "stageids": datas, "fromdata": storeGoodsData1 };
        }
        utilAjaxGJ(url, "post", JSON.stringify(storeGoodsData1), function (data) {
            setTimeout(() => { PlgDialog.msg(data.message == null ? "接口调用失败" : data.message); }, 300);
            $("#tastzhusn1").css("display", "none");
            $("#guojzhuans41").css("display", "block");
            $("#post_Template22").css("display", "none");
            $("#storeGoods_div9").css("display", "none");
            grid4.reload();
        }, "application/json");
    };

    //可以编辑
    function availableGJ() {
        storeGoods_from1.enableAllItem();
    }
    //返回
    $(document).on("click", "#storeGoods_back1", function () {
        $("#tastzhusn1").css("display", "none");
        $("#guojzhuans41").css("display", "block");
        $("#post_Template22").css("display", "none");
        $("#storeGoods_div9").css("display", "none");
    });
    utilImportGJ2("post_Template11", "postionImportId", "postionExportId", "url", function () {
        $("#tastzhusn1").css("display", "none");
        $("#guojzhuans41").css("display", "block");
        $("#post_Template22").css("display", "none");
        $("#storeGoods_div9").css("display", "none");
    });
};