/**
 *
 * @author BOGON
 * @version 1.0
 */

$(function () {
    initDict("po_item_type")
    initNecessaryDatas("pc/sys/factory/rest/allList,pc/sys/purchaseGroup/rest/listAll")
    initDataGrid();
});
var reloadOk = false;
var submitFlag = false;
let el = {
    tb: '#tb',
    searchForm: '#searchForm',
    bodyHeight: document.documentElement.clientHeight,
    formDialog: null,
    replayTableHidden: false, // 采购确认列表隐藏
    deliveryTableHidden: true, //送货单创建列表隐藏
    materialName: '#materialName',
    orderCode: '#orderCode',
    zlsdh: '#zlsdh',
    mrp: '#mrp',
    createName: '#createName',
    viewCode: '#viewCode',
    supplierErpCode: '#supplierErpCode',
    startCreateDate: '#startCreateDate',
    supplierName: '#supplierName',
    endCreateDate: '#endCreateDate',
    factoryCode: '#factoryCode',
    supplierCode1:'#supplierCode1',
    poType: '#poType',
    editIndex: undefined,
    editData: {},
    changQu: []
};

function printById(ids) {
    var iframe = document.getElementById('printOrder');
    iframe.src = g.ctx + 'pc/order/deliveryOrder/page/print?ids=' + ids;
}

function initDataGrid() {
    let toolbar = [];
    // 送货单创建列表
    el.replayTableHidden = true;
    el.deliveryTableHidden = false;
    toolbar = [{
        text: '导出', id: 'btn_export', iconCls: 'icon-download', handler: function () {
            let exportFields = [
                {
                    field: 'rowTypeCode', title: '项目类别', width: '100', align: 'left',
                    exportEnum: getDictList('po_item_type'),
                    formatter: function (value) {
                        return getDicName("po_item_type", value);
                    }
                }, {
                    field: 'purchaseGroupCode',
                    title: '采购组编码',
                    width: 100
                }, {
                    field: 'orderCode', title: 'SAP订单号',
                    width: '100', align: 'left'
                },
                {
                    field: 'orderRow', title: '订单行号',
                    width: '60', align: 'left'
                },
                {
                    field: 'orderDate', title: '订单凭证日期',
                    width: '85', align: 'left'
                },
                {
                    field: 'materialCode', title: '物料编码',
                    width: '100', align: 'left'
                },
                {
                    field: 'materialName', title: '物料名称',
                    width: '200', align: 'left'
                },
                {
                    field: 'unitCode', title: '订单单位',
                    width: '80', align: 'left',
                }, {
                    field: 'markFlag', title: '交货类型', width: '90', align: 'left',
                    exportEnum: [
                        {code: 'B', codeWithDesc: '计划交货'},
                        {code: 'A', codeWithDesc: '订单交货'}
                    ]
                },
                {
                    field: 'num', title: '订单数量',
                    width: '100', align: 'left',
                },
                {
                    field: 'receivedNum', title: '接收数量',
                    width: '100', align: 'left',
                }, {
                    field: 'noClearNum', title: '未清数量',
                    width: '100', align: 'left',
                },
                {
                    field: 'onlineNum', title: '在途送货单',
                    width: '100', align: 'left',
                },
                {
                    field: 'unlockNum', title: '解锁数量',
                    width: '100', align: 'left'
                },
                {
                    field: 'unlockRemark', title: '解锁备注',
                    width: '100', align: 'left',
                },
                {
                    field: 'enableNum', title: '可送数量',
                    width: '100', align: 'left',
                },
                {
                    field: 'deliveryNum', title: '送货数量',
                    width: '100', align: 'left',
                    color: 13
                },
                {
                    field: 'batchNum', title: '供应商批号',
                    color: 13,
                    width: '100', align: 'left', editor: {type: 'textbox'}
                },
                {
                    field: 'requireDate', title: '需求交期',
                    width: '100', align: 'left',
                },
                {
                    field: 'replayRequireDate', title: '回复日期',
                    width: '100', align: 'left',
                    exportDateFormatter: "yyyy-MM-dd"
                },
                {
                    field: 'factoryName', title: '收货工厂',
                    width: '120', align: 'left'
                },
                {
                    field: 'supplierName', title: '供应商名称', width: '100', align: 'left'
                },
                {
                    field: 'remark', title: '送货备注',
                    color: 13,
                    width: '200', align: 'left'
                }
            ]
            xhrPostExport('pc/order/purchaseOrderDetail/rest/exportForDeliveryCreate', searchParams(), exportFields);
        }
    }, {
        id: 'createDeliveryOrder_btn', text: '创建送货单', iconCls: 'icon-redo', handler: function () {
            debugger;
            createDeliveryOrder(0)
        }
    }, {
        id: 'unlock_btn', text: '解锁', iconCls: 'icon-lock', handler: function () {
            $(el.tb).edatagrid("saveRow")
            var datagrid = $(el.tb);
            if (submitFlag) {
                return false
            }
            var rows = $(el.tb).datagrid('getChecked');
            if (rows.length === 0) {
                topWin.layer.alert(tipsi18n.xzjlcz, {icon: 0});
                return false;
            }
            let postRows = []
            let tipMsg = [];
            $.each(rows, function (idx, itm) {
                if (itm.markFlag === 'A') {
                    tipMsg.push(itm.orderCode + "-" + itm.orderRow + "不是计划交货")
                } else {
                    if (itm.unlockNum === '') {
                        tipMsg.push(itm.orderCode + "-" + itm.orderRow + "解锁数量不能为空")
                    }
                }
            })

            if (tipMsg.length > 0) {
                topWin.layer.alert(tipMsg.join("</br>"), {icon: 0});
                return false;
            }
            //如果是单个解锁，弹窗输入解锁原因
            if (rows.length === 1) {
                $.each(rows, function (idx, itm) {
                    if (itm.unlockNum === 0) {
                        tipMsg.push(itm.orderCode + "-" + itm.orderRow + "解锁数量不能为0")
                    }
                })
                if (tipMsg.length > 0) {
                    topWin.layer.alert(tipMsg.join("</br>"), {icon: 0});
                    return false;
                }
                topWin.layer.prompt({
                    title: '请输入解锁原因',
                    formType: 2,
                    value: rows[0].unlockRemark
                }, function (value, index, elem) {
                    $.each(rows, function (idx, itm) {
                        if (itm.unlockNum === '') {
                            // tipMsg.push(itm.orderCode + "-" + itm.orderRow + "数量不能为空")
                        } else {
                            postRows.push({
                                id: itm.id, unlockNum: itm.unlockNum, unlockRemark: value
                            })
                            var columns = datagrid.datagrid("options").columns;
                            rows[idx][columns[0][9].field] = itm.unlockNum;
                            datagrid.datagrid('refreshRow',idx);
                            // deliveryNum = itm.unlockNum;
                            // // 刷新缓存数据
                            // var dataList = JSON.parse(localStorage.getItem("deliveryOrderRows"))
                            // for (var i=0; i< dataList.length;i++) {
                            //     var item = dataList[i];
                            //     if (item.id == itm.id) {
                            //         dataList[i].deliveryNum = itm.unlockNum;
                            //         break;
                            //     }
                            // }
                            // localStorage.setItem("deliveryOrderRows", JSON.stringify(dataList));
                        }
                    });

                    var loading = top.layer.msg('解锁中', {icon: 16, shade: [0.1, '#000'], time: false});
                    submitFlag = true;
                    $.ajax({
                        url: g.ctx + 'pc/order/purchaseOrderDetail/rest/unlock',
                        method: "POST",
                        contentType: 'application/json',
                        data: JSON.stringify(postRows),
                        complete: function (data) {
                            submitFlag = false
                            top.layer.close(loading);
                            topWin.layer.close(index);
                        },
                        success: function (data) {
                            topWin.layer.msg("操作成功", {icon: 1});
                            top.layer.close(loading);
                            topWin.layer.close(index);
                            $(el.tb).datagrid('reload');
                        },
                        error: function (data) {
                            topWin.layer.msg(data.responseJSON.message, {icon: 2});
                        }
                    });
                });
            } else {
                $.each(rows, function (idx, itm) {
                    if (itm.markFlag === 'A') {
                        // tipMsg.push(itm.orderCode + "-" + itm.orderRow + "不是计划交货")
                    } else {
                        if (itm.unlockNum === '') {
                            // tipMsg.push(itm.orderCode + "-" + itm.orderRow + "数量不能为空")
                        } else {
                            postRows.push({
                                id: itm.id, unlockNum: itm.unlockNum, unlockRemark: itm.unlockRemark
                            })
                            var columns = datagrid.datagrid("options").columns;
                            rows[idx][columns[0][9].field] = itm.unlockNum;
                            datagrid.datagrid('refreshRow',idx);
                        }
                    }
                })

                var loading = top.layer.msg('解锁中', {icon: 16, shade: [0.1, '#000'], time: false});
                submitFlag = true;
                $.ajax({
                    url: g.ctx + 'pc/order/purchaseOrderDetail/rest/unlock',
                    method: "POST",
                    contentType: 'application/json',
                    data: JSON.stringify(postRows),
                    complete: function (data) {
                        submitFlag = false
                        top.layer.close(loading);
                    },
                    success: function (data) {
                        topWin.layer.msg("操作成功", {icon: 1});
                        $(el.tb).datagrid('reload');
                    },
                    error: function (data) {
                        topWin.layer.msg(data.responseJSON.message, {icon: 2});
                    }
                });
            }

            if (postRows.length == 0) {
                //topWin.layer.alert("请选择要解锁的订单", {icon: 0});
                //return false;
            }
        }
    }, {
        text: '解锁数据导出', id: 'btn_unlock_export', iconCls: 'icon-download', handler: function () {
            let exportFields = [
                {
                    field: 'orderCode', title: 'SAP订单号',
                    width: '100', align: 'left'
                },
                {
                    field: 'orderRow', title: '订单行号',
                    width: '60', align: 'left'
                },
                {
                    field: 'orderDate', title: '订单凭证日期',
                    width: '85', align: 'left'
                },
                {
                    field: 'materialCode', title: '物料编码',
                    width: '100', align: 'left'
                },
                {
                    field: 'materialName', title: '物料名称',
                    width: '200', align: 'left'
                },
                {
                    field: 'factoryCode', title: '收货工厂',
                    width: '200', align: 'left'
                },
                {
                    field: 'noClearNum', title: '未清数量', hidden: el.deliveryTableHidden, width: '80', align: 'left',
                },
                {
                    field: 'requireDate', title: '需求交期', width: '80', align: 'left',
                },
                {
                    field: 'onlineNum', title: '在途送货单', hidden: el.deliveryTableHidden, width: '80', align: 'left',
                }, {
                    field: 'enableNum', title: '可送数量', hidden: el.deliveryTableHidden, width: '80', align: 'left',
                },{
                    field: 'unlockNum', title: '解锁数量',
                    width: '100', align: 'left',
                    color: 13
                },
                {
                    field: 'unlockRemark', title: '解锁备注',
                    width: '100', align: 'left',
                    color: 13
                }
            ]

            //采购修改为按查询条件导出， 2024-08-28 邓京术
            let query = searchParams();
            query['exportType'] = 1;
            xhrPostExport('pc/order/purchaseOrderDetail/rest/exportForDeliveryCreate', query, exportFields);

            //只能按勾选导出
            // var rows = $(el.tb).datagrid('getChecked');
            // if (rows.length === 0) {
            //     topWin.layer.alert("请选择需要导出的解锁数据", {icon: 0});
            //     return false;
            // }
            // let postRows = []
            // $.each(rows, function (idx, itm) {
            //     postRows.push({
            //         id: itm.id, unlockNum: itm.unlockNum, unlockRemark: itm.unlockRemark
            //     })
            // })
            // xhrPostExport('pc/order/purchaseOrderDetail/rest/exportUnlockForDeliveryCreate', searchParams(), exportFields);
        }
    }, {
        id: 'unlock_import_btn', text: '导入解锁', iconCls: 'icon-lock', handler: function () {
            $(el.tb).edatagrid("saveRow")
            $("#detailUpload .filebox-label").click();
        }
    }, {
        id: 'btn_clear', text: '清除已选', iconCls: 'icon-cancel', handler: function () {
            topWin.layer.confirm('是否确认清除已选的列？', {icon: 3}, function (iid) {
                $(el.tb).datagrid('clearChecked');
                topWin.layer.close(iid);
            })
        }
    }, {
        id: 'createDeliveryOrder_overseas_btn', text: '批量导入送货单 (境外)', iconCls: 'icon-redo', handler: function () {
            createDeliveryOrder(1)
        }
    }]

    let searchHeight = $(el.searchForm).outerHeight();
    let pager = initGridPagerNew(el.bodyHeight - searchHeight, 37, 40);
    $(el.tb).edatagrid({
        method: "GET",
        url: g.ctx + 'pc/order/purchaseOrderDetail/rest/listForDeliveryCreate',
        loadMsg: '数据远程载入中,请等待...',
        pagination: true,
        idField: 'id',
        pageSize: pager.pageSize,
        pageList: pager.pageList,
        checkbox: true,
        singleSelect: false,
        height: el.bodyHeight - searchHeight,
        frozenColumns: [[{field: 'ck', checkbox: true},
            {
                field: 'rowTypeCode', title: '项目类别', width: '100', align: 'left',
                exportEnum: getDictList('po_item_type'),
                formatter: function (value) {
                    return getDicName("po_item_type", value);
                }
            }, {
                field: 'purchaseGroupCode',
                title: '采购组编码',
                width: 100,
                exportEnum: getNecessaryDataList('pc/sys/purchaseGroup/rest/listAll'),
                formatter: function (value) {
                    return getNecessaryDataName('pc/sys/purchaseGroup/rest/listAll', value);
                }
            }, {
                field: 'orderCode', title: 'SAP订单号', width: '100', align: 'left', sortable: true
            }, {
                field: 'orderRow', title: '订单行号', width: '60', align: 'left'
            }, {
                field: 'orderDate', title: '订单凭证日期',
                width: '85', align: 'left'
            },{
                field: 'materialCode', title: '物料编码', width: '100', align: 'left', sortable: true
            }, {
                field: 'materialName', title: '物料名称', width: '200', align: 'left'
            }

        ]],
        columns: [[{
            field: 'unitCode', title: '订单单位', width: '100'
        }, {
            field: 'num', title: '订单数量', width: '80', align: 'left'
        }, {
            field: 'receivedNum', title: '接收数量',
            width: '100', align: 'left',
        }, {
            field: 'noClearNum', title: '未清数量', hidden: el.deliveryTableHidden, width: '80', align: 'left',
        }, {
            field: 'onlineNum', title: '在途送货单', hidden: el.deliveryTableHidden, width: '80', align: 'left',
        }, {
            field: 'unlockNum',
            title: '解锁数量',
            width: '80',
            align: 'left', sortable: true,
            editor: {type: 'numberbox', options: {validType: ['num'], editable: isSupplier === 1 ? false : true}}
        }, {
            field: 'unlockRemark',
            title: '解锁备注',
            hidden: el.deliveryTableHidden,
            width: '80',
            align: 'left',
            editor: {type: 'textbox', options: {editable: isSupplier === 1 ? false : true}}
        }, {
            field: 'unlockFlag',
            title: '是否解锁',
            hidden: el.deliveryTableHidden,
            width: '80',
            align: 'left', sortable: true,
            exportEnum: [
                {code: 1, codeWithDesc: '是'},
                {code: 0, codeWithDesc: '否'}
            ],
            formatter: function (value) {
                if (value === 1) {
                    return '<span style="color: red;">是</span>';
                } else {
                    return "否";
                }
            }
        }, {
            field: 'enableNum',
            title: '可送数量',
            hidden: el.deliveryTableHidden,
            width: '80',
            align: 'left',
            formatter: function (value) {
                if (value > 0) {
                    return '<span style="color: red;">' + value + '</span>';
                } else {
                    return value;
                }
            }
        }, {
            field: 'deliveryNum',
            title: '送货数量',
            hidden: el.deliveryTableHidden,
            width: '80',
            align: 'left',
            editor: {type: 'numberbox', options: {validType: ['num'], editable:true}}, //editable: isSupplier === 1 ? false : true
            formatter: function (value) {
                if (value > 0) {
                    return '<span style="color: red;">' + value + '</span>';
                } else {
                    return value;
                }
            }
        },{
            field: 'requireDate', title: '需求交期', width: '80', align: 'left', sortable: true,
        }, {
            field: 'replayRequireDate', title: '回复日期',
            width: '100', align: 'left', sortable: true,
            formatter: function (value) {
                if (value !== '') {
                    return moment(value).format("YYYY-MM-DD")
                }
                return value
            }
        }, {
            field: 'factoryCode', title: '收货工厂', width: '120', align: 'left', sortable: true,
            formatter: function (value) {
                return getNecessaryDataName("pc/sys/factory/rest/allList", value);
            }
        }, {
            field: 'supplierName', title: '供应商名称', width: '100', sortable: true, align: 'left'
        }, {
            field: 'supplierErpCode', title: '供应商编码', width: '100', sortable: true, align: 'left'
        }, {
            field: 'markFlag', title: '交货类型', width: '90', align: 'left',
            exportEnum: [
                {code: 'B', codeWithDesc: '计划交货'},
                {code: 'A', codeWithDesc: '订单交货'}
            ], formatter: function (value) {
                if (value === 'B') {
                    return '计划交货';
                } else {
                    return '订单交货';
                }
            }
        }, {
            field: 'supplierName1', title: '物流供应商', width: '180', align: 'left'
        },{
            field: 'middleClass', title: '物料中类', width: '90', align: 'left'
        }, {
            field: 'quantityRange', title: '容差', width: '60', align: 'left'
        }, {
            field: 'stockCode', title: '库存地点', width: '75', align: 'left', sortable: true
        }, {
            field: 'defaultStock', title: '默认储位', width: '80', align: 'left'
        }, {
            field: 'orderTypeName', title: '订单类型', width: '100', align: 'left'
        }, {
            field: 'remark', title: '订单行备注', width: '100', align: 'left'
        },
            {
                field: 'unlockDate',
                title: '解锁时间',
                width: 75
            }]],
        toolbar: toolbar,
        onAfterEdit: function (rowIndex, rowData, changes) {
            //结束编辑后记录编辑数据
            el.editData[rowData.id] = rowData;
        },
        onLoadSuccess: function (data) {
            data.rows.forEach(function (tmp, i) {
                if (el.editData[tmp.id]) {
                    $(el.tb).datagrid('updateRow', {
                        index: $(el.tb).datagrid('getRowIndex', tmp), row: {



                            deliveryNum: el.editData[tmp.id].deliveryNum, batchNum: el.editData[tmp.id].batchNum
                        }
                    });
                }
            });
            reloadOk = true;
        },
        onBeforeLoad: function (param) {
            displayButtonByUserRole('delivery.manage.consult.purchase.order.index');
            reloadOk = false;
        }
    });
}


// 创建送货单
function createDeliveryOrder(overseasFlag) {
    debugger;
    let dataList = [];
    // $(el.tb).edatagrid("rejectChanges");
    $(el.tb).edatagrid("saveRow");
    var rows = $(el.tb).datagrid('getChecked');
    let supplierCode1 = $('#supplierCode1').combobox('getValue');
    if (rows.length === 0||overseasFlag===1) {
        //如果什么都不选就进入，导入创建送货单的页面
        let url = g.ctx + "pc/order/deliveryOrder/page/importCreate?supplierCode1="+supplierCode1+'&overseasFlag='+overseasFlag;
        var deliveryCreateDialog = openDialog("deliveryCreateDialog", url, {
            title: overseasFlag===1?'境外送货单创建':'送货单创建',
            width: '100%',
            height: '100%',
            cache: false,
            modal: true,
            border: false,
            draggable: false,
            shadow: false,
            onClose: function () {
                deliveryCreateDialog.dialog('destroy');
            },
            buttons: [{
                text: '发货', iconCls: 'icon-save', handler: function () {
                    topWin.frames['deliveryCreateDialog'].page.saveOrUpdate(function (id,shipment,supplierCode) {
                        topWin.layer.msg('发货成功', {icon: 1});
                        deliveryCreateDialog.dialog('close');
                        $(el.tb).datagrid('reload');
                        $(el.tb).datagrid('clearChecked');
                        //增加发货方式判断
                        if (shipment != "1") {
                            // 出货检验报告是否必填判断
                            $.ajax({
                                url: g.ctx + 'pc/sys/supplierOrderFileSetting/rest/getBySupplierCode',
                                method: "GET",
                                data: {
                                    supplierCode: supplierCode
                                },
                                success: function(data) {
                                    if (!(data && data.requiredFlag === "1")) {
                                        printById(id);
                                    }
                                }
                            });
                        }
                    }, 1);
                }
            }, {
                text: '关闭', iconCls: 'icon-cancel', handler: function () {
                    deliveryCreateDialog.dialog('close');
                }
            }]
        })
    } else {

        let factoryCode;
        let companyCode;
        let companyName;
        let factoryName;
        let supplierName;

        let supplierErpCode = "";  // 同一个送货单，供应商相同
        let purOrderId = '';
        for (let i = 0; i < rows.length; i++) {
            let row = rows[i];
            let orderRor = "单号:" + row.orderCode + ",行号:" + row.orderRow
            //如果是美元则报错
            if (row.currencyCode === "USD" && (row.orderTypeCode !== 'Z006' && row.orderTypeCode !== 'Z007')) {
                topWin.layer.msg("币种是USD的采购订单，由物流供应商创建送货单。");
                return;
            }
            if (!factoryCode) {
                factoryCode = row.factoryCode;
                factoryName = row.factoryName;
            } else {
                if (factoryCode !== row.factoryCode) {
                    topWin.layer.msg(orderRor + "工厂不一致，请选择其他记录");
                    return;
                }
            }
            if (supplierErpCode === "") {
                supplierErpCode = row.supplierErpCode;
                supplierName = row.supplierName;
            } else {
                if (supplierErpCode !== row.supplierErpCode && isSupplier === 0) {
                    topWin.layer.msg(orderRor + "供应商编码不一致，请选择其他记录");
                    return;
                }
            }
            if (!companyCode) {
                companyCode = row.companyCode;
                companyName = row.companyName;
            }
            if (!purOrderId) {
                purOrderId = row.orderId;
            }
            if (row.state !== 7) {
                topWin.layer.msg(orderRor + "不是已确认状态，请选择其他记录");
                return;
            }

            if ((row.unlockFlag === '' || row.unlockFlag === 0) && row.enableNum <= 0 && row.markFlag === 'B') {
                topWin.layer.msg(orderRor + "可送数量不可为0");
                return;
            }
            if ((row.unlockFlag === 1 || row.markFlag === 'A') && row.noClearNum <= 0) {
                topWin.layer.msg(orderRor + "未清数量不可为0");
                return;
            }

            let data = row;
            data.purchaseOrderCode = row.orderCode;
            data.supplierCode = row.supplierErpCode;
            data.purchaseOrderRow = row.orderRow;
            data.buyer = row.createName;

            if (data.deliveryNum != '' &&　data.deliveryNum　!= '0') {
                if (row.unlockFlag === 1) {
                    //data.deliveryNum = row.unlockNum;
                    data.enableNum = row.unlockNum;
                } else if (row.markFlag === 'A') {
                    //data.deliveryNum = (row.noClearNum - row.onlineNum).toFixed(0);
                    data.enableNum = (row.noClearNum - row.onlineNum).toFixed(0);
                } else {
                    //data.deliveryNum = row.enableNum;
                }

            } else {
                if (row.unlockFlag === 1) {
                    data.deliveryNum = row.unlockNum;
                    data.enableNum = row.unlockNum;
                } else if (row.markFlag === 'A') {
                    data.deliveryNum = (row.noClearNum - row.onlineNum).toFixed(0);
                    data.enableNum = (row.noClearNum - row.onlineNum).toFixed(0);
                } else {
                    data.deliveryNum = row.enableNum;
                }
            }
            //判断送货数量不能大于可送数量
            if (parseInt(data.deliveryNum) > parseInt(row.enableNum)) {
                topWin.layer.msg(orderRor + "送货数量不可大于可送数量");
                return;
            }

            data.gmpPackingSpecification = data.deliveryNum
            var packNum = Math.floor(data.deliveryNum / data.gmpPackingSpecification);
            var partialNum = data.deliveryNum % data.gmpPackingSpecification;
            if (partialNum > 0) {
                packNum = packNum + 1;
            }
            data.packNum = packNum;
            data.partialNum = partialNum;
            dataList.push(data);
        }

        localStorage.setItem("deliveryOrderRows", JSON.stringify(dataList));
        let p = "companyCode=" + companyCode + "&companyName=" + companyName + "&factoryCode=" + factoryCode + "&factoryName=" + factoryName + "&purOrderId=" + purOrderId + "&supplierErpCode=" + supplierErpCode + "&supplierName=" + supplierName+"&supplierCode1="+supplierCode1;
        let url = g.ctx + "pc/order/deliveryOrder/page/createOrUpdate?" + p;
        var deliveryCreateDialog = openDialog("deliveryCreateDialog", url, {
            title: '送货单创建',
            width: '100%',
            height: '100%',
            cache: false,
            modal: true,
            border: false,
            draggable: false,
            shadow: false,
            onClose: function () {
                deliveryCreateDialog.dialog('destroy');
            },
            buttons: [{
                text: '发货', iconCls: 'icon-save', handler: function () {
                    topWin.frames['deliveryCreateDialog'].saveOrUpdate(function (id,shipment) {
                        topWin.layer.msg('发货成功', {icon: 1});
                        deliveryCreateDialog.dialog('close');
                        if (shipment != "1") {
                            $.ajax({
                                url: g.ctx + 'pc/sys/supplierOrderFileSetting/rest/getBySupplierCode',
                                method: "GET",
                                data: {
                                    supplierCode: rows[0].supplierErpCode
                                },
                                success: function(data) {
                                    if (!(data && data.requiredFlag === "1")) {
                                        printById(id);
                                    }
                                }
                            });
                        }
                        $(el.tb).datagrid('reload');
                        $(el.tb).datagrid('clearChecked');
                        //发货成功清除缓存明细的值
                        // const deliveryOrderRowsValue =  localStorage.removeItem("deliveryOrderRows");
                        //
                        // if (deliveryOrderRowsValue !== null && deliveryOrderRowsValue !== '') {
                        //     // 如果 'test' 键存在且有值（非空字符串）
                        //     // 则删除该键
                        //     localStorage.removeItem('test');
                        //     console.log('Key "test" with value "' + storedValue + '" has been removed.');
                        // }
                    }, 1);
                }
            }, {
                text: '关闭', iconCls: 'icon-cancel', handler: function () {
                    deliveryCreateDialog.dialog('close');
                }
            }]
        })
    }
}

function dataImport() {
    var files = $("#detailFileInput").filebox('files'), formData = new FormData();
    if (files.length != 1) {
        return
    }
    formData.append('file', files[0]);
    let loadIndex = topWin.layer.load(0);
    var url = g.ctx + 'pc/order/purchaseOrderDetail/rest/importUnlock'
    $.ajax({
        url: url, type: 'POST', data: formData, processData: false,
        contentType: false,
        async: true, // 设置为同步请求
        complete: function () {
            $("#detailFileInput").filebox('clear')
            topWin.layer.close(loadIndex);
        },
        success: function (obj) {
            topWin.layer.alert("导入成功", {icon: 1}, function (idx) {
                topWin.layer.close(idx);
                searchInfo()
            });

        }, error: function (data) {
            topWin.layer.alert(data.responseJSON.message, {icon: 2});
        }
    });
}