/**
 * 工作单位变更管理初始化
 */
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: '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: 'talentArrangeName', visible: true, align: 'center', valign: 'middle',width:"80px",},
        {title: '原工作单位', field: 'oldEnterpriseName', visible: true, align: 'center', valign: 'middle',width:"120px",'class': 'uitd_showTip'},
        {title: '原人才标签', field: 'oldTalentTypeName', visible: isShow, align: 'center', valign: 'middle',width:"90px",'class': 'uitd_showTip'},
        {title: '原入职时间', field: 'oldAnyTime', visible: false, align: 'center', valign: 'middle',width:"80px",'class': 'uitd_showTip'},
        {title: '离职时间', field: 'quitTime', visible: false, align: 'center', valign: 'middle',width:"80px",'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:"120px",'class': 'uitd_showTip'},
        {title: '新人才标签', field: 'newTalentTypeName', visible: isShow, align: 'center', valign: 'middle',width:"80px",'class': 'uitd_showTip'},
        {title: '入职/变更时间', field: 'anyTime', visible: true, align: 'center', valign: 'middle',width:"80px",'class': 'uitd_showTip'},
        {title: '单位变更申报原因', field: 'anyReason', visible: isShow, align: 'center', valign: 'middle',width:"120px",'class': 'uitd_showTip'},
        {title: '合同起止时间', field: 'starttime', visible: true, align: 'center', valign: 'middle',width:"140px",'class': 'uitd_showTip',
            formatter :function (value,row,index) {
                if(row.starttime!=null && row.endtime!=''){
                    return row.starttime+"至"+row.endtime;
                }else{
                    return row.starttime+"至长期"
                }
            }
        },
        {title: '审核状态', field: 'checkState', visible: true, align: 'center', valign: 'middle',width:"80px",
            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.openAddTalentWorkunitChange = function () {
    var ajax = new $ax(Feng.ctxPath + "/api/commonBatch/valiateIsAdd", function (data) {
        if(data.code == 200){
            var index = layer.open({
                type: 2,
                title: '添加工作单位变更',
                area: ['800px', '420px'], //宽高
                fix: false, //不固定
                maxmin: true,
                content: Feng.ctxPath + '/api/talentWorkunitChange/talentWorkunitChange_add?year='+data.obj,
                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.TalentWorkunitChangeInfoDlg.addSubmit();
                },btn2: function(index, layero){
                    var obj = layero.find("iframe")[0].contentWindow;
                    obj.TalentWorkunitChangeInfoDlg.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');
                }
            });
            TalentWorkunitChange.layerIndex = index;
            layer.full(index);
        }else{
            Feng.info(data.msg);
        }
    }, function (data) {
        Feng.error("校验失败!" + data.responseJSON.message + "!");
    });
    ajax.set("type",CONFIG.project_workchange);
    ajax.start();
};

/**
 * 打开查看工作单位变更详情
 */
TalentWorkunitChange.openTalentWorkunitChangeDetail = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/api/commonBatch/valiateIsEditOrSubmit", function (data) {
            if (data.code == 200) {
                if(TalentWorkunitChange.seItem.checkState==1){
                    Feng.error("申报正在审核中");return;
                }if(TalentWorkunitChange.seItem.checkState==3){
                    Feng.error("审核通过，无法修改");return;
                }
                var index = layer.open({
                    type: 2,
                    title: '工作单位变更详情',
                    area: ['800px', '420px'], //宽高
                    fix: false, //不固定
                    maxmin: true,
                    content: Feng.ctxPath + '/api/talentWorkunitChange/talentWorkunitChange_update/' + TalentWorkunitChange.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.TalentWorkunitChangeInfoDlg.addSubmit();
                    },btn2: function(index, layero){
                        var obj = layero.find("iframe")[0].contentWindow;
                        obj.TalentWorkunitChangeInfoDlg.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');
                    }
                });
                TalentWorkunitChange.layerIndex = index;
                layer.full(index);
            }else{
                Feng.info(data.msg);
            }
        }, function (data) {
            Feng.error("校验失败!" + data.responseJSON.message + "!");
        });
        ajax.set("type",CONFIG.project_workchange);
        ajax.set("year",TalentWorkunitChange.seItem.year);
        ajax.start();
    }
};

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


/**
 * 删除工作单位变更
 */
TalentWorkunitChange.delete = function () {
    if (this.check()) {
        if(TalentWorkunitChange.seItem.checkState!=-1){
            Feng.error("已提交审核，无法删除");return;
        }
        var operation = function() {
            var ajax = new $ax(Feng.ctxPath + "/api/talentWorkunitChange/delete", function (data) {
                Feng.success("删除成功!");
                TalentWorkunitChange.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("talentWorkunitChangeId", TalentWorkunitChange.seItem.id);
            ajax.start();
        }
        Feng.confirm("删除后无法恢复，确认删除吗?", operation);
    }
};


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

/**
 * 重置
 */
TalentWorkunitChange.reset = function (){
    $("#talentName").val("");
    $("#idCard").val("");
    $("#oldEnterpriseId").val("");
    $("#newEnterpriseId").val("");
    $("#checkState").val("");
}


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

/**
 * 显示审核日志
 */
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, "/api/talentWorkunitChange/list", defaultColunms);
    table.setPaginationType("server");
    TalentWorkunitChange.table = table.init();
});
