/**
 * 积分记录
 */
var IntegralVerify = {
    id: "IntegralLogTable", //表格id
    seItem: null, //选中的条目
    table: null,
    layerIndex: -1
};
IntegralVerify.formParams = function () {
    var queryData = {};
    queryData['card_type'] = $("#card_type").val();
    queryData['card_number'] = $("#card_number").val();
    queryData['apply_year'] = $("#apply_year").val();
    queryData['enterprise_id'] = $("#enterprise_id").val();
    queryData['shareholder'] = $("#shareholder").val();
    return queryData;
}

/**
 * 初始化表格的列
 */
IntegralVerify.initColumn = function () {
    var type = $("#type").val();
    return [
        {field: 'selectItem', radio: true},
        {title: '积分来源', field: 'mainType', visible: true, align: 'center', valign: 'middle', width: '80px',
            formatter: function (value, row, index) {
                switch (value) {
                    case "integral":
                        return "积分申报";
                    case "talent":
                        return "人才申报";
                    case "talentTypeChange":
                        return "人才层次变更";
                    default:
                        return "未分类";
                }
            }
        },
        {title: '申报年度', field: 'apply_year', visible: true, align: 'center', valign: 'middle', width: '80px'},
        {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: true, align: 'center', valign: 'middle', width: "100px"},
        {title: '是否股东', field: 'shareholder', visible: true, align: 'center', valign: 'middle', width: "100px",
            formatter: function (value, row, index) {
                if (value == 1) {
                    return '是';
                }
                if (value == 2) {
                    return '否';
                }
            }
        },
        {title: '申报标准', field: 'details', visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip', width: "120px"},
        {title: '上次积分', field: 'lastPoints', visible: true, align: 'center', valign: 'middle', width: "100px",
            formatter: function (value, row, index) {
                return value;
            }
        },
        {title: '增加积分', field: 'gainPoints', visible: true, align: 'center', valign: 'middle', width: "100px",
            formatter: function (value, row, index) {
                return value;
            }
        },
        {title: '当前积分', field: 'nowPoints', visible: true, align: 'center', valign: 'middle', width: "100px",
            formatter: function (value, row, index) {
                return value;
            }
        },
        {title: '首次提交时间', field: 'first_submit_time', visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip', width: "100px"},
        {title: '积分获得时间', field: 'createTime', visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip', width: "100px"},
        {title: '操作', field: 'mainId', visible: true, align: 'center', valign: 'middle', width: "80px",
            formatter: function (value, row, index) {
                var type = row.mainType == "integral" ? CONFIG.project_integral_apply : CONFIG.project_rcrd;
                return "<span class='label label-success' onclick=\"IntegralVerify.showLog('" + value + "'," + "'" + type + "')\" >" +
                        "<i class=\"fa fa-book\"></i>日志" +
                        "</span>";
            }
        }
    ];
};
/**
 * 检查是否选中
 */
IntegralVerify.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length != 1) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        IntegralVerify.seItem = selected[0];
        return true;
    }
};


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

/**
 * 重置
 */
IntegralVerify.reset = function () {
    $("#name").val("");
    $("#card_number").val("");
    $("#phone").val("");
    $("#email").val("");
    $("#checkState").val("");
    $("#apply_year").val("");
    $("#enterprise_id").val("").trigger("chosen:updated");
    $("#shareholder").val("");
}
/**
 * 显示导出模态框
 */
IntegralVerify.showExportModal = function () {
    $("#exportForm")[0].reset();
    $("#commonExportModal").modal("show");
}

/**
 * 导出提交
 */
IntegralVerify.export = function (process) {
    var names = '';
    var values = '';
    var commonExport = "";
    $("#field_info li input").each(function (index) {
        if ($(this).is(":checked")) {
            values = values + $(this).val() + ",";
            names = names + $(this).next().text() + ",";
        }
    });
    var queryData = IntegralVerify.formParams();
    commonExport = "integralLogListExport";
    $("#commonExportModal").modal('hide');
    var params = $("#exportForm").serialize();
    var url = "/admin/integralVerify/" + commonExport + "?" + params;
    window.location.href = url;
}
/**
 * 下载
 */
IntegralVerify.download = function () {
    if (this.check()) {
        if (IntegralVerify.seItem.mainType == "talent") {
            layer.alert("不能下载非积分申报的附件");
            return;
        }
        window.location.href = encodeURI(encodeURI(Feng.ctxPath + "/common/api/downloadZip?type=20&id=" + IntegralVerify.seItem.mainId));
    }
}


IntegralVerify.openCheckIntegralVerify = function () {
    if (this.check()) {
        if (IntegralVerify.seItem.mainType == "talent") {
            layer.alert("不能查看非积分申报的内容");
            return;
        }
        var index = layer.open({
            type: 2,
            title: '积分申报记录',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: '/admin/integralVerify/detail/id/' + IntegralVerify.seItem.mainId + '/1',
            btn: ['<i class="fa fa-eraser"></i>&nbsp;&nbsp;关闭'],
            btnAlign: 'c'
        });
        layer.full(index);
        IntegralVerify.layerIndex = index;
    }
}

/**
 * 显示审核日志
 */
IntegralVerify.showLog = function (id, type) {
    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": type, "mainId": id, "typeFileId": "", "active": 1})
        }
    });
}

IntegralVerify.creatFieldCheckModal = function () {
    return '<form id="firstCheckForm">\n' +
            '                    <div class="form-group" style="margin: 10px;">\n' +
            '                        <div >\n' +
            '                            <label for="checkMsg" class="control-label">可修改字段</label>\n' +
            '                            <div id="field_info">\n' +
            '                                <ul>\n' +
            '                                </ul>\n' +
            '                            </div>\n' +
            '                            <label for="checkMsg" class="control-label">可修改附件</label>\n' +
            '                            <div id="field_file">\n' +
            '                            </div>\n' +
            '                            <div class="form-group" style="text-align: center">\n' +
            '                                <button type="button" class="btn btn-primary" onclick="IntegralVerify.checkAll()">全选</button>\n' +
            '                                <button type="button" class="btn btn-success" onclick="IntegralVerify.unCheckAll()">反选</button>\n' +
            '                            </div>\n' +
            '                        </div>\n' +
            '                    </div>\n' +
            '                </form>';
}

/**
 * 全选
 */
IntegralVerify.checkAll = function () {
    $("#field_info input").each(function () {
        this.checked = true;
    })
}
/**
 * 反选
 */
IntegralVerify.unCheckAll = function () {
    $("#field_info input").each(function () {
        if (this.checked) {
            this.checked = false;
        } else {
            this.checked = true;
        }
    })
}

$(function () {
    var defaultColunms = IntegralVerify.initColumn();
    var process = $("#process").val();
    var card_type = $("#card_type").val();
    var card_number = $("#card_number").val();
    var table = new BSTable(IntegralVerify.id, "/admin/integralVerify/list/process/" + process + "/card_type/" + card_type + "/card_number/" + card_number, defaultColunms);
    table.setPaginationType("server");
    table.setSingleSelect(false);
    table.setOnDblClickRow(function () {
        IntegralVerify.openCheckIntegralVerify();
    });
    IntegralVerify.table = table.init();
    $("#enterprise_id").on('chosen:ready', function (e, params) {
        $(".chosen-container-single .chosen-single").css("padding", "4px 0px 0px 4px");
    });
    $("#enterprise_id").val("");
    $("#enterprise_id").trigger('chosen:updated');
    $("#enterprise_id").chosen({
        search_contains: true,       //关键字模糊搜索。设置为true，只要选项包含搜索词就会显示；设置为false，则要求从选项开头开始匹配
        disable_search: false,
        width: "100%",
        enable_split_word_search: true
    });
});
