/**
 * 样品申请创建页面
 * view/supplier/product/demo/add.ftl
 */
var el = {
    form: '#form',
    openProuctChooseDialog: null,
    code: '#code',
    supplierCode: '#supplierCode',
    supplierName: '#supplierName',
    materialCode: '#materialCode',
    materialName: '#materialName',
    isSupplier: "#isSupplier",
    supplierId: "#supplierId",
    productName: '#productName',
    productNameEn: '#productNameEn',
    countryName: '#countryName',
    remark: '#remark',
    mpn: '#mpn',
    msl: '#msl',
    eccn: '#eccn',
    countryCode: '#countryCode',
    mslName: '#mslName',
}, isSupplier = $(el.isSupplier).val() !== 'false';


$(function () {
});

function getAvlItem() {
    $.ajax({
        url: g.ctx + "pc/supplier/avl/rest/getAvlItem?materialCode=" + $(el.materialCode).val(),
        type: "GET",
        contentType: "application/json",
        //data: JSON.stringify(postData),
        success: function (data) {
            $(el.form).form('load', {
                productNameEn: data.enName,
                productName: data.zhName,
                mpn: data.model,
                msl: data.msl,
                eccn: data.eccn,
                moq: data.moq,
                mpq: data.mpq,
                cif: data.cif,
                countryCode: data.entryvalue,
            });
        },
        error: function (data) {
            topWin.layer.alert(data.responseJSON.message, {icon: 2});
        }
    });
}





/**
 * 选择物料信息对话框
 */
function openProuctChooseDialog() {

    var selectMaterialElement = openDialog("selectMaterialDialog", g.ctx + 'pc/sys/product/select/dialog?single=1', {
        title: '选择物料',
        width: '80%',
        height: '75%',
        cache: false,
        modal: true,
        border: false,
        draggable: true,
        shadow: false,
        onClose: function () {
            selectMaterialElement.dialog('destroy');
        },
        buttons: [{
            text: '确定',
            iconCls: 'icon-save',
            handler: function () {
                topWin.frames['selectMaterialDialog'].chooseRows(function (data) {
                    console.log(data)
                    $(el.form).form('load', {
                        materialCode: data.materialCode,
                        materialName: data.materialName,
                    });
                    selectMaterialElement.dialog('close');
                    getAvlItem();
                })
            }
        }, {
            text: '关闭',
            iconCls: 'icon-cancel',
            handler: function () {
                selectMaterialElement.dialog('close');
            }
        }]
    });
}

/**
 * 选择供应商
 */
function openSupplierChooseDialog() {
    var supplierDialogElement = openDialog("supplierDialog", g.ctx + "pc/supplier/supplierSelect?single=1", {
        title: common_layer_title.select_supplier_title,
        width: '80%',
        height: '80%',
        cache: false,
        modal: true,
        border: false,
        draggable: true,
        shadow: false,
        onClose: function () {
            supplierDialogElement.dialog('destroy');
        },
        buttons: [{
            text: '选择',
            iconCls: 'icon-save',
            handler: function () {
                topWin.frames['supplierDialog'].chooseRow(function (data) {
                    $(el.form).form('load', {
                        supplierId: data.id,
                        supplierCode: data.supplierCode,
                        supplierName: data.supplierName
                    });
                    supplierDialogElement.dialog('close');
                });
            }
        }, {
            text: '关闭',
            iconCls: 'icon-cancel',
            handler: function () {
                supplierDialogElement.dialog('close');
            }
        }]
    });
}


/**
 * 提交
 * @param state 1暂存 2 提交
 * @returns {boolean}
 */
function saveDemo(state, call) {

    //供应商角色&提交
    if (isSupplier && state === 2) {
        if (!$(el.form).form('validate')) {
            topWin.layer.msg(commonMsg.msg2);
            return false;
        }
    }
    if (!isSupplier) {
        var supplierId = $(el.supplierId).val();
        if (!supplierId) {
            topWin.layer.msg('请选择供应商');
            return false;
        }
    }

    var materialCode = $(el.materialCode).val();
    if (!materialCode) {
        topWin.layer.msg('请选择物料');
        return false;
    }

    var productName = $(el.productName).val();
    if (!productName) {
        //topWin.layer.msg('请输入制造商名称');
        //return false;
    }

    //const regex = /[^a-zA-Z0-9/ ]/g
    const regMPNStr = /\\|\?|\？|\"|\“|\”|\'|\‘|\’|\<|\>|\{|\}|\[|\]|\【|\】|\：|\:|\（|\）|\、|\×|\，|\^|\$|\!|\~|\`|\|/g
    const regStr = /\\|\/|\?|\？|\"|\“|\”|\'|\‘|\’|\<|\>|\{|\}|\[|\]|\【|\】|\/|\：|\:|\（|\）|\、|\×|\，|\^|\$|\!|\~|\`|\|/g
    const regNum = /^\d+(\.\d+)?$/;
    var productNameEn = $(el.productNameEn).val();
    if (!productNameEn) {
        topWin.layer.msg('请输入制造商英文简称');
        return false;
    }

    for(var i= 0;i < productNameEn.length;i ++) {
        if(productNameEn.charCodeAt(i) <= 0 || productNameEn.charCodeAt(i) >= 255) {
            topWin.layer.msg('制造商英文简称不能包含中文，请重新输入');
            return false;
        }
    }
    var flag1 = regStr.test(productNameEn);
    if(flag1) {
        topWin.layer.msg('制造商英文简称不能包含特殊字符，请重新输入');
        return false;
    }

    var mpnName = $(el.mpn).val();
    if (!mpnName) {
        topWin.layer.msg('请输入MPN(厂家型号)');
        return false;
    }

    for(var i= 0;i < mpnName.length;i ++) {
        if(mpnName.charCodeAt(i) <= 0 || mpnName.charCodeAt(i) >= 255) {
            topWin.layer.msg('MPN(厂家型号)不能包含中文，请重新输入');
            return false;
        }
    }

    var flage = regMPNStr.test(mpnName);
    if(flage) {
        topWin.layer.msg('MPN(厂家型号)不能包含特殊字符，请重新输入');
        return false;
    }


    var mpn = $('#mpn').val();

    var flage2 = regStr.test(mpn);
    if(flage2) {
        topWin.layer.msg('MPN(厂家型号)不能包含特殊字符，请重新输入');
        return false;
    }


    var mpnFlag = false;
    // 发起AJAX请求
    $.ajax({
        url: g.ctx + "pc/order/purchaseOrderDetail/rest/checkOriginIsRequired",
        type: "GET",
        async: false,
        data: {
            materialCode: materialCode
        },
        success: function (data) {
            debugger;
            if(data){
                if(mpn.includes('/')){
                    mpnFlag = true;
                }
            }
        },
        error: function (data) {

        }
    })
   if(mpnFlag){
       topWin.layer.msg('越南料号的MPN(厂家型号)不能填写"/"，请重新输入');
       return false;
   }


    let postData1 = $(el.form).serializeJson();
    var countryCode = postData1.countryCode;
    if (!countryCode) {
        //topWin.layer.msg('请选择国系');
        //return false;
    }

    var eccn = postData1.eccn;
    if (countryCode == 'United States' && !eccn) {
        //topWin.layer.msg('请输入ECCN');
        //return false;
    }

    var msl = postData1.msl;
    if (!msl) {
        topWin.layer.msg('请选择湿敏等级');
        return false;
    }

    var moq = postData1.moq;
    var flage1 = regNum.test(moq);
    if (!flage1) {
        //topWin.layer.msg('MOQ只能输入数字，请重新输入');
        //return false;
    }

    var mpq = postData1.mpq;
    var flage2 = regNum.test(mpq);
    if (!flage2) {
        //topWin.layer.msg('MPQ只能输入数字，请重新输入');
        //return false;
    }

    var cif = postData1.cif;
    var flage3 = regNum.test(cif);
    if (!flage3) {
        //topWin.layer.msg('货期(P/L)只能输入数字，请重新输入');
        //return false;
    }

    var o = [];
    var n = [];
    $("input[name='oldName']").each(function (v, r) {
        o.push($(this).val())
    });
    $("input[name='newName']").each(function (v, r) {
        n.push($(this).val())
    });
    let postData = $(el.form).serializeJson()

    $.ajax({
        url: g.ctx + "pc/supplier/avl/rest/save",
        type: "POST",
        contentType: "application/json",
        data: JSON.stringify(postData),
        success: function (data) {
            call()
        },
        error: function (data) {
            topWin.layer.alert(data.responseJSON.message, {icon: 2});
        }
    });
}



