/**
 * guoj
 * 2018.10.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 ddfrom = null;
    //判断是否新增
    var message;
    //修改时候的数据
    var updataData;
    //按钮
    var batton = {
        del: "删除",
        update: "更新",
        run: "详情"
    };
    //查询数据
    var comp_data;
    utilSyncAjaxGJ("/sys/refdata/dtl/dd-list/get", "post", { "objectcode": "wms_storeroom" }, function (datas) {
        $("#kubie_select").attr("consignor", JSON.stringify(datas.data));
    });
    var gjzsdata = { "pageSize": 30, "pageNum": 1 };
    //查询
    queryGj();
    function queryGj() {
        $("#kubiewc").empty();
        utilSyncAjaxGJ("/basic/storeroom/list", "post", JSON.stringify(gjzsdata), function (datas) {
            comp_data = datas.data.list;
            for (var i = 0; i < comp_data.length; i++) {
                var text = comp_data[i].exsattr1 == null ? "" : comp_data[i].exsattr1;
                comp_data[i].name = comp_data[i].storeroomname;
                comp_data[i].description = text;
                comp_data[i].useNo = comp_data[i].storeroomcode;
                comp_data[i].btns = batton;
                comp_data[i].id = JSON.stringify(comp_data[i]);
            }
        }, "application/json");
        //渲染数据
        var config = {
            renderer: "kubiewc",
            data: {
                zoneId: 1,
                zoneName: "库别",
                isShowAdd: true,
                customerList: comp_data
            }
        };
        var plgCard = new PlgCardList(config);

        let btn = "<div class=\"plg-export\"><button class=\"layui-btn layui-btn-normal\" name=\"plg-export\"><i class=\"layui-icon layui-icon-download-circle\"></i>导出</button></div>";
        $("#treasury_div3 .plg-zone-header").prepend(btn);

        //导出
        plgCard.on("export", function () {
            unfilledWorktime_export();
        });
        //删除
        plgCard.on("del", function (ind) {
            var data = JSON.stringify([{ "storeroomcodes": ind.storeroomcode }]);
            utilDelete("/basic/storeroom/batch-delete", "put", data, function (data) {
                setTimeout(() => { PlgDialog.msg(data.message == null ? "接口调用失败" : data.message); }, 300);
                queryGj();
            }, "application/json");
        });
        //新增
        plgCard.on("add", function () {
            ddfrom.clear();
            $("#treasury_title").html("新增");
            $("#treasury_div9").css("display", "none");
            $("#treasury_div1").css("display", "block");
            $("#treasury_div3").css("display", "none");
            message = 0;
        });
        //主界面导出事件
        function unfilledWorktime_export() {
            var v = "";
            var url = "/basic/storeroom/export?storeroomcode=" + v;
            var token = localStorage.getItem("authorization");
            if (token == null || token == "") {
                PlgDialog.msg("登录状态异常，请重新登录！");
                window.location.href = "/menu?path=login";
                return;
            }
            var xhh = new XMLHttpRequest();
            xhh.open("get", url);
            xhh.setRequestHeader("Authorization", token);
            xhh.responseType = "blob";
            xhh.onreadystatechange = function () {
                if (xhh.readyState === 4 && xhh.status === 200) {
                    var filename = "库别" + new Date().getTime() + ".xlsx";
                    var blob = new Blob([xhh.response], { type: "text/xlsx" });
                    var csvUrl = URL.createObjectURL(blob);
                    var link = document.createElement("a");
                    link.href = csvUrl;
                    link.download = filename;
                    link.click();
                }
            };
            xhh.send();

        }
        //详情
        plgCard.on("run", function (rdata) {
            $("#treasury_div9").css("display", "block");
            $("#treasury_div1").css("display", "none");
            $("#treasury_div3").css("display", "none");
            var dataSelect = JSON.parse($("#kubie_select").attr("consignor"));
            new PlgPanel({
                renderer: "treasury_div422",
                skin: 2,
                header: {
                    isShow: true,
                    title: "基本信息"
                },
                defaultBody: {
                    layoutCol: 3,
                    valueBj: true,
                    cols: [
                        [{
                            label: "库别",
                            value: getSelectValue(dataSelect, rdata.storeroomcode)
                        },
                        {
                            label: "库别编号",
                            value: rdata.storeroomcode
                        },
                        {
                            label: "描述",
                            value: rdata.exsattr1
                        }]
                    ]
                }
            });
        });
        //修改
        plgCard.on("update", function (ind) {
            ddfrom.clear();
            updataData = ind;
            $("#treasury_title").html("修改");
            $("#treasury_div9").css("display", "none");
            $("#treasury_div1").css("display", "block");
            $("#treasury_div3").css("display", "none");
            ddfrom.setFormData(ind);
            message = 1;
        });
    }
    //跳转页面
    var carousel = layui.carousel;
    carousel.render({
        elem: "#treasury_div2",
        autoplay: false,
        index: 0,
        arrow: "none",
        indicator: "none",
        anim: "fade",
        width: "100%", //设置容器宽度
        height: "100%"
    });

    //返回
    $(document).on("click", "#treasury_return", function () {
        $("#treasury_div9").css("display", "none");
        $("#treasury_div1").css("display", "none");
        $("#treasury_div3").css("display", "block");
    });
    //新增或修改from页面
    ddfrom = $("#treasury_div4").initPlgForm({
        items: [
            { type: "settings", position: "label-left", width: 300, labelWidth: 80 },
            { type: "newcolumn", offset: 30 },
            {
                type: "combo", readonly: true, name: "storeroomcode", label: "库别 :", width: 260, options: JSON.parse($("#kubie_select").attr("consignor"))
            },
            { type: "newcolumn", offset: 10 },
            { type: "input", name: "exsattr1", label: "描述:", width: 260 }]
    });
    //保存
    document.querySelector("#treasury_save").onclick = function () {
        //数据data
        var data = ddfrom.getFormData();
        if (data.exsattr1.length > 30) {
            layer.msg("描述长度不能超过30");
            return;
        }
        //请求地址和类型
        var url = "/basic/storeroom/update";
        var type = "put";
        var contentType = "application/json";
        if (message == 1) {
            data.storeroomcodes = data.storeroomcode;
            data.storeroomcode = updataData.storeroomcode;
        }
        data.storeroomname = ddfrom.getCombo("storeroomcode").getComboText();
        //message新增为0，修改为1
        if (message == 0) {
            url = "/basic/storeroom/batch-create";
            contentType = "application/json";
            type = "post";
            data = [data];
        }
        //调用ajax方法
        utilAjaxGJ(url, type, JSON.stringify(data), function (data) {
            setTimeout(() => { PlgDialog.msg(data.message == null ? "接口调用失败" : data.message); }, 300);
            queryGj();
            $("#treasury_div9").css("display", "none");
            $("#treasury_div1").css("display", "none");
            $("#treasury_div3").css("display", "block");
        }, contentType);
    };
};