var el = {
    form : '#form',
    optType : '#optType',
    tb : '#tb',
    id : '#id',
    purchaseGroupCode : '#purchaseGroupCode',
    companyCode : '#companyCode',
    taxCode : '#taxCode',
    files : '#files',
    currencyCode : '#currencyCode',
    paymentMethod : '#paymentMethod',
    purchaseOrgCode : '#purchaseOrgCode',
    type : '#type',
    supplierName : '#supplierName',
    supplierCode : '#supplierCode',
    purchaseCategoryCode: '#purchaseCategoryCode',
    spotFlag: '#spotFlag',
    businessAgreeFlag: '#businessAgreeFlag',
    customerBearFlag: '#customerBearFlag',
    bearRatio: '#bearRatio',
};
let editor = undefined;

$(function () {
    initDict('unit,currency,payment_method,msl_level,country_series,danger_level')
    initNecessaryDatas('pc/sys/purchaseCategory/rest/listAll,pc/sys/tax/rest/listAll,pc/sys/factory/rest/allList')
    initCombo();
    loadGrid();
});

/**
 * 加载下拉框数据
 */
function initCombo() {
    //采购组
    $(el.purchaseGroupCode).combobox({
        valueField: 'code',
        textField: 'codeWithDesc',
        limitToList: 'true',
        method: 'POST',
        prompt: '--- 请选择 ---',
        url: g.ctx + 'pc/sys/purchaseGroup/rest/listAll'
    });
    $(el.companyCode).combobox({
        valueField: 'code',
        textField: 'codeWithDesc',
        limitToList: 'true',
        method: 'POST',
        prompt: '--- 请选择 ---',
        url: g.ctx + 'pc/sys/company/rest/listAll'
    });
    //调价类型
    $(el.type).combobox({
        valueField: 'code',
        textField: 'name',
        limitToList: 'true',
        value:1,
        data: [
            {'name': '数据修改', 'code': 1},
            {'name': '季度降价', 'code': 2}
        ],
        editable: false,
        prompt: '--- 请选择 ---',
        panelHeight: 'auto',
    });

    $(el.supplierName).textbox({
        prompt: '请选择供应商',
        required: true,
        editable:false,
        icons: [
            {
                iconCls: 'icon-search',
                handler: function () {
                    if (optType!==2&&optType!==3){
                        choseSupplier();
                    }
                }
            }
        ]
    });

    $(el.supplierName).textbox('textbox').css('cursor', 'pointer');
    $(el.supplierName).textbox('textbox').bind('click', function () {
        if (optType!==2&&optType!==3){
            choseSupplier();
        }
    });

    //器件分类
    $(el.purchaseCategoryCode).combobox({
        valueField: 'code',
        textField: 'codeWithDesc',
        limitToList: 'true',
        method: 'POST',
        prompt: '--- 请选择 ---',
        data:getNecessaryDataList('pc/sys/purchaseCategory/rest/listAll'),
    });

    //是否现货
    $(el.spotFlag).combobox({
        valueField: 'code',
        textField: 'name',
        limitToList: 'true',
        data: [
            {'name': '否', 'code': '0'},
            {'name': '是', 'code': '1'}
        ],
        editable: false,
        prompt: '--- 请选择 ---',
        panelHeight: 'auto',
        onChange: function (data) {
            if (Number(data)===1){
                $(el.businessAgreeFlag).combobox({ disabled: false, required: true });
                $(el.customerBearFlag).combobox({ disabled: false, required: true });
                $(el.bearRatio).numberbox({ disabled: false, required: true });
            }else {
                $(el.businessAgreeFlag).combobox({ disabled: true, required: false });
                $(el.customerBearFlag).combobox({ disabled: true, required: false });
                $(el.bearRatio).numberbox({ disabled: true, required: false });
                $(el.bearRatio).numberbox('setValue', '');
            }
        }
    });

    //事业部是否同意
    $(el.businessAgreeFlag).combobox({
        valueField: 'code',
        textField: 'name',
        limitToList: 'true',
        data: [
            {'name': '否', 'code': '0'},
            {'name': '是', 'code': '1'}
        ],
        editable: false,
        prompt: '--- 请选择 ---',
        panelHeight: 'auto'
    });

    //客户是否承担
    $(el.customerBearFlag).combobox({
        valueField: 'code',
        textField: 'name',
        limitToList: 'true',
        data: [
            {'name': '否', 'code': '0'},
            {'name': '是', 'code': '1'}
        ],
        editable: false,
        prompt: '--- 请选择 ---',
        panelHeight: 'auto',
    });
}

/**
 * 选择供应商
 */
function choseSupplier() {
    el.choseSupplierDialog = openDialog('choseSupplier', g.ctx + "pc/supplier/purchaseCategory/page/selector?single=1", {
        title: '供应商选择',
        width: '80%',
        height: '70%',
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            el.choseSupplierDialog.dialog('destroy');
        },
        buttons: [
            {
                text: '确定',
                iconCls: 'icon-ok',
                handler: function () {
                    topWin.frames['choseSupplier'].chooseRow(function (data) {
                        console.log(data[0])
                        var oriSupCode = $(el.supplierCode).val();
                        if(oriSupCode !== data[0].supplierCode){
                            clearPageTableData();
                        }
                        $(el.supplierCode).val(data[0].supplierCode);
                        $(el.supplierName).textbox('setValue', data[0].supplierName);
                        $(el.companyCode).combobox('setValue', data[0].companyCode);
                        $(el.purchaseOrgCode).val(data[0].purchaseOrgCode);
                        $(el.taxCode).val(data[0].taxCode);
                        $(el.currencyCode).val(data[0].currencyCode);
                        $(el.paymentMethod).val(data[0].paymentTerms);
                        $(el.purchaseCategoryCode).combobox('setValue',data[0].purchaseCategoryCode);
                        el.choseSupplierDialog.dialog('close');
                    });
                }
            },
            {
                text: '关闭',
                iconCls: 'icon-cancel',
                handler: function () {
                    el.choseSupplierDialog.dialog('close');
                }
            }
        ]
    });
}

function loadGrid() {
    var url = g.ctx + 'pc/inquiry/price/audit/detail/rest/getDetailListByAuditId?auditId='+$(el.id).val();
    let columns = [
        {
            field: 'ck',
            checkbox: true
        }
    ];
    if (optType===3){
        url = g.ctx + 'pc/inquiry/price/audit/detail/rest/getDetailListByPriceIds?priceIds='+priceIds;
        columns.push(
            {
                field: 'materialCode',
                title: '物料编码<span style="color: red">*</span>',
                width: '180',
                align: 'left'
            },
            {
                field: 'materialName',
                title: '物料名称<span style="color: red">*</span>',
                width: '180',
                align: 'left',
                formatter: function (value, row) {
                    return '<span title="'+value+'">'+value+'</span>';
                }
            },
            // {
            //     field: 'materialSpec', title: '规格', width: '150'
            // },
            // {
            //     field: 'productName', title: '品名', width: '100'
            // },
            // {
            //     field: 'brand', title: '品牌', width: '100'
            // },
            {
                field: 'factoryCode',
                title: '工厂<span style="color: red">*</span>',
                width: '180',
                align: 'left',
                formatter: function (value) {
                    return getNecessaryDataName('pc/sys/factory/rest/allList',value)
                }
            },
            {
                field: 'purchaseUnit',
                title: '采购单位<span style="color: red">*</span>',
                width: '120',
                align: 'left',
                formatter: function (value) {
                    return getDicName('unit',value)
                }
            },
            {
                field: 'originalTaxPrice',
                title: '原含税单价',
                width: '120',
                align: 'left',
                formatter: function (value) {
                    return formatterPrice(value)
                }
            },
            {
                field: 'taxPrice',
                title: '含税单价<span style="color: red">*</span>',
                width: '120',
                align: 'left',
                editor: {
                    type: 'numberbox',
                    options: {
                        min: 0,
                        required:true,
                        precision: priceHandle.precision
                    }
                },
                formatter: function (value) {
                    return formatterPrice(value)
                }
            },
            {
                field: 'taxCode',
                title: '税率<span style="color: red">*</span>',
                width: '180',
                align: 'left',
                formatter: function (value) {
                    return getNecessaryDataName('pc/sys/tax/rest/listAll',value)
                }
            },
            {
                field: 'currencyCode',
                title: '币别<span style="color: red">*</span>',
                width: '120',
                align: 'left',
                formatter: function (value) {
                    return getDicName('currency',value)
                }
            },
            {
                field: 'paymentMethod',
                title: '付款条件<span style="color: red">*</span>',
                width: '250',
                align: 'left',
                editor: {
                    type: 'combobox', options: {
                        // readonly: true,
                        required: true,
                        valueField: "code",
                        textField: "codeWithDesc",
                        data: getDictList('payment_method'),
                        editable: false,
                        panelHeight: '150'
                    }
                },
                formatter: function (value) {
                    return getDicName('payment_method',value)
                }
            },
            {
                field: 'effectDate',
                title: '生效日期<span style="color: red">*</span><button type="button" onclick="copy(\'effectDate\')" class="easyui-linkbutton">复制</button>',
                width: '150',
                align: 'left',
                editor: {
                    type: 'datebox',
                    options: {
                        required: true
                    }
                },
                formatter: function (value) {
                    if (value) {
                        return new Date(value).format('yyyy-MM-dd');
                    }
                    return value;
                }
            },
            {
                field: 'invalidDate',
                title: '失效日期<span style="color: red">*</span><button type="button" onclick="copy(\'invalidDate\')" class="easyui-linkbutton">复制</button>',
                width: '150',
                align: 'left',
                editor: {
                    type: 'datebox',
                    options: {
                        required: true
                    }
                },
                formatter: function (value) {
                    if (value) {
                        return new Date(value).format('yyyy-MM-dd');
                    }
                    return value;
                }
            },
            {
                field: 'purchaseType',
                title: '采购类型<span style="color: red">*</span>',
                width: '120',
                align: 'left',
                editor: {
                    type: 'combobox',
                    options: {
                        editable: false,
                        required: true,
                        valueField: 'id', textField: 'txt',
                        data: [
                            {id: '0', txt: '标准'},
                            {id: '3', txt: '委外'}],
                        prompt: '请选择'
                    }
                },
                formatter: function (value) {
                    if (value&&Number(value) === 0) {
                        return '标准';
                    } else if (value&&Number(value) === 3) {
                        return '委外';
                    }
                },
            },
            {
                field: 'stageNum1',
                title: '阶梯数量1',
                width: '120',
                align: 'left',
                editor: {
                    type: 'numberbox',
                    options: {
                        min: 0,
                        precision: 2
                    }
                }
            },
            {
                field: 'stagePrice1',
                title: '阶梯价格1',
                width: '120',
                align: 'left',
                editor: {
                    type: 'numberbox',
                    options: {
                        min: 0,
                        precision: priceHandle.precision
                    }
                },
                formatter: function (value) {
                    return formatterPrice(value)
                }
            },
            {
                field: 'stageNum2',
                title: '阶梯数量2',
                width: '120',
                align: 'left',
                editor: {
                    type: 'numberbox',
                    options: {
                        min: 0,
                        precision: 2
                    }
                }
            },
            {
                field: 'stagePrice2',
                title: '阶梯价格2',
                width: '120',
                align: 'left',
                editor: {
                    type: 'numberbox',
                    options: {
                        min: 0,
                        precision: priceHandle.precision
                    }
                },
                formatter: function (value) {
                    return formatterPrice(value)
                }
            },
            {
                field: 'stageNum3',
                title: '阶梯数量3',
                width: '120',
                align: 'left',
                editor: {
                    type: 'numberbox',
                    options: {
                        min: 0,
                        precision: 2
                    }
                }
            },
            {
                field: 'stagePrice3',
                title: '阶梯价格3',
                width: '120',
                align: 'left',
                editor: {
                    type: 'numberbox',
                    options: {
                        min: 0,
                        precision: priceHandle.precision
                    }
                },
                formatter: function (value) {
                    return formatterPrice(value)
                }
            },
            {
                field: 'stageNum4',
                title: '阶梯数量4',
                width: '120',
                align: 'left',
                editor: {
                    type: 'numberbox',
                    options: {
                        min: 0,
                        precision: 2
                    }
                }
            },
            {
                field: 'stagePrice4',
                title: '阶梯价格4',
                width: '120',
                align: 'left',
                editor: {
                    type: 'numberbox',
                    options: {
                        min: 0,
                        precision: priceHandle.precision
                    }
                },
                formatter: function (value) {
                    return formatterPrice(value)
                }
            },
            {
                field: 'stageNum5',
                title: '阶梯数量5',
                width: '120',
                align: 'left',
                editor: {
                    type: 'numberbox',
                    options: {
                        min: 0,
                        precision: 2
                    }
                }
            },
            {
                field: 'stagePrice5',
                title: '阶梯价格5',
                width: '120',
                align: 'left',
                editor: {
                    type: 'numberbox',
                    options: {
                        min: 0,
                        precision: priceHandle.precision
                    }
                },
                formatter: function (value) {
                    return formatterPrice(value)
                }
            },
            {
                field: 'applyTit',
                title: '采购申请号',
                width: '150',
                align: 'left'
            },
            {
                field: 'applyItemNo',
                title: '采购申请行号',
                width: '120',
                align: 'left'
            },
            {
                field: 'mpn',
                title: '制造商型号',
                width: '120',
                align: 'left',
                editor: {
                    required: true,
                    type: 'textbox',
                }
            },
            {
                field: 'manufacturer',
                title: '制造商中文',
                width: '120',
                align: 'left',
                editor: {
                    required: true,
                    type: 'textbox',
                }
            },
            {
                field: 'manufacturerEn',
                title: '制造商英文',
                width: '120',
                align: 'left',
                editor: {
                    required: true,
                    type: 'textbox',
                }
            },
            {
                field: 'countryName',
                title: '国系',
                width: '120',
                align: 'left',
                editor: {
                    type: 'combobox',
                    options: {
                        required: false,
                        valueField: "code",
                        textField: "codeWithDesc",
                        data: getDictList('country_series'),
                        editable: false,
                        panelHeight: '150'
                    }
                },
                formatter: function (value) {
                    return getDicName('country_series',value)
                }
            },
            {
                field: 'msl',
                title: '潮湿等级',
                width: '120',
                align: 'left',
                editor: {
                    type: 'combobox',
                    options: {
                        required: false,
                        valueField: "code",
                        textField: "codeWithDesc",
                        data: getDictList('msl_level'),
                        editable: false,
                        panelHeight: '150'
                    }
                },
                formatter: function (value) {
                    return getDicName('msl_level',value)
                }
            },
            {
                field: 'cancelWindow',
                title: '取消窗',
                width: '120',
                align: 'left',
                editor: {
                    type: 'textbox',
                }
            },
            {
                field: 'delayWindow',
                title: '推迟窗',
                width: '120',
                align: 'left',
                editor: {
                    type: 'textbox',
                }
            },
            {
                field: 'riskLevel',
                title: '风险等级',
                width: '120',
                align: 'left',
                editor: {
                    type: 'combobox',
                    options: {
                        valueField: "code",
                        textField: "codeWithDesc",
                        data: getDictList('danger_level'),
                        editable: false,
                        panelHeight: '150'
                    }
                },
                formatter: function (value) {
                    return getDicName('danger_level',value)
                }
            },
            {
                field: 'logisticsAgencyFee',
                title: '物流代理费',
                width: '120',
                align: 'left',
                editor: {
                    type: 'numberbox',
                    options: {
                        min: 0,
                        precision: priceHandle.precision
                    }
                },
                formatter: function (value) {
                    return formatterPrice(value)
                }
            },
            {
                field: 'tariff',
                title: '关税',
                width: '120',
                align: 'left',
                editor: {
                    type: 'numberbox',
                    options: {
                        min: 0,
                        precision: priceHandle.precision
                    }
                },
                formatter: function (value) {
                    return formatterPrice(value)
                }
            },
            {
                field: 'eccn',
                title: 'ECCN',
                width: '120',
                align: 'left',
                editor: {
                    type: 'textbox',
                }
            },
            {
                field: 'remark',
                title: '备注',
                width: '180',
                align: 'left',
                editor: {
                    type: 'textbox'
                }
            }
        );
    }else {
        columns.push(
            {
                field: 'materialCode',
                title: '物料编码<span style="color: red">*</span>',
                width: '180',
                align: 'left',
                editor: {
                    type: 'textbox',
                    options: {
                        required: true,
                        editable:false,
                        prompt: '选择自动带出物料信息',
                        icons: [{
                            iconCls: 'icon-search',
                            handler: function () {
                                let rowIndex = $($(this).parents('tr')[1])[0]['rowIndex'];
                                let gridRow = $('#tb').datagrid('getRows')[rowIndex];
                                var selectMaterialElement = openDialog("selectMaterialDialog", g.ctx + 'pc/sys/product/select/dialog?purchaseGroupFlag=1', {
                                    title: '选择物料',
                                    width: '80%',
                                    height: '75%',
                                    cache: false,
                                    modal: true,
                                    border: false,
                                    draggable: false,
                                    shadow: false,
                                    onClose: function () {
                                        selectMaterialElement.dialog('destroy');
                                    },
                                    buttons: [{
                                        text: '确定',
                                        iconCls: 'icon-save',
                                        handler: function () {
                                            topWin.frames['selectMaterialDialog'].chooseRows(function (data) {
                                                let ps = $(el.tb).datagrid('getRows');
                                                for (let i = 0; i < data.length; i++) {
                                                    let product = data[i];
                                                    let sameFlag = false;
                                                    for (let j = 0; j < ps.length; j++) {
                                                        if (product.id === ps[j].productId) {
                                                            sameFlag = true;
                                                            break;
                                                        }
                                                    }
                                                    if (sameFlag){
                                                        continue;
                                                    }

                                                    //物料编码
                                                    let materialCode = $(el.tb).datagrid('getEditor', {
                                                        index: rowIndex,
                                                        field: 'materialCode'
                                                    }).target;
                                                    materialCode.textbox('setValue', product.materialCode);
                                                    //物料描述
                                                    let materialName = $(el.tb).datagrid('getEditor', {
                                                        index: rowIndex,
                                                        field: 'materialName'
                                                    }).target;
                                                    materialName.textbox('setValue',product.materialName);

                                                    //规格
                                                    let materialSpec = $(el.tb).datagrid('getEditor', {
                                                        index: rowIndex,
                                                        field: 'materialSpec'
                                                    }).target;
                                                    materialSpec.textbox('setValue',product.materialSpec);

                                                    //品名
                                                    let productName = $(el.tb).datagrid('getEditor', {
                                                        index: rowIndex,
                                                        field: 'productName'
                                                    }).target;
                                                    productName.textbox('setValue',product.productName);

                                                    //品牌
                                                    let brand = $(el.tb).datagrid('getEditor', {
                                                        index: rowIndex,
                                                        field: 'brand'
                                                    }).target;
                                                    brand.textbox('setValue',product.brand);

                                                    //工厂
                                                    let factoryCode = $(el.tb).datagrid('getEditor', {
                                                        index: rowIndex,
                                                        field: 'factoryCode'
                                                    }).target;
                                                    factoryCode.combobox('setValue',product.factoryCode);
                                                    factoryCode.combobox('readonly',true);

                                                    //采购单位
                                                    let purchaseUnit = $(el.tb).datagrid('getEditor', {
                                                        index: rowIndex,
                                                        field: 'purchaseUnit'
                                                    }).target;
                                                    purchaseUnit.combobox('setValue',product.orderUnit);
                                                }
                                                selectMaterialElement.dialog('close');
                                            });
                                        }
                                    }, {
                                        text: '关闭',
                                        iconCls: 'icon-cancel',
                                        handler: function () {
                                            selectMaterialElement.dialog('close');
                                        }
                                    }]
                                });
                            }
                        }]
                    }
                }
            },
            {
                field: 'materialName',
                title: '物料名称<span style="color: red">*</span>',
                width: '180',
                align: 'left',
                editor: {
                    type: 'textbox',
                    options: {
                        required:true
                    }
                }
            },
            {
                field: 'materialSpec', title: '规格', width: '150',
                editor: {
                    type: 'textbox'
                }
            },
            {
                field: 'productName', title: '品名', width: '100',
                editor: {
                    type: 'textbox'
                }
            },
            {
                field: 'brand', title: '品牌', width: '100',
                editor: {
                    type: 'textbox'
                }
            },
            {
                field: 'factoryCode',
                title: '工厂<span style="color: red">*</span>',
                width: '180',
                align: 'left',
                editor: {
                    type: 'combobox',
                    options: {
                        valueField: "code",
                        textField: "codeWithDesc",
                        data: getNecessaryDataList('pc/sys/factory/rest/allList'),
                        required:true,
                        editable: false
                    }
                },
                formatter: function (value) {
                    return getNecessaryDataName('pc/sys/factory/rest/allList',value)
                }
            },
            {
                field: 'purchaseUnit',
                title: '采购单位<span style="color: red">*</span>',
                width: '120',
                align: 'left',
                editor: {
                    type: 'combobox',
                    options: {
                        valueField: "code",
                        textField: "codeWithDesc",
                        data: getDictList('unit'),
                        required:true,
                        editable: false
                    }
                },
                formatter: function (value) {
                    return getDicName('unit',value)
                }
            },
            {
                field: 'originalTaxPrice',
                title: '原含税单价',
                width: '120',
                align: 'left',
                formatter: function (value) {
                    return formatterPrice(value)
                }
            },
            {
                field: 'taxPrice',
                title: '含税单价<span style="color: red">*</span>',
                width: '120',
                align: 'left',
                editor: {
                    type: 'numberbox',
                    options: {
                        min: 0,
                        required:true,
                        precision: priceHandle.precision
                    }
                },
                formatter: function (value) {
                    return formatterPrice(value)
                }
            },
            {
                field: 'taxCode',
                title: '税率<span style="color: red">*</span>',
                width: '180',
                align: 'left',
                editor: {
                    type: 'combobox',
                    options: {
                        valueField: "code",
                        textField: "codeWithDesc",
                        data: getNecessaryDataList('pc/sys/tax/rest/listAll'),
                        editable: false,
                        required:true,
                        panelHeight: '150'
                    }
                },
                formatter: function (value) {
                    return getNecessaryDataName('pc/sys/tax/rest/listAll',value)
                }
            },
            {
                field: 'currencyCode',
                title: '币别<span style="color: red">*</span>',
                width: '120',
                align: 'left',
                editor: {
                    type: 'combobox', options: {
                        // readonly: true,
                        required: true,
                        valueField: "code",
                        textField: "codeWithDesc",
                        data: getDictList('currency'),
                        editable: false,
                        panelHeight: '150'
                    }
                },
                formatter: function (value) {
                    return getDicName('currency',value)
                }
            },
            {
                field: 'paymentMethod',
                title: '付款条件<span style="color: red">*</span>',
                width: '250',
                align: 'left',
                editor: {
                    type: 'combobox', options: {
                        // readonly: true,
                        required: true,
                        valueField: "code",
                        textField: "codeWithDesc",
                        data: getDictList('payment_method'),
                        editable: false,
                        panelHeight: '150'
                    }
                },
                formatter: function (value) {
                    return getDicName('payment_method',value)
                }
            },
            {
                field: 'effectDate',
                title: '生效日期<span style="color: red">*</span>',
                width: '150',
                align: 'left',
                editor: {
                    type: 'datebox',
                    options: {
                        required: true
                    }
                },
                formatter: function (value) {
                    if (value) {
                        return new Date(value).format('yyyy-MM-dd');
                    }
                    return value;
                }
            },
            {
                field: 'invalidDate',
                title: '失效日期<span style="color: red">*</span><button type="button" onclick="copy(\'brand\')" class="easyui-linkbutton">复制</button>',
                width: '150',
                align: 'left',
                editor: {
                    type: 'datebox',
                    options: {
                        required: true
                    }
                },
                formatter: function (value) {
                    if (value) {
                        return new Date(value).format('yyyy-MM-dd');
                    }
                    return value;
                }
            },
            {
                field: 'purchaseType',
                title: '采购类型<span style="color: red">*</span>',
                width: '120',
                align: 'left',
                editor: {
                    type: 'combobox',
                    options: {
                        editable: false,
                        required: true,
                        valueField: 'id', textField: 'txt',
                        data: [
                            {id: '0', txt: '标准'},
                            {id: '3', txt: '委外'}],
                        prompt: '请选择'
                    }
                },
                formatter: function (value) {
                    if (value&&Number(value) === 0) {
                        return '标准';
                    } else if (value&&Number(value) === 3) {
                        return '委外';
                    }
                },
            },
            {
                field: 'stageNum1',
                title: '阶梯数量1',
                width: '120',
                align: 'left',
                editor: {
                    type: 'numberbox',
                    options: {
                        min: 0,
                        precision: 2
                    }
                }
            },
            {
                field: 'stagePrice1',
                title: '阶梯价格1',
                width: '120',
                align: 'left',
                editor: {
                    type: 'numberbox',
                    options: {
                        min: 0,
                        precision: priceHandle.precision
                    }
                },
                formatter: function (value) {
                    return formatterPrice(value)
                }
            },
            {
                field: 'stageNum2',
                title: '阶梯数量2',
                width: '120',
                align: 'left',
                editor: {
                    type: 'numberbox',
                    options: {
                        min: 0,
                        precision: 2
                    }
                }
            },
            {
                field: 'stagePrice2',
                title: '阶梯价格2',
                width: '120',
                align: 'left',
                editor: {
                    type: 'numberbox',
                    options: {
                        min: 0,
                        precision: priceHandle.precision
                    }
                }
            },
            {
                field: 'stageNum3',
                title: '阶梯数量3',
                width: '120',
                align: 'left',
                editor: {
                    type: 'numberbox',
                    options: {
                        min: 0,
                        precision: 2
                    }
                }
            },
            {
                field: 'stagePrice3',
                title: '阶梯价格3',
                width: '120',
                align: 'left',
                editor: {
                    type: 'numberbox',
                    options: {
                        min: 0,
                        precision: priceHandle.precision
                    }
                },
                formatter: function (value) {
                    return formatterPrice(value)
                }
            },
            {
                field: 'stageNum4',
                title: '阶梯数量4',
                width: '120',
                align: 'left',
                editor: {
                    type: 'numberbox',
                    options: {
                        min: 0,
                        precision: 2
                    }
                }
            },
            {
                field: 'stagePrice4',
                title: '阶梯价格4',
                width: '120',
                align: 'left',
                editor: {
                    type: 'numberbox',
                    options: {
                        min: 0,
                        precision: priceHandle.precision
                    }
                },
                formatter: function (value) {
                    return formatterPrice(value)
                }
            },
            {
                field: 'stageNum5',
                title: '阶梯数量5',
                width: '120',
                align: 'left',
                editor: {
                    type: 'numberbox',
                    options: {
                        min: 0,
                        precision: 2
                    }
                }
            },
            {
                field: 'stagePrice5',
                title: '阶梯价格5',
                width: '120',
                align: 'left',
                editor: {
                    type: 'numberbox',
                    options: {
                        min: 0,
                        precision: priceHandle.precision
                    }
                },
                formatter: function (value) {
                    return formatterPrice(value)
                }
            },
            {
                field: 'applyTit',
                title: '采购申请号',
                width: '150',
                align: 'left',
                editor: {
                    type: 'textbox',
                    options:{
                        required: false
                    }
                }
            },
            {
                field: 'applyItemNo',
                title: '采购申请行号',
                width: '120',
                align: 'left',
                editor: {
                    type: 'textbox',
                    options:{
                        required: false
                    }
                }
            },
            {
                field: 'mpn',
                title: '制造商型号',
                width: '120',
                align: 'left',
                editor: {
                    required: true,
                    type: 'textbox',
                }
            },
            {
                field: 'manufacturer',
                title: '制造商中文',
                width: '120',
                align: 'left',
                editor: {
                    required: true,
                    type: 'textbox',
                }
            },
            {
                field: 'manufacturerEn',
                title: '制造商英文',
                width: '120',
                align: 'left',
                editor: {
                    required: true,
                    type: 'textbox',
                }
            },
            {
                field: 'countryName',
                title: '国系',
                width: '120',
                align: 'left',
                editor: {
                    type: 'combobox',
                    options: {
                        required: true,
                        valueField: "code",
                        textField: "codeWithDesc",
                        data: getDictList('country_series'),
                        editable: false,
                        panelHeight: '150'
                    }
                },
                formatter: function (value) {
                    return getDicName('country_series',value)
                }
            },
            {
                field: 'msl',
                title: '潮湿等级',
                width: '120',
                align: 'left',
                editor: {
                    type: 'combobox',
                    options: {
                        required: true,
                        valueField: "code",
                        textField: "codeWithDesc",
                        data: getDictList('msl_level'),
                        editable: false,
                        panelHeight: '150'
                    }
                },
                formatter: function (value) {
                    return getDicName('msl_level',value)
                }
            },
            {
                field: 'cancelWindow',
                title: '取消窗',
                width: '120',
                align: 'left',
                editor: {
                    type: 'textbox',
                }
            },
            {
                field: 'delayWindow',
                title: '推迟窗',
                width: '120',
                align: 'left',
                editor: {
                    type: 'textbox',
                }
            },
            {
                field: 'riskLevel',
                title: '风险等级',
                width: '120',
                align: 'left',
                editor: {
                    type: 'combobox',
                    options: {
                        valueField: "code",
                        textField: "codeWithDesc",
                        data: getDictList('danger_level'),
                        editable: false,
                        panelHeight: '150'
                    }
                },
                formatter: function (value) {
                    return getDicName('danger_level',value)
                }
            },
            {
                field: 'logisticsAgencyFee',
                title: '物流代理费',
                width: '120',
                align: 'left',
                editor: {
                    type: 'numberbox',
                    options: {
                        min: 0,
                        precision: priceHandle.precision
                    }
                },
                formatter: function (value) {
                    return formatterPrice(value)
                }
            },
            {
                field: 'tariff',
                title: '关税',
                width: '120',
                align: 'left',
                editor: {
                    type: 'numberbox',
                    options: {
                        min: 0,
                        precision: priceHandle.precision
                    }
                },
                formatter: function (value) {
                    return formatterPrice(value)
                }
            },
            {
                field: 'eccn',
                title: 'ECCN',
                width: '120',
                align: 'left',
                editor: {
                    type: 'textbox',
                }
            },
            {
                field: 'remark',
                title: '备注',
                width: '180',
                align: 'left',
                editor: {
                    type: 'textbox'
                }
            }
        )
    }
    var toolbar = [];
    if (optType===0||optType===1){
        toolbar.push(
            {
                text: "新增",
                iconCls: 'icon-add',
                handler: function () {
                    if(!$(el.supplierCode).val()){
                        layer.msg('请先选择供应商!');
                        return;
                    }
                    $(el.tb).datagrid('appendRow', {
                        state:1,
                        taxCode:$(el.taxCode).val(),
                        currencyCode:$(el.currencyCode).val(),
                        paymentMethod:$(el.paymentMethod).val(),
                        purchaseOrgCode:$(el.purchaseOrgCode).val()
                    });
                    let index = $(el.tb).datagrid('getRows').length - 1;
                    if (editor !== undefined) {
                        let flag = $(el.tb).datagrid('validateRow', editor);
                        if(!flag){
                            layer.msg('检验未通过,编辑已取消!');
                            $('#tb').datagrid('deleteRow', index);
                            return;
                        }
                        //关闭原编辑行
                        $(el.tb).datagrid('endEdit', editor);
                        editor = index;
                        $(el.tb).datagrid('beginEdit', editor);
                    }else{
                        editor = index;
                        $(el.tb).datagrid('beginEdit', editor);
                    }
                }
            }, "-",{
                text: "删除",
                iconCls: 'icon-remove',
                handler: function () {
                    let row = $(el.tb).datagrid('getSelected');
                    if (!row) {
                        layer.alert("请选择数据");
                        return;
                    }
                    let index = $(el.tb).datagrid('getRowIndex', row);
                    $(el.tb).datagrid('deleteRow', index);
                    //删除行的时候，要把编辑行的索引更新
                    if (index < editor) {
                        editor--;
                    } else if (index === editor) {
                        editor = -1;
                    }
                }
            }
        );
    }
    $(el.tb).datagrid({
        iconCls: 'icon-list',
        height: '430',
        singleSelect: false,
        loadMsg: "数据加载中......",
        rownumbers: true,
        nowrap: true,
        striped: true,
        url: url,
        columns: [columns],
        onClickRow: function (index, rowData) {
            if(optType !== 2){
                if(editor !== undefined){
                    let flag = $(el.tb).datagrid('validateRow', editor);
                    if(!flag){
                        layer.msg('检验未通过!');
                        return;
                    }
                    $(el.tb).datagrid('endEdit', editor);
                }
                //编辑行编辑时状态为编辑
                if(optType === 1){
                    $(el.tb).datagrid('updateRow', {index: index, row: {state: 2}})
                }
                editor = index;
                $(el.tb).datagrid('beginEdit', index);
            }
        },
        toolbar: toolbar,
    });
}

function copy(type) {
    $(el.tb).datagrid('endEdit', editor);
    let allRows = $(el.tb).datagrid('getRows');
    for (let i = 0;i<allRows.length;i++){
        $(el.tb).datagrid('endEdit', i);

    }
    for (let i = 0; i < allRows.length; i++) {
        $(el.tb).datagrid('updateRow', {
            index: i,
            row: {
                [type]: allRows[0][type]
            }
        });
    }
}

/**
 * 清空页面表格数据
 */
function clearPageTableData() {
    $(el.tb).datagrid('loadData', {total: 0, rows: []});
}

function saveOrUpdate(call){
    if($(el.form).form('validate')){
        if(editor !== undefined){
            let flag = $(el.tb).datagrid('validateRow', editor);
            if(!flag){
                layer.msg('检验未通过!请检查行明细中红框未填');
                return;
            }
            $(el.tb).datagrid('endEdit', editor);
        }
        var formData = $(el.form).serializeJson();
        formData.files = $(el.files).attr('data-fileIds');
        let row = $(el.tb).datagrid('getRows');
        if (row.length <= 0) {
            layer.msg('请添加明细', {time: 2000});
            return false;
        }
        for (let i = 0 ; i < row.length ; i ++){
            let flag = false, msg = "";
            if(row[i].effectDate && row[i].invalidDate){
                if (row[i].effectDate > row[i].invalidDate) {
                    layer.msg("明细第"+ ++i+"行，生效日期必须小于失效日期");
                    return;
                }
            }
            row[i].noTaxPrice = getTaxPrice(row[i].taxCode,row[i].taxPrice,0,precision,1)
        }
        formData.detailList = row;
        var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
        $.ajax({
            url: g.ctx + 'pc/inquiry/price/audit/rest/saveOrUpdate',
            method: 'post',
            data: JSON.stringify(formData),
            contentType: 'application/json',
            success: function (data) {
                call();
            },
            complete: function(data) {
                top.layer.close(loading);
            },
            error: function (data) {
                parent.layer.alert(JSON.parse(data.responseText).message,{icon: 2, shadeClose: true, title: '温馨提示'});
            }
        });
    }
}