/**
 * 样品申请创建页面
 * view/order/deliveryOrderControl/delivery_order_control_add.ftl
 */
var el = {
    form: '#form',
    openProuctChooseDialog: null,
    supplierCode: '#supplierCode',
    supplierName: '#supplierName',
    materialCode: '#materialCode',
    materialName: '#materialName',
    applyUser: '#applyUser',
    manageUser: '#manageUser',
    effectiveDate: '#effectiveDate',
    isSupplier: "#isSupplier",
    supplierId: "#supplierId",
    remark: '#remark',
    controlNum: '#controlNum',
}, isSupplier = $(el.isSupplier).val() !== 'false';


$(function () {
});

/**
 * 选择物料信息对话框
 */
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: '100%',
        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 effectiveDate = $(el.effectiveDate).val();
    if (!effectiveDate) {
        topWin.layer.msg('请选择生效日期');
        return false;
    }

    const regNum = /^\d+(\.\d+)?$/;
    let postData1 = $(el.form).serializeJson();

    var controlNum = postData1.controlNum;
    var flage1 = regNum.test(controlNum);
    if (!flage1) {
        topWin.layer.msg('送货单管理数量只能输入数字，请重新输入');
        return false;
    }
    let postData = $(el.form).serializeJson()

    $.ajax({
        url: g.ctx + "pc/order/deliveryOrderControl/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});
        }
    });
}



