/**
 * 离职管理管理初始化
 */
var TalentQuit = {
    id: "TalentQuitTable", //表格id
    seItem: null, //选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
TalentQuit.initColumn = function () {
    var type = $("#type").val();
    var isShow = true;
    if (type == 2) {
        isShow = false;
    }
    return [
        {field: 'selectItem', radio: true},
        {title: '姓名', field: 'talentName', visible: true, align: 'center', valign: 'middle', width: "80px", 'class': 'uitd_showTip'},
        {title: '离职企业', field: 'enterpriseName', visible: true, align: 'center', valign: 'middle', width: "120px", 'class': 'uitd_showTip'},
        {title: '证件号码', field: 'idCard', visible: true, align: 'center', valign: 'middle', width: "150px", 'class': 'uitd_showTip'},
        {title: '人才标签', field: 'talentTypeName', visible: isShow, align: 'center', valign: 'middle', width: "100px", 'class': 'uitd_showTip'},
        {title: '人才层次', field: 'talentArrangeName', visible: true, align: 'center', valign: 'middle', width: "80px", 'class': 'uitd_showTip'},
        {title: '认定时间', field: 'identifyTime', visible: true, align: 'center', valign: 'middle', width: "100px", 'class': 'uitd_showTip'},
        {title: '合同开始时间', field: 'starttime', visible: true, align: 'center', valign: 'middle', width: "100px", 'class': 'uitd_showTip'},
        {title: '合同结束时间', field: 'endtime', visible: true, align: 'center', valign: 'middle', width: "100px", 'class': 'uitd_showTip'},
        {title: '离职时间', field: 'quitTime', visible: true, align: 'center', valign: 'middle', width: "100px", 'class': 'uitd_showTip'},
        {title: '离职申报原因', field: 'quitReason', visible: isShow, align: 'center', valign: 'middle', width: "120px", 'class': 'uitd_showTip'},
        {title: '审核状态', field: 'checkState', visible: true, align: 'center', valign: 'middle', width: "80px",
            formatter: function (value, row, index) {
                if (value == -1) {
                    return '<span class=\'label\'>待提交</span>';
                }
                if (value == 1) {
                    return '<span class=\'label label-success\'>待审核</span>';
                }
                if (value == 2) {
                    return '<span class=\'label label-danger\'>已驳回</span>';
                }
                if (value == 3) {
                    return '<span class=\'label label-primary\'>已通过</span>';
                }
                if (value == 9) {
                    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=\"TalentQuit.showLog('" + value + "')\" >" +
                        "<i class=\"fa fa-book\"></i>日志" +
                        "</span>";
            }
        }
    ];
};

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

/**
 * 点击添加离职管理
 */
TalentQuit.openAddTalentQuit = function () {
    var ajax = new $ax(Feng.ctxPath + "/common/batch/checkBatchValid", function (data) {
        if (data.code == 200) {
            var index = layer.open({
                type: 2,
                title: '添加离职管理',
                area: 'auto', //宽高
                fix: false, //不固定
                maxmin: true,
                content: Feng.ctxPath + '/enterprise/talent_quit/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.TalentQuitInfoDlg.addSubmit();
                }, btn2: function (index, layero) {
                    var obj = layero.find("iframe")[0].contentWindow;
                    obj.TalentQuitInfoDlg.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');
                }
            });
            TalentQuit.layerIndex = index;
            layer.full(index);
        } else {
            Feng.info(data.msg);
        }
    }, function (data) {
        Feng.error("校验失败!" + data.responseJSON.message + "!");
    });
    ajax.set("type", CONFIG.project_quit);
    ajax.start();
};

/**
 * 打开查看离职管理详情
 */
TalentQuit.openTalentQuitDetail = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/common/batch/checkBatchValid", function (data) {
            if (data.code == 200) {
                if (TalentQuit.seItem.checkState == 1) {
                    Feng.error("申报正在审核中");
                    return;
                }
                if (TalentQuit.seItem.checkState == 3) {
                    Feng.error("审核通过，无法修改");
                    return;
                }
                var index = layer.open({
                    type: 2,
                    title: '离职管理详情',
                    area: ['800px', '420px'], //宽高
                    fix: false, //不固定
                    maxmin: true,
                    content: Feng.ctxPath + '/enterprise/talent_quit/apply/id/' + TalentQuit.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.TalentQuitInfoDlg.addSubmit();
                    }, btn2: function (index, layero) {
                        var obj = layero.find("iframe")[0].contentWindow;
                        obj.TalentQuitInfoDlg.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');
                    }
                });
                TalentQuit.layerIndex = index;
                layer.full(index);
            } else {
                Feng.info(data.msg);
            }
        }, function (data) {
            Feng.error("校验失败!" + data.responseJSON.message + "!");
        });
        ajax.set("type", CONFIG.project_quit);
        ajax.set("year", TalentQuit.seItem.year);
        ajax.start();
    }
};

/**
 * 打开查看离职管理详情
 */
TalentQuit.openTalentQuitSelect = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '离职管理详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/enterprise/talent_quit/view/id/' + TalentQuit.seItem.id,
        });
        TalentQuit.layerIndex = index;
        layer.full(index);
    }
};


/**
 * 删除离职管理
 */
TalentQuit.delete = function () {
    if (this.check()) {
        if (TalentQuit.seItem.checkState != -1) {
            Feng.error("已提交审核，无法删除");
            return;
        }
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/enterprise/talent_quit/delete", function (data) {
                if (data.code == 200) {
                    Feng.success(data.msg);
                    TalentQuit.table.refresh();
                } else {
                    Feng.error(data.msg);
                }
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", TalentQuit.seItem.id);
            ajax.start();
        }
        Feng.confirm("删除后无法恢复，确认删除吗?", operation);
    }
};


/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
TalentQuit.formParams = function () {
    var queryData = {};
    queryData['talentName'] = $("#talentName").val();
    queryData['idCard'] = $("#idCard").val();
    queryData['enterpriseName'] = $("#companyName").val();
    queryData['talentArrange'] = $("#talentArrange").val();
    return queryData;
}

/**
 * 重置
 */
TalentQuit.reset = function () {
    $("#talentName").val("");
    $("#idCard").val("");
    $("#enterpriseName").val("");
    $("#talentArrange").val("");
}


/**
 * 查询离职管理列表
 */
TalentQuit.search = function () {
    TalentQuit.table.refresh({query: TalentQuit.formParams()});
};

/**
 * 显示审核日志
 */
TalentQuit.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_quit, "mainId": id, "typeFileId": "", "active": 1})
        }
    });

}

$(function () {
    var defaultColunms = TalentQuit.initColumn();
    var table = new BSTable(TalentQuit.id, "/enterprise/talent_quit/list", defaultColunms);
    table.setPaginationType("server");
    TalentQuit.table = table.init();
    Feng.addAjaxSelect({
        "id": 'talentArrange',
        "displayCode": "code",
        "displayName": "name",
        "type": "GET",
        "url": Feng.ctxPath + "/common/tool/findChildDictByCode?code=talent_arrange"
    });
});
