/**
 * 交付模块-标准预测计划供应商回复页面JS
 *
 * @version 1.0
 */

let currentRowNo = 0;
var page = {
    editIndex: undefined,
    bodyHeight: document.documentElement.clientHeight,
    /**
     * 时间选中下边记录
     */
    replySelectInfo: {index: null, rows: null},
    saveOrUpdate: function () {
        $('#tb').edatagrid("saveRow");
        page.replySelectInfo.rows = null;
        var rows = page.replyDataGrid.datagrid("getRows");
        var s=JSON.stringify(rows);
        var list = JSON.parse(s);
        var msg="";
        var pd=true;
        list.forEach(function(item) {
            var  rp= item.mainMaterialReply;
            if(rp!==''){
                var count=rp.split('*').length - 1;
                if(count>1  && rp.indexOf(";") === -1){
                    alert("请以英文 ; 符号进行拼接回复！");
                    pd= false;
                }
                var  rps = rp.split(';');
                if(rps!==''){
                    var znum=0;
                    rps.forEach(function(mrp) {
                        var sl = getDataAfterCharacter(mrp, '*');
                        var num = parseInt(sl);
                        znum=znum+num;
                    });
                    var  sq= parseInt(item.shortageQuantity);
                    var  cj=sq+znum;
                    if(cj<0){
                        msg=msg+"\n校验失败";
                        alert("回复数量要大于缺料数量！");
                        pd= false;
                    }
                }
            }
        });
        if( !pd){
            return false;
        }
        function getDataAfterCharacter(str, character) {
            const parts = str.split(character);
            return parts.length > 1 ? parts[1].trim() : '';
        }

        var loadingIndex = topWin.layer.msg(tipsi18n.tjz, {icon: 16, shade: 0});
        $.ajax({
            url: g.ctx + 'pc/order/shortagePlan/rest/saveReply',
            data: JSON.stringify(rows),
            contentType: 'application/json',
            type: 'POST',
            complete: function () {
                topWin.layer.close(loadingIndex);
            },
            success: function () {
                topWin.layer.alert(tipsi18n.bccg, {icon: 1}, function (index) {
                    page.replyDataGrid.datagrid('reload');
                    topWin.layer.close(index)
                });
            },
            error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    },
    // 采购或管理员登录，查询欠料计划回复的数据，邓京术
    initTable: function () {
        page.replyDataGrid = $('#tb').edatagrid({
            url: g.ctx + 'pc/order/shortagePlan/rest/getReplyData?planId=' + planId,
            method: 'GET',
            iconCls: 'icon-list',
            width: '100%',
            autoSave: true,
            height: (page.bodyHeight - 80),
            singleSelect: true,
            pagination: true,
            queryParams: {
                status: 0
            },
            loadMsg: itablei8n.dataLoading,
            rownumbers: true,
            remoteSort: true,
            fitColumns: false,
            nowrap: true,
            toolbar: [
                // {
                //     text: itablei8n.save,
                //     iconCls: 'icon-save',
                //     hidden: true,
                //     handler: function () {
                //         page.saveOrUpdate();
                //     }
                // }
            ],
            columns: [[
                {
                    field: 'ck',
                    checkbox: true
                },
                {
                    title: "欠料版本",
                    width: 100,
                    field: "shortageVersion",sortable: true
                },
                {
                    title: "工单号",
                    width: 100,
                    field: "workOrderNumber",sortable: true
                },
                {
                    title: "产品料号",
                    width: 100,
                    field: "productCode",sortable: true
                },
                {
                    title: "产品描述长文本",
                    width: 100,
                    field: "productLongName"
                },
                {
                    title: "工厂",
                    width: 100,
                    field: "factoryCode",sortable: true
                },
                {
                    title: "物料编号",
                    width: 100,
                    field: "materialCode",sortable: true
                },
                {
                    title: "物料描述（长描述）",
                    width: 100,
                    field: "materialLongName"
                },
                {
                    title: "物料组",
                    width: 100,
                    field: "materialGroup"
                },
                {
                    title: "物料组描述",
                    width: 100,
                    field: "materialGroupName"
                },
                {
                    title: "需求数量",
                    width: 100,
                    field: "demandQuantity",sortable: true
                },
                {
                    title: "物料异常",
                    width: 100,
                    field: "zyichng"
                },
                {
                    title: "现有量",
                    width: 100,
                    field: "currentQuantity",sortable: true
                },
                {
                    title: "冻结库存",
                    width: 100,
                    field: "frozenInventory",sortable: true
                },
                {
                    title: "不良品仓数量",
                    width: 100,
                    field: "defectiveWarehouseQuantity",sortable: true
                },
                {
                    title: "质量检验",
                    width: 100,
                    field: "qualityInspection",sortable: true
                },
                {
                    title: "质检冻结库存",
                    width: 100,
                    field: "checkFrozenInventory",sortable: false
                },
                {
                    title: "请购数量",
                    width: 100,
                    field: "purchaseRequestQuantity",sortable: true
                },
                {
                    title: "采购未交数量",
                    width: 100,
                    field: "undeliveredPurchaseQuantity",sortable: true
                },
                {
                    title: "缺料数量",
                    color: 13,
                    width: 100,
                    field: "shortageQuantity",sortable: true
                },
                {
                    title: "合计缺料数",
                    width: 100,
                    field: "totalShortageQuantity",sortable: true
                },
                {
                    title: "需求日期",
                    width: 100,
                    color: 13,
                    field: "demandDate",
                    exportDateFormatter: "yyyy-MM-dd",
                    formatter:function (value){
                        if(value!==''){
                            return moment(value).format("YYYY-MM-DD")
                        }
                        return value
                    },sortable: true
                },
                {
                    title: "上线日期",
                    width: 100,
                    field: "launchDate",
                    exportDateFormatter: "yyyy-MM-dd",
                    formatter:function (value){
                        if(value!==''){
                            return moment(value).format("YYYY-MM-DD")
                        }
                        return value
                    },sortable: true
                },
                {
                    title: "备注(上线日期)",
                    width: 100,
                    field: "launchDateRemark"
                },

                // /**
                //  *                 {
                //  *                     title: "<span style='color: red'>*</span>主料回复数量",
                //  *                     color: 13,
                //  *                     width: 100,
                //  *                     field: "mainMaterialReplyQuantity",
                //  *                     editor: {type: "numberbox", options: {min: 0, precision: 0}},sortable: true
                //  *                 },
                 {
                      title: "<span style='color: red'>*</span>主料回复日期<br/>(务必满足'缺料数量'栏数量)",
                      width: 180,
                      color: 13,
                      field: "mainMaterialReplyDate",
                      exportDateFormatter: "yyyy-MM-dd",
                      editor: {type: "datebox", options: {editable: true}},
                      formatter:function (value){
                          if(value!==''){
                              return moment(value).format("YYYY-MM-DD")
                          }
                          return value
                      },sortable: true
                  },

                {
                    title: "<span style='color: red'>*</span>分批交货交期回复<br/>（回复格式：YYYY-MM-DD*数量;YYYY-MM-DD*数量）",
                    width: 400,
                    color: 13,
                    field: "mainMaterialReply",
                    editor: {type: "textbox", options: {editable: true}},sortable: true

                },
                {
                    title: "<span style='color: red'>*</span>备注",
                    width: 100,
                    color: 13,
                    field: "remark",
                    editor: {type: "textbox", options: {editable: true}},sortable: true
                },

                {
                    title: "采购组",
                    width: 100,
                    field: "purchaseGroup",
                    formatter: function (value) {
                        return getNecessaryDataName('pc/sys/purchaseGroup/rest/listAll', value);
                    },sortable: true
                },
                {
                    title: "供应商",
                    width: 100,
                    field: "supplierName",sortable: true
                },
                {
                    title: "供应商编码",
                    width: 100,
                    field: "supplierCode",sortable: true
                },
                {
                    title: "后继物料",
                    width: 100,
                    field: "successorMaterial",sortable: true
                },
                {
                    title: "后继物料未交数量",
                    width: 100,
                    field: "successorMaterialUndeliveredQuantity",sortable: true
                },
                {
                    title: "<span style='color: red'>*</span>后继物料回复数量",
                    color: 13,
                    width: 100,
                    field: "successorMaterialReplyQuantity",
                    editor: {type: "numberbox", options: {min: 0, precision: 0}},sortable: true
                },
                {
                    title: "<span style='color: red'>*</span>后继物料交期回复",
                    width: 100,
                    color: 13,
                    field: "successorMaterialDeliveryDateReply",
                    exportDateFormatter: "yyyy-MM-dd",
                    editor: {type: "datebox", options: {editable: true}},
                    formatter:function (value){
                        if(value!==''){
                            return moment(value).format("YYYY-MM-DD")
                        }
                        return value
                    },sortable: true
                },
                {
                    title: "替代料1",
                    width: 100,
                    field: "substituteMaterial1",sortable: true
                },
                {
                    title: "替代料1库存",
                    width: 100,
                    field: "substituteMaterial1Inventory",sortable: true
                },
                {
                    title: "<span style='color: red'>*</span>替代料1回复数量",
                    color: 13,
                    width: 100,
                    field: "substituteMaterial1ReplyQuantity",
                    editor: {type: "numberbox", options: {min: 0, precision: 0}},sortable: true
                },
                {
                    title: "<span style='color: red'>*</span>替代料1交期回复",
                    width: 100,
                    color: 13,
                    field: "substituteMaterial1DeliveryDate",
                    exportDateFormatter: "yyyy-MM-dd",
                    editor: {type: "datebox", options: {editable: true}},
                    formatter:function (value){
                        if(value!==''){
                            return moment(value).format("YYYY-MM-DD")
                        }
                        return value
                    },sortable: true
                },
                {
                    title: "替代料2",
                    width: 100,
                    field: "substituteMaterial2",sortable: true
                },
                {
                    title: "替代料2库存",
                    width: 100,
                    field: "substituteMaterial2Inventory",sortable: true
                },
                {
                    title: "<span style='color: red'>*</span>替代料2回复数量",
                    color: 13,
                    width: 100,
                    field: "substituteMaterial2ReplyQuantity",
                    editor: {type: "numberbox", options: {min: 0, precision: 0}},sortable: true
                },
                {
                    title: "<span style='color: red'>*</span>替代料2交期回复",
                    width: 100,
                    color: 13,
                    field: "substituteMaterial2DeliveryDate",
                    exportDateFormatter: "yyyy-MM-dd",
                    editor: {type: "datebox", options: {editable: true}},
                    formatter:function (value){
                        if(value!==''){
                            return moment(value).format("YYYY-MM-DD")
                        }
                        return value
                    },sortable: true
                },
                {
                    title: "替代料3",
                    width: 100,
                    field: "substituteMaterial3",sortable: true
                },
                {
                    title: "替代料3库存",
                    width: 100,
                    field: "substituteMaterial3Inventory",sortable: true
                },
                {
                    title: "<span style='color: red'>*</span>替代料3回复数量",
                    color: 13,
                    width: 100,
                    field: "substituteMaterial3ReplyQuantity",
                    editor: {type: "numberbox", options: {min: 0, precision: 0}},sortable: true
                },
                {
                    title: "<span style='color: red'>*</span>替代料3交期回复",
                    width: 100,
                    color: 13,
                    field: "substituteMaterial3DeliveryDate",
                    exportDateFormatter: "yyyy-MM-dd",
                    editor: {type: "datebox", options: {editable: true}},
                    formatter:function (value){
                        if(value!==''){
                            return moment(value).format("YYYY-MM-DD")
                        }
                        return value
                    },sortable: true
                },
                {
                    title: "回复人",
                    width: 100,
                    field: "updateName"
                },
                {
                    title: "回复时间",
                    width: 100,
                    field: "updateTime",
                    exportDateFormatter: "yyyy-MM-dd"
                }
            ]]
        });
    },
    //如果是供应商登录，查询需要回复的欠料计划数据，邓京术
    initSuplierTable: function () {
        page.replyDataGrid = $('#tb').edatagrid({
            url: g.ctx + 'pc/order/shortagePlan/rest/getReplyData?planId=' + planId,
            method: 'GET',
            iconCls: 'icon-list',
            width: '100%',
            autoSave: true,
            height: (page.bodyHeight - 80),
            singleSelect: true,
            pagination: true,
            queryParams: {
                status: 0
            },
            loadMsg: itablei8n.dataLoading,
            rownumbers: true,
            remoteSort: true,
            fitColumns: false,
            nowrap: true,
            toolbar: [

            ],
            columns: [[
                {
                    field: 'ck',
                    checkbox: true
                },
                {
                    title: "欠料版本",
                    width: 100,
                    field: "shortageVersion",sortable: true
                },
                {
                    title: "工单号",
                    width: 100,
                    field: "workOrderNumber",sortable: true
                },
                {
                    title: "产品料号",
                    width: 100,
                    field: "productCode",sortable: true
                },
                {
                    title: "工厂",
                    width: 100,
                    field: "factoryCode",sortable: true
                },
                {
                    title: "物料编号",
                    width: 100,
                    field: "materialCode",sortable: true
                },
                {
                    title: "物料描述（长描述）",
                    width: 100,
                    field: "materialLongName"
                },
                {
                    title: "物料组",
                    width: 100,
                    field: "materialGroup"
                },
                {
                    title: "物料组描述",
                    width: 100,
                    field: "materialGroupName"
                },
                {
                    title: "需求数量",
                    width: 100,
                    field: "demandQuantity",sortable: true
                },
                {
                    title: "物料异常",
                    width: 100,
                    field: "zyichng"
                },
                // {
                //     title: "质检冻结库存",
                //     width: 100,
                //     field: "frozenInventory",sortable: true
                // },
                {
                    title: "不良品仓数量",
                    width: 100,
                    field: "defectiveWarehouseQuantity",sortable: true
                },
                {
                    title: "质量检验",
                    width: 100,
                    field: "qualityInspection",sortable: true
                },
                {
                    title: "质检冻结库存",
                    width: 100,
                    field: "checkFrozenInventory",sortable: false
                },
                {
                    title: "请购数量",
                    width: 100,
                    field: "purchaseRequestQuantity",sortable: true
                },
                {
                    title: "采购未交数量",
                    width: 100,
                    field: "undeliveredPurchaseQuantity",sortable: true
                },
                {
                    title: "缺料数量",
                    color: 13,
                    width: 100,
                    field: "shortageQuantity",sortable: true
                },
                {
                    title: "合计缺料数",
                    width: 100,
                    field: "totalShortageQuantity",sortable: true
                },
                {
                    title: "需求日期",
                    width: 100,
                    color: 13,
                    field: "demandDate",
                    exportDateFormatter: "yyyy-MM-dd",
                    formatter:function (value){
                        if(value!==''){
                            return moment(value).format("YYYY-MM-DD")
                        }
                        return value
                    },sortable: true
                },
                {
                    title: "上线日期",
                    width: 100,
                    field: "launchDate",
                    exportDateFormatter: "yyyy-MM-dd",
                    formatter:function (value){
                        if(value!==''){
                            return moment(value).format("YYYY-MM-DD")
                        }
                        return value
                    },sortable: true
                },
                {
                    title: "备注(上线日期)",
                    width: 100,
                    field: "launchDateRemark"
                },
                // {
                //     title: "<span style='color: red'>*</span>主料回复数量",
                //     color: 13,
                //     width: 100,
                //     field: "mainMaterialReplyQuantity",
                //     editor: {type: "numberbox", options: {min: 0, precision: 0}},sortable: true
                // },
                {
                    title: "<span style='color: red'>*</span>主料回复日期<br/>(务必满足'缺料数量'栏数量)",
                    width: 180,
                    color: 13,
                    field: "mainMaterialReplyDate",
                    exportDateFormatter: "yyyy-MM-dd",
                    editor: {type: "datebox", options: {editable: true}},
                    formatter:function (value){
                        if(value!==''){
                            return moment(value).format("YYYY-MM-DD")
                        }
                        return value
                    },sortable: true
                },
                {
                    title: "<span style='color: red'>*</span>分批交货交期回复<br/>(回复格式：YYYY-MM-DD*数量;YYYY-MM-DD*数量）",
                    width: 400,
                    color: 13,
                    field: "mainMaterialReply",
                    editor: {type: "textbox", options: {editable: true}},sortable: true

                },
                {
                    title: "<span style='color: red'>*</span>备注",
                    width: 100,
                    color: 13,
                    field: "remark",
                    editor: {type: "textbox", options: {editable: true}},sortable: true
                },

                {
                    title: "采购组",
                    width: 100,
                    field: "purchaseGroup",
                    formatter: function (value) {
                        return getNecessaryDataName('pc/sys/purchaseGroup/rest/listAll', value);
                    },sortable: true
                },
                {
                    title: "供应商",
                    width: 100,
                    field: "supplierName",sortable: true
                },
                {
                    title: "供应商编码",
                    width: 100,
                    field: "supplierCode",sortable: true
                },
                {
                    title: "后继物料",
                    width: 100,
                    field: "successorMaterial",sortable: true
                },
                {
                    title: "后继物料未交数量",
                    width: 100,
                    field: "successorMaterialUndeliveredQuantity",sortable: true
                },
                {
                    title: "<span style='color: red'>*</span>后继物料回复数量",
                    color: 13,
                    width: 100,
                    field: "successorMaterialReplyQuantity",
                    editor: {type: "numberbox", options: {min: 0, precision: 0}},sortable: true
                },
                {
                    title: "<span style='color: red'>*</span>后继物料交期回复",
                    width: 100,
                    color: 13,
                    field: "successorMaterialDeliveryDateReply",
                    exportDateFormatter: "yyyy-MM-dd",
                    editor: {type: "datebox", options: {editable: true}},
                    formatter:function (value){
                        if(value!==''){
                            return moment(value).format("YYYY-MM-DD")
                        }
                        return value
                    },sortable: true
                },
                {
                    title: "替代料1",
                    width: 100,
                    field: "substituteMaterial1",sortable: true
                },
                {
                    title: "替代料1库存",
                    width: 100,
                    field: "substituteMaterial1Inventory",sortable: true
                },
                {
                    title: "<span style='color: red'>*</span>替代料1回复数量",
                    color: 13,
                    width: 100,
                    field: "substituteMaterial1ReplyQuantity",
                    editor: {type: "numberbox", options: {min: 0, precision: 0}},sortable: true
                },
                {
                    title: "<span style='color: red'>*</span>替代料1交期回复",
                    width: 100,
                    color: 13,
                    field: "substituteMaterial1DeliveryDate",
                    exportDateFormatter: "yyyy-MM-dd",
                    editor: {type: "datebox", options: {editable: true}},
                    formatter:function (value){
                        if(value!==''){
                            return moment(value).format("YYYY-MM-DD")
                        }
                        return value
                    },sortable: true
                },
                {
                    title: "替代料2",
                    width: 100,
                    field: "substituteMaterial2",sortable: true
                },
                {
                    title: "替代料2库存",
                    width: 100,
                    field: "substituteMaterial2Inventory",sortable: true
                },
                {
                    title: "<span style='color: red'>*</span>替代料2回复数量",
                    color: 13,
                    width: 100,
                    field: "substituteMaterial2ReplyQuantity",
                    editor: {type: "numberbox", options: {min: 0, precision: 0}},sortable: true
                },
                {
                    title: "<span style='color: red'>*</span>替代料2交期回复",
                    width: 100,
                    color: 13,
                    field: "substituteMaterial2DeliveryDate",
                    exportDateFormatter: "yyyy-MM-dd",
                    editor: {type: "datebox", options: {editable: true}},
                    formatter:function (value){
                        if(value!==''){
                            return moment(value).format("YYYY-MM-DD")
                        }
                        return value
                    },sortable: true
                },
                {
                    title: "替代料3",
                    width: 100,
                    field: "substituteMaterial3",sortable: true
                },
                {
                    title: "替代料3库存",
                    width: 100,
                    field: "substituteMaterial3Inventory",sortable: true
                },
                {
                    title: "<span style='color: red'>*</span>替代料3回复数量",
                    color: 13,
                    width: 100,
                    field: "substituteMaterial3ReplyQuantity",
                    editor: {type: "numberbox", options: {min: 0, precision: 0}},sortable: true
                },
                {
                    title: "<span style='color: red'>*</span>替代料3交期回复",
                    width: 100,
                    color: 13,
                    field: "substituteMaterial3DeliveryDate",
                    exportDateFormatter: "yyyy-MM-dd",
                    editor: {type: "datebox", options: {editable: true}},
                    formatter:function (value){
                        if(value!==''){
                            return moment(value).format("YYYY-MM-DD")
                        }
                        return value
                    },sortable: true
                },
                {
                    title: "回复人",
                    width: 100,
                    field: "updateName"
                },
                {
                    title: "回复时间",
                    width: 100,
                    field: "updateTime",
                    exportDateFormatter: "yyyy-MM-dd"
                }

            ]]
        });
    },
    // 点击导入按钮事件，邓京术
    detailImport: function (v1, v2) {
        var filebox = $("#detailFileInput").filebox("files");
        if (filebox.length == 1) {
            var formData = new FormData();
            formData.append("file", filebox[0]);
            var index = topWin.layer.msg("数据导入中...", {icon: 16, time: 0, shade: 0.1});
            $.ajax({
                url: g.ctx + 'pc/order/shortagePlan/rest/replyImport',
                data: formData,
                type: 'post',
                contentType: false,
                processData: false,
                complete: function () {
                    topWin.layer.close(index);
                    $("#detailFileInput").filebox("clear");
                },
                success: function (data) {
                    debugger
                    //需要把数组data转换成以ID为key的对象
                    var dataMap = {};
                    for (var i = 0; i < data.length; i++) {
                        dataMap[data[i].id] = data[i];
                    }
                    let rows = $('#tb').datagrid("getRows")
                    for (var i = 0; i < rows.length; i++) {
                        var row = rows[i];
                        let rowIndex = $('#tb').datagrid("getRowIndex", row)
                        if (dataMap[row.id]) {
                            let data = dataMap[row.id]
                            if(data.mainMaterialReplyDate) {
                                data.mainMaterialReplyDate = moment(data.mainMaterialReplyDate).format("YYYY-MM-DD");
                            }
                            if(data.successorMaterialDeliveryDateReply) {
                                data.successorMaterialDeliveryDateReply = moment(data.successorMaterialDeliveryDateReply).format("YYYY-MM-DD");
                            }
                            if(data.substituteMaterial1DeliveryDate) {
                                data.substituteMaterial1DeliveryDate = moment(data.substituteMaterial1DeliveryDate).format("YYYY-MM-DD");
                            }
                            if(data.substituteMaterial2DeliveryDate) {
                                data.substituteMaterial2DeliveryDate = moment(data.substituteMaterial2DeliveryDate).format("YYYY-MM-DD");
                            }
                            if(data.substituteMaterial3DeliveryDate) {
                                data.substituteMaterial3DeliveryDate = moment(data.substituteMaterial3DeliveryDate).format("YYYY-MM-DD");
                            }

                            $('#tb').datagrid("updateRow", {
                                index: rowIndex, row: {
                                    // mainMaterialReplyQuantity: data.mainMaterialReplyQuantity,
                                    remark: data.remark,
                                    mainMaterialReply: data.mainMaterialReply,
                                    mainMaterialReplyDate:  data.mainMaterialReplyDate,
                                    successorMaterialReplyQuantity: data.successorMaterialReplyQuantity,
                                    successorMaterialDeliveryDateReply:  data.successorMaterialDeliveryDateReply,
                                    substituteMaterial1ReplyQuantity: data.substituteMaterial1ReplyQuantity,
                                    substituteMaterial1DeliveryDate:  data.substituteMaterial1DeliveryDate,
                                    substituteMaterial2ReplyQuantity: data.substituteMaterial2ReplyQuantity,
                                    substituteMaterial2DeliveryDate:  data.substituteMaterial2DeliveryDate,
                                    substituteMaterial3ReplyQuantity: data.substituteMaterial3ReplyQuantity,
                                    substituteMaterial3DeliveryDate:  data.substituteMaterial3DeliveryDate
                                }
                            });
                        }
                    }
                    debugger
                    if (data.length >= 10000) {
                        topWin.layer.close(index);
                        // topWin.layer.msg();
                        topWin.layer.alert("数据导入中,请稍候在文件操作菜单查询结果,结果未返回前,请勿重复导入!!!", {icon: 1});
                    } else {
                        topWin.layer.confirm('导入成功，是否提交?', {icon: 3}, function (idd) {
                            topWin.layer.close(index);
                            confirmReply2(function () {
                                $(el.tb).datagrid('load');
                            })
                        })
                    }
                },
                error: function (data) {
                    topWin.layer.alert(data.responseJSON.message, {icon: 2});
                }
            });
        }
    },
    /**
     * 悬浮显示较上次上升or下降
     *
     * @param dom
     */
    showChange: function (dom) {
        var upOrDown = $(dom).data('val');
        var preVal = $(dom).data('pre_val');
        var thisVal = $(dom).data('this_val');
        if (upOrDown === 'up') {
            layer.tips('<div><p style="text-align: center;font-size: 1.2em;">' + tipsi18n.shs + '</p><p>' + tipsi18n.sgbbhfsl + '：' + preVal + '</p><p>' + tipsi18n.dqbbhfsl + '：' + thisVal + '</p></div>', dom, {
                tips: [4, '#f54545'],
                time: 4000
            });
        } else {
            layer.tips('<div><p style="text-align: center;font-size: 1.2em;">' + tipsi18n.xj + '</p><p>' + tipsi18n.sgbbhfsl + '：' + preVal + '</p><p>' + tipsi18n.dqbbhfsl + '：' + thisVal + '</p></div>', dom, {
                tips: [4, '#090'],
                time: 4000
            });
        }

    }
};
$(function () {
    $('#status').combobox({
        valueField: 'value',
        textField: 'label',
        data: [
            {label: '全部', value: '-1'},
            {label: '待回复', value: '0'},
            {label: '已回复', value: '1'},
        ],
        editable: false,
        required: true,
        value: '0',
        panelHeight: 'auto',
        prompt: '-- 请选择 --'
    });
    /**
     * 导出
     */
    $('#export_btn').on('click', function () {
        // 检查按钮是否被禁用
        if ($(this).linkbutton('options').disabled) {
            return;
        }

        // 保存表格当前编辑的行
        $('#tb').edatagrid("saveRow");
        // 组装请求参数
        var params = searchParams();
        params.planId = planId;
        var tableData = getExcelKeyTitle("tb");
        xhrPostExport('pc/order/shortagePlan/rest/exportReplyData',  params,tableData)

    });

    /**
     * 导入
     */
    $('#import_btn').on('click', function () {
        if ($(this).linkbutton('options')['disabled']) {
            return
        }
        $("#detailUpload .filebox-label").click();
    });
    initNecessaryDatas('pc/sys/purchaseGroup/rest/listAll')

    if (isSupplier) {
        page.initSuplierTable();
    } else {
        page.initTable();
    }

});

//回复页面点击保存并提交时的事件 邓京术
function confirmReply(callback) {
    // 结束行编辑
    // $('#tb').edatagrid("saveRow")
    page.replySelectInfo.rows = null;
    page.replyDataGrid.edatagrid("saveRow");
    var rows = page.replyDataGrid.datagrid("getRows");
    //var loadingIndex = topWin.layer.msg(tipsi18n.tjz, {icon: 16, shade: 0});
    var s=JSON.stringify(rows);
    var list = JSON.parse(s);
    var msg="";
    var pd=true;
    list.forEach(function(item) {
        var  rp= item.mainMaterialReply;
        if(rp!==''){
            var count=rp.split('*').length - 1;
            if(count>1  && rp.indexOf(";") === -1){
                alert("请以英文 ; 符号进行拼接回复！");
                pd= false;
            }
            var  rps = rp.split(';');
            if(rps!==''){
                var znum=0;
                rps.forEach(function(mrp) {
                    var sl = getDataAfterCharacter(mrp, '*');
                    var num = parseInt(sl);
                    znum=znum+num;
                });
                var  sq= parseInt(item.shortageQuantity);
                var  cj=sq+znum;
                if(cj<0){
                    msg=msg+"校验失败";
                    alert("回复数量要大于缺料数量！");
                    pd= false;
                }
            }
        }
    });
    if(!pd){
        return false;
    }

    function getDataAfterCharacter(str, character) {
        const parts = str.split(character);
        return parts.length > 1 ? parts[1].trim() : '';
    }

    $.ajax({
        url: g.ctx + 'pc/order/shortagePlan/rest/saveReply',
        data: JSON.stringify(rows),
        contentType: 'application/json',
        type: 'POST',
        complete: function () {
            //topWin.layer.close(loadingIndex);
        },
        success: function () {
            confirmReply1();
        },
        error: function (data) {
            topWin.layer.alert(data.responseJSON.message, {icon: 2});
        }
    });
}

// 页面点击保存并提交事件，先保存成功后在提交的操作，邓京术 非导入
function confirmReply1() {
    //var loadingIndex = topWin.layer.msg(tipsi18n.tjz, {icon: 16, shade: 0});
    $.ajax({
        url: g.ctx + 'pc/order/shortagePlan/rest/confirmReply',
        data: JSON.stringify(searchParams()),
        contentType: 'application/json',
        type: 'POST',
        complete: function () {
            //topWin.layer.close(loadingIndex);
        },
        success: function () {
            topWin.layer.alert('操作成功', {icon: 1}, function (index) {
                page.replyDataGrid.datagrid('reload');
                topWin.layer.close(index)
            });
        },
        error: function (data) {
            topWin.layer.alert(data.responseJSON.message, {icon: 2});
        }
    });
}
// 欠料计划回复页面点击导入成功后，确认提交给sap,邓京术
function confirmReply2() {
    //var loadingIndex = topWin.layer.msg(tipsi18n.tjz, {icon: 16, shade: 0});
    $.ajax({
        url: g.ctx + 'pc/order/shortagePlan/rest/confirmReplyImport',
        // data: '',
        contentType: 'application/json',
        type: 'POST',
        complete: function () {
            //topWin.layer.close(loadingIndex);
        },
        success: function () {
            topWin.layer.alert('操作成功', {icon: 1}, function (index) {
                page.replyDataGrid.datagrid('reload');
                topWin.layer.close(index)
            });
        },
        error: function (data) {
            topWin.layer.alert(data.responseJSON.message, {icon: 2});
        }
    });
}

function doResetSearch() {
    $('#searchForm').form("clear");
    $('input').not('[type="radio"]').val('');
    $('.Wdate').val('');
    $('.select2').val('0');
    //遍历所有select2重置
    $('.select2').each(function () {
        $(this).val(null).trigger("change");
    });
    $('#planId').val(planId);
    $('#tb').datagrid('options').queryParams = $('#searchForm').serializeJson();
    $('#tb').datagrid('load');
}





