/**
 * 数据管理-采购组管理新增编辑页面JS
 *
 * Created by li.kui on 20203/17.
 */
'use strict';
var el = {
    dataForm: '#dataForm',
    id: '#id',
    purchaseGroupCode: '#purchaseGroupCode',
    purchaseGroupName: '#purchaseGroupName',
    orgCode: '#orgCode',
    autoDeploy: '#autoDeploy',
    orgName: '#orgName'
};

$(function () {
    $(el.orgName).textbox({
        editable: false,
        required: true,
        tipPosition: 'bottom',
        icons: [
            {
                iconCls: 'icon-search',
                handler: function () {
                    choseOrg();
                }
            }
        ]
    });

    // 初始化使用状态下拉框
    $('#autoDeploy').combobox({
        valueField: 'value',
        textField: 'label',
        data: [
            {label: '是', value: "1"},
            {label: '否', value: "2"}
        ],
        required: true,
        editable: false,
        panelHeight: 'auto',
    });

    $(el.orgName).textbox('textbox').css('cursor', 'pointer');
    $(el.orgName).textbox('textbox').bind('click', function () {
        choseOrg();
    });
});


/**
 * 选择组织机构数据
 */
function choseOrg() {

    var purchaseOrgyDialog = openDialog('purchaseOrgyDialog', g.ctx + 'pc/sys/org/orgTree', {
        title: "组织部门",
        width: '400px',
        height: '700px',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            purchaseOrgyDialog.dialog('destroy');
        },
        buttons: [
            {
                text: itablei8n.save,
                handler: function () {
                    topWin.frames['purchaseOrgyDialog'].confirm(function (cbox) {

                            $(el.orgCode).val(cbox.code);
                            $(el.orgName).textbox('setValue', cbox.name);

                        purchaseOrgyDialog.dialog('close');
                    });
                }
            },
            {
                text: itablei8n.close,
                handler: function () {
                    purchaseOrgyDialog.dialog('close');
                }
            }
        ]
    });
}


/**
 * 数据提交
 *
 * @param callback 回调函数
 */
function saveOrUpdate(callback) {
    if($(el.dataForm).form('validate')){
        var requestData = {};
        requestData.id = $(el.id).val();
        requestData.purchaseGroupCode = $.trim($(el.purchaseGroupCode).textbox('getValue'));
        requestData.purchaseGroupName = $.trim($(el.purchaseGroupName).textbox('getValue'));
        requestData.orgCode = $.trim($(el.orgCode).val());
        requestData.orgName = $.trim($(el.orgName).textbox('getValue'));
        requestData.autoDeploy = $.trim($(el.autoDeploy).textbox('getValue'));
    console.log("requestData:" + JSON.stringify(requestData))
    var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
    $.ajax({
        url: g.ctx + 'pc/sys/purchaseGroup/rest/saveOrUpdate',
        type: 'POST',
        data: requestData,
        success: function (data) {
            callback();
        },
        complete: function(data) {
            top.layer.close(loading);
        },
        error: function (data) {
            topWin.layer.alert(data.responseJSON.message, {icon: 2});
        }
    });
    }else{
        layer.msg("红色框为必输项，请填写！");
    }
}