/**
 * 人才类别变更管理初始化
 */
var TalentTypeChange = {
    id: "TalentTypeChangeTable", //表格id
    seItem: null, //选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
TalentTypeChange.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '姓名', field: 'talentName', visible: true, align: 'center', valign: 'middle', width: "80px", 'class': 'uitd_showTip'},
        {title: '原公布入选月份', field: 'oldIdentifyMonth', visible: true, align: 'center', valign: 'middle', width: "90px", },
        {title: '原人才层次', field: 'oldTalentArrangeName', visible: true, align: 'center', valign: 'middle', width: "90px", 'class': 'uitd_showTip'},
        {title: '原认定条件', field: 'oldIdentifyCondition', visible: true, align: 'center', valign: 'middle', width: "120px", 'class': 'uitd_showTip'},
        {title: '原认定条件证书取得时间', field: 'oldIdentifyGetTime', visible: true, align: 'center', valign: 'middle', width: "130px", 'class': 'uitd_showTip'},
        {title: '新公布入选月份', field: 'newIdentifyMonth', visible: true, align: 'center', valign: 'middle', width: "80px"},
        {title: '新人才层次', field: 'newTalentArrangeName', visible: true, align: 'center', valign: 'middle', width: "80px"},
        {title: '新认定条件', field: 'newIdentifyCondition', visible: true, align: 'center', valign: 'middle', width: "120px", 'class': 'uitd_showTip'},
        {title: '新认定条件证书取得时间', field: 'newIdentifyGetTime', visible: true, align: 'center', valign: 'middle', width: "130px"},
        {title: '审核状态', field: 'checkState', visible: true, align: 'center', valign: 'middle', width: "80px",
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "<span class='label'>待提交</span>"
                } else if (value == 2) {
                    return "<span class='label label-success'>待总院审核</span>"
                } else if (value == 10 || value == 5) {
                    return "<span class='label label-danger'>已驳回</span>"
                } else if (value == 9) {
                    return "<span class='label label-danger'>初审驳回</span>"
                } else if (value == 8) {
                    return "<span class='label label-danger'>总院驳回</span>"
                } else {
                    if (row.isPublic >= 5) {
                        if (value == -1) {
                            return "<span class='label label-danger'>审核不通过</span>"
                        } else if (value == 35) {
                            return "<span class='label label-primary'>认定通过</span>"
                        } else {
                            return "<span class='label label-success'>审核中</span>"
                        }
                    } else {
                        return "<span class='label label-success'>审核中</span>"
                    }
                }
            }
        },
        {title: '操作', field: 'id', visible: true, align: 'center', valign: 'middle', width: "80px",
            formatter: function (value, row, index) {
                return "<span class='label label-success' onclick=\"TalentTypeChange.showLog('" + value + "')\" >" +
                        "<i class=\"fa fa-book\"></i>日志" +
                        "</span>";
            }
        },
    ];
};

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

/**
 * 点击添加人才类别变更
 */
TalentTypeChange.openAddTalentTypeChange = function () {
    var ajax = new $ax("/common/batch/checkBatchValid", function (data) {
        if (data.code == 200) {
            var index = layer.open({
                type: 2,
                title: '添加人才层次变更',
                area: ['800px', '420px'], //宽高
                fix: false, //不固定
                maxmin: true,
                content: Feng.ctxPath + '/enterprise/talent_type_change/apply?year=' + data.batch,
                btn: ['<i class="fa fa-eye"></i>&nbsp;&nbsp;保存未提交', '<i class="fa fa-check layui-bg-green"></i>&nbsp;&nbsp;提交审核', '<i class="fa fa-eraser"></i>&nbsp;&nbsp;取消'],
                btnAlign: 'c',
                btn1: function (index, layero) {
                    var obj = layero.find("iframe")[0].contentWindow;
                    obj.TalentTypeChangeInfoDlg.addSubmit();
                }, btn2: function (index, layero) {
                    var obj = layero.find("iframe")[0].contentWindow;
                    obj.TalentTypeChangeInfoDlg.editSubmit(2);
                    return false;
                },
                success: function (layero, index) {
                    layer.tips('添加基本信息并上传附件后点击', '.layui-layer-btn1', {tips: [1, "#78BA32"], time: 0, closeBtn: 2});
                },
                end: function () {
                    layer.closeAll('tips');
                }
            });
            TalentTypeChange.layerIndex = index;
            layer.full(index);
        } else {
            Feng.info(data.msg);
        }
    }, function (data) {
        Feng.error("校验失败!" + data.responseJSON.message + "!");
    });
    ajax.set("type", CONFIG.project_levelchange);
    ajax.start();
};

/**
 * 打开查看人才类别变更详情
 */
TalentTypeChange.openTalentTypeChangeDetail = function () {
    if (this.check()) {
        var ajax = new $ax("/common/batch/checkBatchValid", function (data) {
            if (data.code == 200) {
                var type = $("#type").val();
                if (TalentTypeChange.seItem.checkState != 1 && !(type != 5 && TalentTypeChange.seItem.checkState == 10) && !(type == 5 && TalentTypeChange.seItem.checkState == 8) && !(type == 5 && TalentTypeChange.seItem.checkState == 9)) {
                    Feng.info("正在审核中,无法修改");
                    return;
                }
                var index = layer.open({
                    type: 2,
                    title: '人才层次变更详情',
                    area: ['800px', '420px'], //宽高
                    fix: false, //不固定
                    maxmin: true,
                    content: Feng.ctxPath + '/enterprise/talent_type_change/apply/id/' + TalentTypeChange.seItem.id,
                    btn: ['<i class="fa fa-eye"></i>&nbsp;&nbsp;保存未提交', '<i class="fa fa-check layui-bg-green"></i>&nbsp;&nbsp;提交审核', '<i class="fa fa-eraser"></i>&nbsp;&nbsp;取消'],
                    btnAlign: 'c',
                    btn1: function (index, layero) {
                        var obj = layero.find("iframe")[0].contentWindow;
                        obj.TalentTypeChangeInfoDlg.addSubmit();
                    }, btn2: function (index, layero) {
                        var obj = layero.find("iframe")[0].contentWindow;
                        obj.TalentTypeChangeInfoDlg.editSubmit(2);
                        return false;
                    },
                    success: function (layero, index) {
                        layer.tips('添加基本信息并上传附件后点击', '.layui-layer-btn1', {tips: [1, "#78BA32"], time: 0, closeBtn: 2});
                    },
                    end: function () {
                        layer.closeAll('tips');
                    }
                });
                TalentTypeChange.layerIndex = index;
                layer.full(index);
            } else {
                Feng.info(data.msg);
            }
        }, function (data) {
            Feng.error("校验失败!" + data.responseJSON.message + "!");
        });
        ajax.set("type", CONFIG.project_levelchange);
        ajax.set("year", TalentTypeChange.seItem.newYear);
        ajax.set("first_submit_time", TalentTypeChange.seItem.firstSubmitTime);
        ajax.start();
    }
};

/**
 * 打开查看人才类别变更详情
 */
TalentTypeChange.openTalentTypeChangeSelect = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '人才层次变更详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/enterprise/talent_type_change/view/id/' + TalentTypeChange.seItem.id + "/2",
        });
        TalentTypeChange.layerIndex = index;
        layer.full(index);
    }
};



/**
 * 删除人才类别变更
 */
TalentTypeChange.delete = function () {
    if (this.check()) {
        if (TalentTypeChange.seItem.checkState != 1 && TalentTypeChange.seItem.checkState != 10) {
            Feng.info("正在审核中,无法删除");
            return;
        }
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/enterprise/talent_type_change/delete", function (data) {
                Feng.success("删除成功!");
                TalentTypeChange.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", TalentTypeChange.seItem.id);
            ajax.start();
        }
        Feng.confirm("删除后无法恢复，确认删除吗?", operation);
    }
};


/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
TalentTypeChange.formParams = function () {
    var queryData = {};
    queryData['talentName'] = $("#talentName").val();
    queryData['oldTalentType'] = $("#oldTalentType").val();
    queryData['oldTalentArrange'] = $("#oldTalentArrange").val();
    queryData['newTalentType'] = $("#newTalentType").val();
    queryData['newTalentArrange'] = $("#newTalentArrange").val();
    queryData['checkState'] = $("#checkState").val();
    queryData['oldIdentifyCondition'] = $("#oldIdentifyCondition").val();
    queryData['newIdentifyCondition'] = $("#newIdentifyCondition").val();
    return queryData;
}

/**
 * 重置
 */
TalentTypeChange.reset = function () {
    $("#talentName").val("");
    $("#oldTalentType").val("");
    $("#oldTalentArrange").val("").trigger("change");
    $("#newTalentType").val("");
    $("#newTalentArrange").val("").trigger("change");
    $("#checkState").val("");
    $("#oldIdentifyCondition").val("");
    $("#newIdentifyCondition").val("");
}

/**
 * 显示审核日志
 */
TalentTypeChange.showLog = function (id) {
    layer.open({
        type: 1,
        title: "日志",
        fixed: false,
        content: '<table id="' + id + '"></table>',
        area: ['80%', '80%'],
        maxmin: true,
        success: function (layero, index) {
            Feng.getCheckLog(id, {"type": CONFIG.project_levelchange, "mainId": id, "typeFileId": "", "active": 1})
        }
    });

}

/**
 * 查询人才类别变更列表
 */
TalentTypeChange.search = function () {
    TalentTypeChange.table.refresh({query: TalentTypeChange.formParams()});
};

/**
 * 获取人才认定
 */
TalentTypeChange.getIdentifyCondition = function (type) {
    var level = $("#" + type + "TalentArrange").val();
    if (level == null || level == '') {
        $("#" + type + "IdentifyCondition").empty();
        $("#" + type + "IdentifyCondition").trigger('chosen:updated');
        return;
    }
    Feng.addAjaxSelect({
        "id": type + "IdentifyCondition",
        "displayCode": "id",
        "displayName": "name",
        "type": "GET",
        "url": Feng.ctxPath + "/common/api/findIdentifyConditionByLevel/level/" + level + "/showAll/1"
    });
    $("#" + type + "IdentifyCondition").trigger('chosen:updated');
}

$(function () {
    var defaultColunms = TalentTypeChange.initColumn();
    var table = new BSTable(TalentTypeChange.id, "/enterprise/talent_type_change/list", defaultColunms);
    table.setPaginationType("server");
    table.setOnDblClickRow(function () {
        TalentTypeChange.openTalentTypeChangeDetail();
    });
    TalentTypeChange.table = table.init();
    //批量加载字典表数据
    var arr = [
        {"name": "oldTalentArrange", "code": "talent_arrange"},
        {"name": "newTalentArrange", "code": "talent_arrange"}]
    Feng.findChildDictBatch(JSON.stringify(arr));
    $("#oldIdentifyCondition,#newIdentifyCondition").chosen({
        search_contains: true,       //关键字模糊搜索。设置为true，只要选项包含搜索词就会显示；设置为false，则要求从选项开头开始匹配
        disable_search: false,
        width: "100%",
        enable_split_word_search: true
    });
});
