/**
 * 认定条件管理管理初始化
 */
var IdentifyCondition = {
    id: "IdentifyConditionTable", //表格id
    seItem: null, //选中的条目
    table: null,
    layerIndex: -1
};

var isShow = false;
/**
 * 初始化表格的列
 */
IdentifyCondition.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '人才层次', field: 'talentLevel', visible: true, align: 'center', valign: 'middle'},
        {title: '人才类别', field: 'type', visible: true, align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "晋江市现代产业体系人才";
                }
                if (value == 2) {
                    return "集成电路优秀人才";
                }
            }
        },
        {title: '名称', field: 'name', visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip'},
        {title: '有效期', field: 'activeYear', visible: true, align: 'center', valign: 'middle'},
        {title: '是否启用', field: 'active', visible: true, align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "<button type=\"button\" style=\"line-height: 1.3\" class=\"btn btn-primary btn-xs\">启用</button>";
                }
                if (value == 2) {
                    return "<button type=\"button\" style=\"line-height: 1.3\" class=\"btn btn-warning btn-xs\">停用</button>";
                }
            }
        },
        {title: '审核单位', field: 'companyNames', visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip'},
        {title: '备注', field: 'description', visible: true, align: 'center', valign: 'middle'},
    ];
};

/**
 * 检查是否选中
 */
IdentifyCondition.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        IdentifyCondition.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加认定条件管理
 */
IdentifyCondition.openAddIdentifyCondition = function () {
    var index = layer.open({
        type: 2,
        title: '添加认定条件',
        area: ['1126px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/admin/talent_condition/add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看认定条件管理详情
 */
IdentifyCondition.openIdentifyConditionDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '认定条件管理详情',
            area: ['1126px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/admin/talent_condition/edit/id/' + IdentifyCondition.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除认定条件管理
 */
IdentifyCondition.delete = function () {
    if (this.check()) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/admin/talent_condition/delete/id/", function (data) {
                Feng.success("删除成功!");
                IdentifyCondition.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", IdentifyCondition.seItem.id);
            ajax.start();
        }
        Feng.confirm("是否刪除该认定条件?", operation);
    }
};

/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
IdentifyCondition.formParams = function () {
    var queryData = {};
    queryData['talentLevel'] = $("#talentLevel").val();
    queryData['type'] = $("#type").val();
    queryData['name'] = $("#name").val();
    queryData['active'] = $("#active").val();
    return queryData;
}


/**
 * 查询认定条件管理列表
 */
IdentifyCondition.search = function () {
    IdentifyCondition.table.refresh({query: IdentifyCondition.formParams()});
};

/**
 * 重置
 */
IdentifyCondition.reset = function () {
    $("#talentLevel").val("");
    $("#type").val("");
    $("#name").val("");
    $("#active").val("");
}

IdentifyCondition.import = function () {
    $("#import-form")[0].reset();
    $("#importModal").modal("show");
}

IdentifyCondition.importSubmit = function () {
    $("#import-form")[0].submit();
}

//回调
IdentifyCondition.callBack = function (data) {
    Feng.info(data.msg);
    if (data.code == 200) {
        $("#importModal").modal("hide");
        IdentifyCondition.table.refresh();
    }
}

//模板下载
IdentifyCondition.download = function () {
    window.location.href = Feng.ctxPath + "/static/downloadFile/identifyConditiontemplate.xlsx";
}

$(function () {
    var userTYpe = $("#userType").val();
    if (userTYpe == 1) {
        isShow = true;
    } else {
        isShow = false;
    }
    var defaultColunms = IdentifyCondition.initColumn();
    var table = new BSTable(IdentifyCondition.id, "/admin/talent_condition/list", defaultColunms);
    table.setPaginationType("server");
    IdentifyCondition.table = table.init();
    //下拉框数据动态加载
    Feng.addAjaxSelect({
        "id": "talentLevel",
        "displayCode": "code",
        "displayName": "name",
        "type": "GET",
        "url": "/admin/dict/findChildDictByCode?code=talent_arrange"
    });
});
