/**
 * 人才认定申报管理初始化
 */
var TalentInfo = {
    id: "TalentInfoTable", //表格id
    seItem: null, //选中的条目
    table: null,
    layerIndex: -1
};
/**
 * 初始化表格的列
 */
TalentInfo.initColumn = function () {
    var type = $("#type").val();
    var isShow = true;
    if (type == 2) {
        isShow = false;
    }
    return [
        {field: 'selectItem', radio: true},
        {title: '人才类别', field: 'type', visible: true, align: 'center', valign: 'middle', width: "100px",
            formatter: function (value, row, index) {
                if (value == 1) {
                    return '晋江市现代产业体系人才';
                }
                if (value == 2) {
                    return '集成电路优秀人才';
                }
            }
        },
        {title: '姓名', field: 'name', visible: true, align: 'center', valign: 'middle', width: "100px",
            formatter: function (value, row, index) {
                if (row.sex == 1) {
                    return value + '<span style="color:#6495ED">【男】</span>';
                } else if (row.sex == 2) {
                    return value + '<span style="color:#FF82AB">【女】</span>';
                } else {
                    return value;
                }
            }
        },
        {title: '证件号码', field: 'card_number', visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip', width: "120px"},
        {title: '单位名称', field: 'enterpriseName', visible: isShow, align: 'center', valign: 'middle', 'class': 'uitd_showTip', width: "80px"},
        {title: '单位标签', field: 'enterpriseTagName', visible: isShow, align: 'center', valign: 'middle', 'class': 'uitd_showTip', width: "80px"},
        {title: '产业领域', field: 'industryName', visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip', width: "120px"},
        {title: '审核状态', field: 'checkState', visible: true, align: 'center', valign: 'middle', width: "100px",
            formatter: function (value, row, index) {
                if (row.real_state != row.checkState) {
                    switch (row.real_state) {
                        case 4:
                            return "<span class='label label-danger'>基础信息审核驳回</span>";
                            break;
                        case 6:
                            return "<span class='label label-danger'>基础信息复审驳回</span>";
                            break;
                        case 11:
                            return "<span class='label label-danger'>初审驳回</span>";
                            break;
                        case 13:
                            return "<span class='label label-danger'>部门驳回</span>";
                            break;
                        case 15:
                            return "<span class='label label-danger'>复审驳回</span>";
                            break;

                    }
                } else {
                    switch (value) {
                        case - 2:
                            return "<span class='label label-danger'>初审失败</span>";
                            break;
                        case - 1:
                            return "<span class='label label-danger'>基础信息审核失败</span>";
                            break;
                        case 7:
                            return "<span class='label label-danger'>基础信息复审失败</span>";
                            break;
                        case 1:
                            return "<span class='label'>保存未提交</span>";
                            break;
                        case 2:
                            if (row.last_state == 4) {
                                return "<span class='label label-success'>待审核（重新提交）</span>";
                            }
                            return "<span class='label label-success'>待审核</span>";
                            break;
                        case 3:
                            return "<span class='label label-success'>待基础条件复审</span>";
                            break;
                        case 5:
                            return "<span class='label'>待详细填报</span>";
                            break;
                        case 8:
                            return "<span class='label'>保存未提交(II)</span>";
                            break;
                        case 9:
                            if (row.last_state == 11) {
                                return "<span class='label label-success'>待初审（重新提交）</span>";
                            }
                            return "<span class='label label-success'>待初审</span>";
                            break;
                        case 10:
                            if (row.companyIds) {
                                return "<span class='label label-success'>待部门审核</span>";
                            } else {
                                return "<span class='label label-success'>待复审</span>";
                            }
                            break;
                        case 12:
                            return "<span class='label label-success'>待复审</span>";
                            break;
                        case 14:
                            return "<span class='label label-primary'>认定通过</span>";
                            break;
                        case 16:
                            return "<span class='label label-danger'>复审失败</span>";
                            break;
                    }
                }
            }
        },
        {title: '审核意见', field: 'checkMsg', visible: false, align: 'center', valign: 'middle', 'class': 'uitd_showTip', width: "100px",
            formatter: function (value, row, index) {
                if (value == 1) {
                    return ""
                } else if (value == 10 || value == 5) {
                    return value;
                } else {
                    if (row.isPublic >= 5) {
                        if (value == -1) {
                            return value
                        } else if (value == 35) {
                            return value;
                        } else {
                            return ""
                        }
                    } else {
                        return ""
                    }
                }
            }
        },
        {title: '操作', field: 'id', visible: true, align: 'center', valign: 'middle', width: "80px",
            formatter: function (value, row, index) {
                return "<span class='label label-success' onclick=\"TalentInfo.showLog('" + value + "')\" >" +
                        "<i class=\"fa fa-book\"></i>日志" +
                        "</span>";
            }
        }
    ];
};

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

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

}


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

/**
 * 打开查看人才认定申报详情
 */
TalentInfo.openTalentInfoDetail = function () {
    if (this.check()) {
        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: '/enterprise/base/add/id/' + TalentInfo.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.TalentInfoInfoDlg.addSubmit();
                    }, btn2: function (index, layero) {
                        var obj = layero.find("iframe")[0].contentWindow;
                        obj.TalentInfoInfoDlg.submitToCheck();
                        return false;
                    },
                    success: function (layero, index) {
                        layer.tips('添加基本信息并上传附件后点击', '.layui-layer-btn1', {tips: [1, "#78BA32"], time: 0, closeBtn: 2});
                    },
                    end: function () {
                        layer.closeAll('tips');
                    }
                });
                layer.full(index);
                TalentInfo.layerIndex = index;
            } else {
                Feng.info(data.msg);
            }
        }, function (data) {
            Feng.error("校验失败!" + data.responseJSON.message + "!");
        });
        ajax.set("type", CONFIG.project_rcrd);
        ajax.set("year", TalentInfo.seItem.year);
        ajax.start();
    }
};


/**
 * 查看
 */
TalentInfo.openTalentInfoSelect = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '基础条件申报',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: '/enterprise/base/view/id/' + TalentInfo.seItem.id,
            btn: ['<i class="fa fa-eraser"></i>&nbsp;&nbsp;取消'],
            btnAlign: 'c',
        });
        layer.full(index);
        TalentInfo.layerIndex = index;
    }
}

/**
 * 删除人才认定申报
 */
TalentInfo.delete = function () {
    if (this.check()) {
        if (TalentInfo.seItem.checkState > 1) {
            Feng.error("该申报已提交审核,无法删除");
            return;
        }
        var operation = function () {
            var ajax = new $ax("/enterprise/base/delete", function (data) {
                Feng.success("删除成功!");
                TalentInfo.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("talentInfoId", TalentInfo.seItem.id);
            ajax.start();
        }
        Feng.confirm("删除后无法恢复，确认删除吗?", operation);
    }
};

/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
TalentInfo.formParams = function () {
    var queryData = {};
    queryData['name'] = $("#name").val();
    queryData['card_number'] = $("#idCard").val();
    queryData['sex'] = $("#sex").val();
    queryData['nation'] = $("#nation").val();
    queryData['nationality'] = $("#nationality").val();
    queryData['talent_arrange'] = $("#talent_arrange").val();
    queryData['checkState'] = $("#checkState").val();
    return queryData;
}

/**
 * 查询人才认定申报列表
 */
TalentInfo.search = function () {
    TalentInfo.table.refresh({query: TalentInfo.formParams()});
};

/**
 * 重置
 */
TalentInfo.reset = function () {
    $("#name").val("");
    $("#idCard").val("");
    $("#sex").val("");
    $("#nation").val("");
    $("#nationality").val("");
    $("#talentType").val("");
    $("#talentArrange").val("");
    $("#checkState").val("");
}

$(function () {
    var defaultColunms = TalentInfo.initColumn();
    var table = new BSTable(TalentInfo.id, "/enterprise/base/list/type/1", defaultColunms);
    table.setPaginationType("server");
    table.setOnDblClickRow(function () {
        TalentInfo.openTalentInfoDetail();
    });
    TalentInfo.table = table.init();
    //批量加载字典表数据
    var arr = [
        {"name": "nation", "code": "nation"},
        {"name": "nationality", "code": "nationality"}
    ];
    Feng.findChildDictBatch(JSON.stringify(arr));
});

