/**
   * smy
   * 2021.09.03
   * 维修厂管理
      */
    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 openType;
    // 获取默认时间
    var startDate = Prolog.formatDate(new Date(), "%YY-%MM") + "-01 00:00:00";
    var endDate = Prolog.formatDate(new Date()) + " 23:59:59";

    let ddfrom;
    //跳转页面
    var carousel = layui.carousel;
    var car = carousel.render({
        elem: "#MPM_div",
        autoplay: false,
        index: 0,
        arrow: "none",
        indicator: "none",
        anim: "fade",
        width: "100%", //设置容器宽度
        height: "100%"
    });
    //跳转方法
    var tt = "MPM_zhu_div";

    function changePanel() {
        car.slide(tt);
        tt = tt == "MPM_xi1" ? "MPM_zhu_div" : "MPM_xi1";
    }
    var grid;

    // 配送中心table
    var branchTable = {
        // terrace显示全部，card分页显示
        skin: "card",
        // renderer:"grid",
        columns: [
            { id: "companyid", name: "id", type: "ro", width: "130", hidden: true },
            { id: "companycode", name: "配送中心编号", type: "ro" },
            { id: "companyname", name: "配送中心名称", type: "ro" }
        ],
        multiselect: false,
        toolbar: [
            { type: "settings", position: "label-left", width: 200 },
            {
                type: "block", id: "bk", blockOffset: 0, list: [
                    { type: "input", name: "companycode", label: "配送中心编号:", width: 150 },
                    { type: "newcolumn" },
                    { type: "button", name: "querycarr", value: "查询" },
                    { type: "newcolumn" },
                    { type: "button", name: "car_Reset", value: "重置" }
                ]
            }
        ],

        url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/pubCompany/queryCompanyBranch",
        pagebar: { type: "simple" },
        type: "post", // 数据提交方式，默认为get
        params: JSON.stringify({ companytype: "S" }), // 查询提交参数，分页参数默认为pageNum,pageSize
        contentType: "application/json",
        page: true, // 是否启用分页
        totalCount: "totalCount" // 总记录数字段，默认totalCount
    };

    //初始加载表格
    gridInit(0);

    function gridInit(type, uData) {
        if (type == 0) {
            //分页参数
            var opts = {
                //terrace显示全部，card分页显示
                skin: "card",
                //renderer:"grid",
                columns: [
                    { id: "gsId", name: "id", type: "ro", width: "100", hidden: true },
                    { id: "gsCode", name: "维修厂编号", type: "ro", width: "150" },
                    { id: "gsName", name: "维修厂名称", type: "ro", width: "150" },
                    { id: "zjm", name: "助记码", type: "ro", width: "150" },
                    { id: "gsShortname", name: "简称", type: "ro", width: "150" },
                    { id: "contactman", name: "联系人", type: "ro", width: "150" },
                    { id: "contacttel1", name: "联系电话", type: "ro", width: "150" },
                    { id: "address", name: "地址", type: "ro", width: "200" },
                    { id: "createtime", name: "录入时间", type: "ro", width: "150" },
                    { id: "createperson", name: "录入人", type: "ro", width: "150" },
                    { id: "memo", name: "备注", type: "ro", width: "150" },
                    {
                        id: "op", name: "操作", type: "button", align: "left", fixed: "right", width: "100", render: function () {
                            return "作废^MPM_tovoid_btn,修改^MPM_modify_btn,复制^MPM_copy_btn,详情^MPM_details_btn";
                        }
                    }
                ],
                multiselect: false,
                fixedLast: true,
                toolbar: [
                    { type: "settings", labelWidth: 150, labelAlign: "right" },
                    {
                        type: "row",
                        list: [
                            { type: "calendar", name: "startDate", className: "layui-col-md4", label: "录入时间查询开始:", value: startDate },
                            { type: "calendar", name: "endDate", className: "layui-col-md4", label: "录入时间查询结束:", value: endDate },
                            { type: "input", name: "zjm", className: "layui-col-md4", label: "助记码:" },
                            { type: "input", name: "gsCode", className: "layui-col-md4", label: "维修厂编号:" },
                            { type: "input", name: "gsName", className: "layui-col-md4", label: "维修厂名称:" },
                            {
                                type: "block", className: "layui-col-md6 form_btn_left",
                                list: [
                                    { type: "button", className: "btn-normal", name: "querycarr", value: "查询" },
                                    { type: "newcolumn" },
                                    { type: "button", name: "car_Reset", className: "normal", value: "重置" }
                                ]
                            },
                            {
                                type: "block", className: "layui-col-md6 right",
                                list: [
                                    { type: "button", className: "btn-addBtn btn-normal", name: "grid_add_btn", value: "新增" }
                                ]
                            }
                        ]
                    }],

                url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/partnerServingStation/query",//数据接口
                type: "post",//数据提交方式，默认为get
                contentType: "application/json",
                params: JSON.stringify({ startDate: startDate, endDate: endDate }),//查询提交参数，分页参数默认为pageNum,pageSize
                page: true, //是否启用分页
                totalCount: "totalCount" //总记录数字段，默认totalCount
            };
            //分页参数 pageSize pageNum
            grid = new PlgGrid(opts);
            grid.renderTo("MPM_zhu");
            // grid.getToolBarForm().setFormData({ "systemId": "prolog-healthy-wms" });
            grid.loadData();
            //按钮添加方法
            grid.attachToolBarEvent("onButtonClick", function (name) {
                //查询
                if (name == "querycarr") {
                    let jsonData = grid.getToolBarForm().getFormData();
                    jsonData.startDate = jsonData.startDate ? Prolog.formatDate(jsonData.startDate) + " 00:00:00" : "";
                    jsonData.endDate = jsonData.endDate ? Prolog.formatDate(jsonData.endDate) + " 23:59:59" : "";
                    if (jsonData.startDate > jsonData.endDate) {
                        layer.msg("开始时间不能大于结束时间");
                        return false;
                    }
                    grid.setParams(JSON.stringify(jsonData));
                    grid.reload();
                }
                //重置
                if (name == "car_Reset") {
                    grid.getToolBarForm().clear();
                    grid.getToolBarForm().setFormData({ startDate: startDate, endDate: endDate });
                }
                //新增
                if (name == "grid_add_btn") {
                    gridInit(1);
                }
            });
        }
        if (type == 1) {
            $("#MPM_xi").html("");
            if (uData && uData.gsId) {
                $("#MPMErr").html("修改");
            }
            else {
                $("#MPMErr").html("新增");
            }
            ddfrom = new PlgForm({
                renderer: "MPM_xi",
                items: [
                    { type: "settings", labelWidth: 80, labelAlign: "right" },
                    {
                        type: "row",
                        list: [
                            { type: "hidden", name: "gsId" },

                            { type: "input", name: "gsCode", className: "layui-col-md4", label: "维修厂编号:", required: true },
                            { type: "input", name: "gsName", className: "layui-col-md4", label: "维修厂名称:", required: true },

                            {
                                type: "selectTable",
                                className: "layui-col-md4",
                                placeholder: "请选择配送中心",
                                readonly: false,
                                table: branchTable,
                                tableWidth: 600,
                                tableHeight: 500,
                                fieldText: "companyname",
                                fieldValue: "companyid",
                                name: "branchidList",
                                label: "配送中心:<span class='dhxform_item_required'>*<span>",
                                listener: {
                                    "init": function (Grid) {
                                        // 注册按钮事件
                                        Grid.attachToolBarEvent("onButtonClick", function (name) {
                                            switch (name) {
                                                case "querycarr":
                                                    var request = Grid.getToolBarForm().getFormData();
                                                    Grid.setParams(JSON.stringify(request));
                                                    Grid.reload();
                                                    break;
                                                case "car_Reset":
                                                    Grid.getToolBarForm().clear();
                                                    break;
                                            }
                                        });
                                    },
                                    "afterSelect": function (isSelected, selectId, sgrid) {
                                        let sdata = sgrid.getRowData(selectId);
                                        ddfrom.setFormData({
                                            branchid: isSelected ? sdata.companyid : "",
                                            branchcode: isSelected ? sdata.companycode : "",
                                            branchname: isSelected ? sdata.companyname : ""
                                        });
                                    }
                                }
                            },
                            { type: "hidden", name: "branchid" },
                            { type: "hidden", name: "branchcode" },
                            { type: "hidden", name: "branchname" },

                            { type: "input", name: "zjm", className: "layui-col-md4", label: "助记码:" },
                            { type: "input", name: "gsShortname", className: "layui-col-md4", label: "简称:" },
                            { type: "input", name: "contactman", className: "layui-col-md4", label: "联系人:" },
                            { type: "input", name: "contacttel1", className: "layui-col-md4", label: "联系电话:" },
                            { type: "input", name: "bankName", className: "layui-col-md4", label: "开户银行:" },
                            { type: "input", name: "bankAccount", className: "layui-col-md4", label: "银行账户:" },
                            { type: "input", name: "invoiceTitle", className: "layui-col-md4", label: "发票抬头:" },
                            { type: "input", name: "address", className: "layui-col-md8", label: "地址:", required: true },
                            { type: "input", name: "memo", className: "layui-col-md12", label: "备注:" }

                        ]
                    }
                ]
            });
            if (uData) {
                ddfrom.setFormData(uData);
                ddfrom.getDForm().setText("branchidList", uData.branchname);
            }
            changePanel();
        }

    }
    // 作废
    window.MPM_tovoid_btn = function (rid, rdata) {
        //询问框
        PlgDialog.confirm("是否作废维修厂？", {
            title: "作废提示",
            btn: ["确定", "取消"]
        }, function (index) {
            PlgDialog.close(index);

            //加载等待
            var pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
            Prolog.ajax({
                url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/partnerServingStation/cancel",
                type: "post",
                contentType: "application/json",
                data: JSON.stringify([{
                    gsId: rid
                }]),
                success: function (data) {
                    PlgDialog.close(pload);
                    if (data.success) {
                        PlgDialog.msg(data.message);
                        setTimeout(function () {
                            grid.reload();
                        }, 10);
                    }
                    else {
                        setTimeout(() => { PlgDialog.alert(data.message == null ? "接口调用失败" : data.message); }, 300);
                    }
                },
                error: function () { PlgDialog.close(pload); PlgDialog.msg("接口调用失败"); }
            });

        });

    };
    // 修改
    window.MPM_modify_btn = function (rid, rdata) {
        openType = "";

        gridInit(1, rdata);

    };
    // 复制
    window.MPM_copy_btn = function (rid, rdata) {
        openType = "";

        rdata.gsId = null;
        gridInit(1, rdata);
    };
    // 详情
    window.MPM_details_btn = function (rid, rdata) {
        openType = 4;

        $("#MPM_xi").html("");
        $("#MPMErr").html("详情");
        $("#MPM_save").css("display", "none");

        new PlgPanel({
            renderer: "MPM_xi",
            skin: 2,
            header: {
                isShow: true,
                title: "详情"
            },
            defaultBody: {
                layoutCol: 3,
                valueBj: true,
                cols: [
                    [
                        {
                            label: "维修厂编号",
                            value: rdata.gsCode
                        },
                        {
                            label: "维修厂名称",
                            value: rdata.gsName
                        },
                        {
                            label: "配送中心",
                            value: rdata.branchname
                        },
                        {
                            label: "助记码",
                            value: rdata.zjm
                        }
                    ],
                    [
                        {
                            label: "简称",
                            value: rdata.gsShortname
                        },
                        {
                            label: "联系人",
                            value: rdata.contactman
                        },
                        {
                            label: "联系电话",
                            value: rdata.contacttel1
                        }

                    ],
                    [
                        {
                            label: "开户银行",
                            value: rdata.bankName
                        },
                        {
                            label: "银行账户",
                            value: rdata.bankAccount
                        },
                        {
                            label: "发票抬头",
                            value: rdata.invoiceTitle
                        }
                    ],
                    [
                        {
                            label: "地址",
                            value: rdata.address
                        }
                    ],
                    [
                        {
                            label: "备注",
                            value: rdata.memo
                        }
                    ]
                ]
            }
        });
        changePanel();

    };

    //返回
    document.querySelector("#MPM_back1").onclick = function () {
        $("#MPM_save").css("display", "block");
        changePanel();
    };
    //保存
    document.querySelector("#MPM_save").onclick = function () {
        //信息比对
        let url = "";
        //数据修改
        let feik = ddfrom.validate();
        if (!feik) {
            return;
        }
        let datai = ddfrom.getFormData();
        delete datai.branchidList;

        // 新增
        if (!datai.gsId) {
            url = UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/partnerServingStation/add";
        }
        else {
            // 修改
            url = UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/partnerServingStation/update";
        }
        //调用ajax方法
        utilAjaxGJ(url, "post", JSON.stringify(datai), function (data) {
            if (data.success) {
                        grid.reload();
                changePanel();
            }
        }, "application/json");
    };


};