/**
 * 人才认定申报管理初始化
 */
var IntegralInfo = {
    id: "IntegralInfoTable", //表格id
    checkAll: false,
    seItem: null, //选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
IntegralInfo.initColumn = function () {
    var type = $("#usertype").val();
    var isShow = true;
    if (type == 2) {
        isShow = false;
    }
    ;
    return [
        {field: 'selectItem', radio: true},
        {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: 'enterpriseName', visible: true, align: 'center', valign: 'middle', width: "100px"},
        {title: '证件类型', field: 'card_type', visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip', width: "100px",
            formatter: function (value, row, index) {
                switch (value) {
                    case 1:
                        return "身份证";
                    case 2:
                        return "港澳通行证";
                    case 3:
                        return "护照";
                }
            }
        },
        {title: '证件号码', field: 'card_number', visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip', width: "120px"},
        {title: '人才层次', field: 'talentLevel', visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip', width: "120px",
            formatter: function (value, row, index) {
                var talentLevel = parseInt(value);
                switch (talentLevel) {
                    case 1:
                        return "第一层次";
                    case 2:
                        return "第二层次";
                    case 3:
                        return "第三层次";
                    case 4:
                        return "第四层次";
                    case 5:
                        return "第五层次";
                    case 6:
                        return "第六层次";
                    case 7:
                        return "第七层次";
                    default:
                        return "非优秀人才";
                }
            }},
        {title: '基础分', field: 'basePoints', visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip', width: "120px"},
        {title: '总积分', field: 'totalPoints', visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip', width: "120px"},
        {title: '操作', field: 'id', visible: true, align: 'center', valign: 'middle', width: '80px',
            formatter: function (value, row, index) {
                return "<span class='label label-success' onclick=\"IntegralInfo.showIntegralLog('" + row.card_type + "','" + row.card_number + "')\" >" +
                        "<i class=\"fa fa-history\"></i> 积分记录" +
                        "</span>";
            }
        }
    ];
};
IntegralInfo.openIntegralLog = function () {
    if (this.check()) {
        IntegralInfo.showIntegralLog(IntegralInfo.seItem.card_type, IntegralInfo.seItem.card_number);
    }
}

/**
 * 显示积分记录
 */
IntegralInfo.showIntegralLog = function (card_type, card_number) {
    layer.open({
        type: 2,
        title: "积分记录",
        fixed: false,
        content: '/admin/integralVerify/integralLog/cardType/' + card_type + '/cardNumber/' + card_number,
        area: ['80%', '80%'],
        fix: false, //不固定
        maxmin: true
    });
}


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


/**
 * 打开查看积分申报-初级审核详情
 */
IntegralInfo.openIntegralInfoDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '人才认定审核详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: '/admin/integralVerify/detail/id/' + IntegralInfo.seItem.id
        });
        layer.full(index);
        IntegralInfo.layerIndex = index;
    }
};

IntegralInfo.openCheckModal = function (type) {
    if ((type == 2 && this.check()) || type == 1) {
        var selected = $('#' + this.id).bootstrapTable('getSelections');
        selected = selected.length > 0 ? selected[0] : [];
        var subtitle = type == 2 ? "个人" : "企业";
        var enterprise_id = selected.enterprise_id;
        var card_type = selected.card_type;
        var card_number = selected.card_number;
        var ajax = new $ax("/admin/integralVerify/veto", function (data) {
            if (data.code == 200) {
                layer.open({
                    type: 1,
                    id: "newVetoModalForm",
                    title: '一票否决(' + subtitle + ")",
                    area: ['800px', '350px'], //宽高
                    fix: false, //不固定
                    shade: 0,
                    maxmin: true,
                    content: IntegralInfo.createVetoFormModal(),
                    btn: ['<i class="fa fa-save"></i>&nbsp;&nbsp;提交', '<i class="fa fa-eraser"></i>&nbsp;&nbsp;关闭'],
                    btnAlign: 'c',
                    zIndex: layer.zIndex,
                    success: function (layero, index) {
                        $("#vetoForm")[0].reset();
                        $("#vetoType").html('<option value="' + type + '">' + subtitle + '</option>');
                        var veto = data.veto;
                        var list = "";
                        if (type == 1) {
                            for (var i in data.enterprises) {
                                list += '<option value="' + data.enterprises[i].id + '" ' + (typeof veto.enterprise_id != "undefined" && veto.enterprise_id == data.enterprises[i].id ? "selected" : "") + '>' + data.enterprises[i].name + '</option>';
                            }
                        } else {
                            list += '<option>' + veto.name + '</option>';
                        }
                        $("#vetoObj").html(list);
                        $("#vetoCheckState").val(veto.active == 1 || typeof veto.active == "undefined" ? 1 : 2);
                        $("#vetoMsg").html(veto.description);
                    },
                    yes: function (index, layero) {
                        IntegralInfo.submitVeto(index, type);
                    }
                });
            } else {
                Feng.error(data.msg);
            }
        }, function (data) {
            Feng.error("校验失败!" + data.responseJSON.message + "!");
        });
        ajax.setData({type: type, card_type: card_type, card_number: card_number, enterprise_id: enterprise_id})
        ajax.start();
    }
}

var locked = false;
/**
 * 提交一票否决
 */
IntegralInfo.submitVeto = function (i, type) {
    var checkState = $("#vetoCheckState").val();
    var checkMsg = $("#vetoMsg").val();
    var vetoObj = $("#vetoObj").val();
    if (checkState == null || checkState == '') {
        Feng.info("请选择否决状态");
        return;
    }
    if (checkMsg == null || checkMsg == '') {
        Feng.info("请填写意见");
        return;
    }
    var card_type = "";
    var card_number = "";
    if (type == 2 && this.check()) {
        card_type = IntegralInfo.seItem.card_type;
        card_number = IntegralInfo.seItem.card_number;
    }
    if (type == 1 && !vetoObj) {
        Feng.info("请选择企业");
        return;
    }
    if (locked)
        return;
    locked = true;
    var ajax = new $ax(Feng.ctxPath + "/admin/integralVerify/submitVeto", function (data) {
        if (data.code == 200) {
            Feng.success(data.msg);
            window.parent.IntegralInfo.table.refresh();
            layer.close(i);
        } else {
            Feng.error(data.msg);
        }
        locked = false;
    }, function (data) {
        Feng.error("提交失败!" + data.responseJSON.message + "!");
        locked = false;
    });
    ajax.setData({"checkState": checkState, "checkMsg": checkMsg, type: type, card_type: card_type, card_number: card_number, enterprise_id: vetoObj});
    ajax.start();
}

IntegralInfo.createVetoFormModal = function () {
    return '<form id="vetoForm">\n' +
            '  <div class="form-group" style="margin: 10px;">\n' +
            '      <div class="row" style="margin-bottom: 10px;">\n' +
            '         <label class="col-sm-2 control-label">类型</label>\n' +
            '         <div class="col-sm-10">\n' +
            '           <select id="vetoType" name="vetoType" class="form-control">\n' +
            '           </select>\n' +
            '         </div>\n' +
            '       </div>\n' +
            '      <div class="row" style="margin-bottom: 10px;">\n' +
            '         <label class="col-sm-2 control-label">名称</label>\n' +
            '         <div class="col-sm-10">\n' +
            '           <select id="vetoObj" name="vetoObj" class="form-control">\n' +
            '           </select>\n' +
            '         </div>\n' +
            '       </div>\n' +
            '      <div class="row" style="margin-bottom: 10px;">\n' +
            '         <label class="col-sm-2 control-label">状态</label>\n' +
            '         <div class="col-sm-10">\n' +
            '           <select id="vetoCheckState" name="vetoCheckState" class="form-control">\n' +
            '               <option value="1">一票否决</option>\n' +
            '               <option value="2">恢复</option>\n' +
            '           </select>\n' +
            '         </div>\n' +
            '       </div>\n' +
            '       <div class="row">\n' +
            '          <label class="col-sm-2 control-label">意见</label>\n' +
            '          <div class="col-sm-10">\n' +
            '           <textarea id="vetoMsg" name="vetoMsg" class="form-control"></textarea>\n' +
            '           </div>\n' +
            '      </div>\n' +
            '   </div>\n' +
            '</form>';
}

/**
 * 选择导出提交
 */
IntegralInfo.checkExport = function () {
    var selected = $('#dataTable').bootstrapTable('getSelections');
    if (!selected || selected.length < 1) {
        Feng.info("请至少选择一行数据!");
        return;
    }
    var ids = "";
    for (var i = 0; i < selected.length; i++) {
        ids = ids + selected[i].id + ",";
    }
    window.location.href = Feng.ctxPath + "/admin/integralVerify/prepareHczx?ids=" + ids;
}

//回调
IntegralInfo.callBack = function (data) {
    Feng.info(data.msg);
    if (data.code == 200) {
        $("#hczxModal").modal("hide");
        IntegralInfo.table.refresh();
    }
}


$(function () {
    var defaultColunms = IntegralInfo.initColumn();
    var table = new BSTable(IntegralInfo.id, "/admin/integralVerify/list/process/4", defaultColunms);
    table.setPaginationType("server");
    table.setSingleSelect(false);
    table.setOnDblClickRow(function () {
        //IntegralInfo.openIntegralInfoDetail();
    });
    var t = IntegralInfo.table = table.init();
    IntegralInfo.init();
    $('#checkAll').click(function () {
        $("#dataTable").bootstrapTable('togglePagination').bootstrapTable('checkAll').bootstrapTable('togglePagination');
    })
    $('#uncheckAll').click(function () {
        $("#dataTable").bootstrapTable('togglePagination').bootstrapTable('uncheckAll').bootstrapTable('togglePagination')
    })
});
