/**
 * 初始化单位详情对话框
 */
var CompanyInfoDlg = {
    companyInfoData: {},
    zTreeInstance: null,
    validateFields: {
        name: {
            validators: {
                notEmpty: {
                    message: '单位名称不能为空'
                }
            }
        },
        shortName: {
            validators: {
                notEmpty: {
                    message: '单位简称不能为空'
                }
            }
        },
        code: {
            validators: {
                notEmpty: {
                    message: '统一社会信用代码不能为空'
                }
            }
        },
        sn: {
            validators: {
                notEmpty: {
                    message: '排序不能为空'
                }
            }
        }
    }
};

/**
 * 清除数据
 */
CompanyInfoDlg.clearData = function () {
    this.companyInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
CompanyInfoDlg.set = function (key, val) {
    this.companyInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
CompanyInfoDlg.get = function (key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
CompanyInfoDlg.close = function () {
    parent.layer.close(window.parent.Company.layerIndex);
}


/**
 * 收集数据
 */
CompanyInfoDlg.collectData = function () {
    this.set('id')
            .set('name')
            .set('shortName')
            .set('code')
            .set('description')
            .set('sn');
}

/**
 * 验证数据是否为空
 */
CompanyInfoDlg.validate = function () {
    $('#companyInfoForm').data("bootstrapValidator").resetForm();
    $('#companyInfoForm').bootstrapValidator('validate');
    return $("#companyInfoForm").data('bootstrapValidator').isValid();
}

/**
 * 提交添加单位
 */
CompanyInfoDlg.addSubmit = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    //提交信息
    var ajax = new $ax("/admin/company/add", function (data) {
        Feng.success(data.msg);
        if (data.code == "200") {
            window.parent.Company.table.refresh();
            CompanyInfoDlg.close();
        }
    }, function (data) {
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.companyInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
CompanyInfoDlg.editSubmit = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    //提交信息
    var ajax = new $ax("/admin/company/edit", function (data) {
        Feng.success(data.msg);
        if (data.code == "200") {
            window.parent.Company.table.refresh();
            CompanyInfoDlg.close();
        }
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.companyInfoData);
    ajax.start();
}

function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "parentDeptMenu" || $(
            event.target).parents("#parentDeptMenu").length > 0)) {
        CompanyInfoDlg.hideDeptSelectTree();
    }
}

$(function () {
    Feng.initValidator("companyInfoForm", CompanyInfoDlg.validateFields);
});
