/**
 * 工作单位变更管理初始化
 */
var TalentWorkunitChange = {
    id: "TalentWorkunitChangeTable", //表格id
    seItem: null, //选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
TalentWorkunitChange.initColumn = function () {
    var type = $("#type").val();
    var isShow = true;
    if (type == 2) {
        isShow = false;
    }
    return [
        {field: 'selectItem', radio: true},
        {title: '年度', field: 'year', visible: true, align: 'center', valign: 'middle', width: "80px", 'class': 'uitd_showTip'},
        {title: '姓名', field: 'talentName', visible: true, align: 'center', valign: 'middle', width: "80px", 'class': 'uitd_showTip'},
        {title: '证件号码', field: 'idCard', visible: true, align: 'center', valign: 'middle', width: "100px", 'class': 'uitd_showTip'},
        {title: '人才层次', field: 'talentArrangeName', visible: true, align: 'center', valign: 'middle', width: "100px", 'class': 'uitd_showTip'},
        {title: '原工作单位', field: 'oldEnterpriseName', visible: true, align: 'center', valign: 'middle', width: "120px", '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: 'newEnterpriseName', 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: 'phone', visible: true, align: 'center', valign: 'middle', width: "100px", 'class': 'uitd_showTip'},
        {title: '入职/变更时间', field: 'anyTime', visible: true, align: 'center', valign: 'middle', width: "100px", 'class': 'uitd_showTip'},
        {title: '单位变更申报原因', field: 'anyReason', visible: isShow, align: 'center', valign: 'middle', width: "120px", 'class': 'uitd_showTip'},
        {title: '审核状态', field: 'checkState', visible: true, align: 'center', valign: 'middle', width: "120px", 'class': 'uitd_showTip',
            formatter: function (value, row, index) {
                if (value == -2) {
                    return '<span class=\'label label-danger\'>审核不通过</span>';
                }
                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=\"TalentWorkunitChange.showLog('" + value + "')\" >" +
                        "<i class=\"fa fa-book\"></i>日志" +
                        "</span>";
            }
        },
    ];
};

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

/**
 * 点击工作单位变更审核
 */
TalentWorkunitChange.openTalentWorkunitChangeCheck = function () {
    if (this.check()) {
        if (TalentWorkunitChange.seItem.checkState != 1 && TalentWorkunitChange.seItem.checkState != 9) {
            Feng.error("不在审核范围内 ");
            return;
        }
        var index = layer.open({
            type: 2,
            title: '工作单位变更审核',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/admin/talentWorkUnitChange/check/id/' + TalentWorkunitChange.seItem.id,
            btn: ['<i class="fa fa-check"></i>&nbsp;&nbsp;审核', '<i class="fa fa-eraser"></i>&nbsp;&nbsp;取消'],
            btnAlign: 'c',
            yes: function (index, layero) {
                var obj = layero.find("iframe")[0].contentWindow;
                obj.TalentWorkunitChangeInfoDlg.submitCheck();
            }, end: function () {
                TalentWorkunitChange.table.refresh();
            }
        });
        TalentWorkunitChange.layerIndex = index;
        layer.full(index);
    }
};

/**
 * 打开查看工作单位变更详情
 */
TalentWorkunitChange.openTalentWorkunitChangeDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '工作单位变更详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/admin/talentWorkUnitChange/check/id/' + TalentWorkunitChange.seItem.id,
        });
        TalentWorkunitChange.layerIndex = index;
        layer.full(index);
    }
};


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

/**
 * 重置
 */
TalentWorkunitChange.reset = function () {
    $("#talentName").val("");
    $("#idCard").val("");
    $("#oldEnterpriseName").val("");
    //$("#oldTalentType").val("");
    $("#newEnterpriseName").val("");
    //$("#newTalentType").val("");
    $("#checkState").val("");
}


/**
 * 查询工作单位变更列表
 */
TalentWorkunitChange.search = function () {
    TalentWorkunitChange.table.refresh({query: TalentWorkunitChange.formParams()});
};

/**
 * 导出
 */
TalentWorkunitChange.export = function () {
    var queryData = TalentWorkunitChange.formParams();
    var url = Feng.ctxPath + "/admin/talentWorkUnitChange/export?" +
            "&talentName=" + queryData.talentName +
            "&idCard=" + queryData.idCard +
            "&oldEnterpriseName=" + queryData.oldEnterpriseName +
            "&newEnterpriseName=" + queryData.newEnterpriseName +
            "&checkState=" + queryData.checkState;
    window.location.href = encodeURI(encodeURI(url));
}


/**
 * 打包下载附件
 */
TalentWorkunitChange.download = function () {
    if (this.check()) {
        window.location.href = encodeURI(encodeURI(Feng.ctxPath + "/common/api/downloadZip?type=4&id=" + TalentWorkunitChange.seItem.id));
    }
}

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

}

$(function () {
    var defaultColunms = TalentWorkunitChange.initColumn();
    var table = new BSTable(TalentWorkunitChange.id, "/admin/talentWorkUnitChange/list", defaultColunms);
    table.setPaginationType("server");
    TalentWorkunitChange.table = table.init();
});
