/**
 * 初始化企业标签审核配置详情对话框
 */
var EnterprisetypePropertiesInfoDlg = {
    enterprisetypePropertiesInfoData: {},
    validateFields: {
        companyId: {
            validators: {
                notEmpty: {
                    message: '审核单位不能为空'
                }
            }
        }
    }
};

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

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

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

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

/**
 * 收集数据
 */
EnterprisetypePropertiesInfoDlg.collectData = function () {
    this
            .set('id')
            .set('enterpriseTag')
            .set('organizationTag')
            .set('institutionTag')
            .set('uniCode')
            .set('description');
    var companys = $("#companyId").val();
    /*var ids = "";
     for (var key in companys) {
     if (companys[key] != null && companys[key] != '') {
     ids = ids + companys[key] + ",";
     }
     }*/
    this.enterprisetypePropertiesInfoData['companyId'] = companys;
}

/**
 * 提交添加
 */
EnterprisetypePropertiesInfoDlg.addSubmit = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    if (this.enterprisetypePropertiesInfoData.companyId == null || this.enterprisetypePropertiesInfoData.companyId == '') {
        Feng.error("审核单位不能为空");
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/admin/enterprise_verify_mgr/add", function (data) {
        if (data.code == "200") {
            Feng.success(data.msg);
            window.parent.EnterprisetypeProperties.table.refresh();
            EnterprisetypePropertiesInfoDlg.close();
        } else {
            Feng.error(data.msg);
        }
    }, function (data) {
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.enterprisetypePropertiesInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
EnterprisetypePropertiesInfoDlg.editSubmit = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    if (this.enterprisetypePropertiesInfoData.companyId == null || this.enterprisetypePropertiesInfoData.companyId == '') {
        Feng.error("审核单位不能为空");
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/admin/enterprise_verify_mgr/edit", function (data) {
        if (data.code == "200") {
            Feng.success(data.msg);
            window.parent.EnterprisetypeProperties.table.refresh();
            EnterprisetypePropertiesInfoDlg.close();
        } else {
            Feng.error(data.msg);
        }
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.enterprisetypePropertiesInfoData);
    ajax.start();
}

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

$(function () {
    Feng.initValidator("enterpriseTypeInfoForm", EnterprisetypePropertiesInfoDlg.validateFields);
    //下拉框数据动态加载
    Feng.addAjaxSelect({
        "id": "enterpriseTag",
        "displayCode": "code",
        "displayName": "name",
        "type": "GET",
        "url": "/admin/dict/findChildDictByCode?code=enterprise_tag"
    });
    Feng.addAjaxSelect({
        "id": "organizationTag",
        "displayCode": "code",
        "displayName": "name",
        "type": "GET",
        "url": "/admin/dict/findChildDictByCode?code=organization_tag"
    });
    Feng.addAjaxSelect({
        "id": "institutionTag",
        "displayCode": "code",
        "displayName": "name",
        "type": "GET",
        "url": "/admin/dict/findChildDictByCode?code=institution_tag"
    });
    Feng.addAjaxSelect({
        "id": "companyId",
        "displayCode": "id",
        "displayName": "name",
        "type": "GET",
        "url": Feng.ctxPath + "/common/api/getCompanyKvs"
    });
    //下拉框数据回显
    $("select").each(function () {
        $(this).val($(this).attr("selectVal"));
    });
});
