var el = {
    form : '#form',
    useState: '#useState',
    factoryCode: '#factoryCode',
    materialCode: '#materialCode',
    componentCode: '#componentCode',
}
$(function () {
    $(el.factoryCode).combobox({
        url: g.ctx + 'pc/sys/factory/rest/listAll',
        editable: false,
        valueField: 'code',
        textField: 'codeWithDesc',
        prompt: '--- 请选择 ---',
        panelHeight: 'auto',
        onLoadSuccess: function () { //加载完成后,设置选中第一项
            // var val = $(this).combobox("getData");
            // for (var item in val[0]) {
            //     if (item === "code") {
            //         $(this).combobox("select", val[0][item]);
            //         loadGrid();
            //     }
            // }
        },
        onChange: function(newValue, oldValue) {
            // 处理选项改变事件
            if (newValue !== oldValue) {
                // 用户更改了选项，执行相应操作
                $("#materialCode").textbox('setValue','');
                $('#materialName').textbox('setValue','');
                $("#componentCode").textbox('setValue','');
                $('#componentName').textbox('setValue','');
            }
        }
    });
});

/**
 * 选择物料页面
 */
function selectMaterial(type) {
    var factoryCode = $(el.factoryCode).combobox('getValue');

    if(
        factoryCode === '' || factoryCode === null || factoryCode === undefined
    ){
        layer.alert("请先选择工厂");
        return;
    }
    var selectMaterialElement = openDialog("selectMaterialDialog", g.ctx + 'pc/sys/product/select/dialog?single=1&factoryCode='+factoryCode, {
        title: '选择物料',
        width: '80%',
        height: '75%',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            selectMaterialElement.dialog('destroy');
        },
        buttons: [
            {
                text: itablei8n.confirm,
                handler: function () {
                    topWin.frames['selectMaterialDialog'].chooseRows(function (cbox) {console.log(cbox)
                        if(type === 1){
                            $("#materialCode").textbox('setValue',cbox.materialCode);
                            $('#materialName').textbox('setValue',cbox.materialName);
                        }
                        if(type === 2){
                            $("#componentCode").textbox('setValue',cbox.materialCode);
                            $('#componentName').textbox('setValue',cbox.materialName);
                        }
                        selectMaterialElement.dialog('close');
                    });
                }
            },
            {
                text: itablei8n.close,
                handler: function () {
                    selectMaterialElement.dialog('close');
                }
            }
        ]


    });
}
function materialSelect1(){
    selectMaterial(1);
}
function materialSelect2(){
    selectMaterial(2);
}
function saveOrUpdate(call) {
    if($(el.form).form('validate')){
        var formData = $(el.form).serializeJson();
        var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
        $.ajax({
            url: g.ctx + 'pc/order/purchaseOrderBom/rest/saveOrUpdate',
            method: 'post',
            data: formData,
            success: function (data) {
                call();
            },
            complete: function(data) {
                top.layer.close(loading);
            },
            error: function (data) {
                layer.alert(JSON.parse(data.responseText).message);
            }
        });
    }else{
        layer.msg("红色框为必输项，请填写！");
    }
}