/**
 * 离职管理管理初始化
 */
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: '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:"150px",'class': 'uitd_showTip'},
        {title: '离职企业', field: 'enterpriseName', visible: true, align: 'center', valign: 'middle',width:"120px",'class': 'uitd_showTip'},
        {title: '人才标签', field: 'talentTypeName', visible: true, 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.openTalentQuitCheck = function () {
    if (this.check()) {
        if(TalentQuit.seItem.checkState!=1 && TalentQuit.seItem.checkState!=9){
            Feng.info("不在审核范围内");
            return ;
        }
        var index = layer.open({
            type: 2,
            title: '离职管理审核',
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/talentQuit/talentQuit_check/' + TalentQuit.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.TalentQuitInfoDlg.submitCheck();
            }

        });
        TalentQuit.layerIndex = index;
        layer.full(index);
    }
};

/**
 * 打开查看离职管理页面
 */
TalentQuit.openTalentQuitDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '离职管理详情',
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/talentQuit/talentQuit_check/' + TalentQuit.seItem.id
        });
        TalentQuit.layerIndex = index;
        layer.full(index);
    }
};


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

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


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

/**
 * 导出
 */
TalentQuit.export = function (){
    var queryData = TalentQuit.formParams();
    var url = Feng.ctxPath + "/talentQuit/export?" +
        "&talentName=" + queryData.talentName +
        "&idCard=" + queryData.idCard +
        "&enterpriseName=" + queryData.enterpriseName +
        "&talentArrange=" + queryData.talentArrange +
        "&checkState=" + queryData.checkState ;
    window.location.href = encodeURI(encodeURI(url));
}


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


/**
 * 显示审核日志
 */
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, "/talentQuit/list", defaultColunms);
    table.setPaginationType("server");
    TalentQuit.table = table.init();
    Feng.addAjaxSelect({
        "id": 'talentArrange',
        "displayCode": "code",
        "displayName": "name",
        "type": "GET",
        "url": Feng.ctxPath + "/api/common/findChildDictByCode?code=un_talentLevel"
    });
});
