/**
 * 显示审核日志
 */
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})
        }
    });
}


/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
TalentInfo.formParams = function () {
    var queryData = {};
    queryData['name'] = $("#name").val();
    queryData['card_number'] = $("#card_number").val();
    queryData['sex'] = $("#sex").val();
    queryData['nation'] = $("#nation").val();
    queryData['nationality'] = $("#nationality").val();
    queryData['province'] = $("#province").val();
    queryData['politics'] = $("#politics").val();
    queryData['enterprise_id'] = $("#enterprise_id").val();
    if ($("#company_id").length > 0) {
        queryData['company_id'] = $("#company_id").val();
    }
    queryData['industry_field'] = $("#industry_field").val();
    queryData['industry_field_old'] = $("#industry_field_old").val();
    queryData['enterprise_tag'] = $("#enterprise_tag").val();
    queryData['source'] = $("#source").val();
    queryData['city'] = $("#city").val();
    queryData['import_way'] = $("#import_way").val();
    queryData['highest_degree'] = $("#highest_degree").val();
    queryData['major'] = $("#major").val();
    queryData['study_abroad'] = $("#study_abroad").val();
    queryData['phone'] = $("#phone").val();
    queryData['email'] = $("#email").val();
    queryData['breakFaith'] = $("#breakFaith").val();
    queryData['talent_type'] = $("#talent_type").val();
    queryData['isMatchZhiren'] = $("#isMatchZhiren").val();
    queryData['talent_arrange'] = $("#talent_arrange").val();
    queryData['street'] = $("#street").val();
    if ($("#talent_condition").val() == null || $("#talent_condition").val() == '' || $("#talent_condition").val() == "null") {
        queryData['talent_condition'] = "";
    } else {
        queryData['talent_condition'] = $("#talent_condition").val();
    }
    queryData['identifyMonth'] = $("#identifyMonth").val();
    queryData['checkState'] = $("#checkState").val();
    queryData['isPublic'] = $("#isPublic").val();
    queryData['active'] = $("#active").val()
    queryData['apply_year'] = $("#apply_year").val();
    queryData['isEffect'] = $("#isEffect").val();
    //queryData['offset'] = 0;
    return queryData;
}

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

/**
 * 重置
 */
TalentInfo.reset = function () {
    $("#name").val("");
    $("#card_number").val("");
    $("#sex").val("");
    $("#nation").val("");
    $("#nationality").val("");
    $("#province").val("");
    $("#politics").val("");
    $("#enterprise_id").val("").trigger("chosen:updated");
    if ($("#company_id").length > 0) {
        $("#company_id").val("").trigger("chosen:updated");
    }
    $("#industry_field").val("");
    $("#industry_field_old").val("");
    $("#enterprise_tag").val("");
    $("#source").val("");
    $("#fromCity").val("");
    $("#import_way").val("");
    $("#highest_degree").val("");
    $("#major").val("");
    $("#study_abroad").val("");
    $("#phone").val("");
    $("#email").val("");
    $("#breakFaith").val("");
    $("#talent_type").val("");
    $("#isMatchZhiren").val("");
    $("#talent_arrange").val("");
    $("#street").val("");
    $("#talent_condition").val("").trigger("chosen:updated");
    $("#checkState").val("");
    $("#identifyMonth").val("");
    $("#isPublic").val("");
    $("#active").val("")
    $("#apply_year").val("");
    $("#isEffect").val("");
}



/**
 * 获取人才认定
 */
TalentInfo.getIdentifyCondition = function () {
    var level = $("#talent_arrange").val();
    if (level == null || level == '') {
        $("#talent_condition").empty();
        $("#talent_condition").trigger('chosen:updated');
        return;
    }
    Feng.addAjaxSelect({
        "id": "talent_condition",
        "displayCode": "id",
        "displayName": "name",
        "type": "GET",
        "url": Feng.ctxPath + "/common/api/findIdentifyConditionByLevel/level/" + level
    });
    $("#talent_condition").trigger('chosen:updated');
}
/**
 * 获取人才认定
 */
TalentInfo.getProvince = function () {
    Feng.addAjaxSelect({
        "id": "province",
        "displayCode": "code",
        "displayName": "name",
        "type": "GET",
        "url": Feng.ctxPath + "/common/tool/getProvinceSelect"
    });
}

/**
 * 显示导出模态框
 */
TalentInfo.showExportModal = function () {
    $("#exportForm")[0].reset();
    $("#commonExportModal").modal("show");
}

/**
 * 修改驳回的字段及附件
 */
TalentInfo.updateFieldsAndFiles = function () {
    if (this.check()) {
        var ajax = new $ax("/enterprise/talent/findFieldsAndFiles?id=" + TalentInfo.seItem.id, function (data) {
            if (data.code == 200) {
                layer.open({
                    type: 1,
                    id: "neewFieldFormModel",
                    title: '修改',
                    area: ['800px', '450px'], //宽高
                    fix: false, //不固定
                    shade: 0,
                    maxmin: true,
                    content: TalentInfo.creatFieldCheckModal(),
                    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) {

                        var html1 = '';
                        if (typeof data.fieldList != "undefined" && data.fieldList.length > 0) {
                            for (var key in data.fieldList) {
                                html1 += '<li style="float:left;margin:0 10px 10px 0;"><input type="checkbox" value="' + data.fieldList[key]["key"] + '"><span>' + data.fieldList[key]["value"] + '</span></li>';
                            }
                        }

                        var html2 = '';
                        for (var key in data.fileList) {
                            html2 = html2 + '<ul><li style="width: 100%"><input type="checkbox" value="' + data.fileList[key].id + '"><span>' + data.fileList[key].name + '</span></li></ul>';
                        }
                        $("#firstCheckForm #field_info ul").css("overflow", "hidden").html(html1);
                        $("#field_file").css("overflow", "hidden").empty().append(html2);
                        //$("#field_file").empty().append(html);
                        if (data.select.fields != null && data.select.fields != '') {
                            $("#firstCheckForm #field_info li input").each(function () {
                                if (data.select.fields.indexOf($(this).val()) != -1) {
                                    this.checked = true;
                                }
                            });
                        }
                        if (data.select.files != null && data.select.files != '') {
                            $("#field_file input").each(function () {
                                if (data.select.files.indexOf($(this).val()) != -1) {
                                    this.checked = true;
                                }
                            });
                        }
                    },
                    yes: function (index, layero) {
                        TalentInfo.submitFieldsAndFiles(index, data.id);
                    }
                });
            } else {
                Feng.error(data.msg);
            }
        }, function (data) {
            Feng.error("查询失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    }
}

/**
 * 修改提交
 * @param index
 * @param id
 */
TalentInfo.submitFieldsAndFiles = function (index, id) {
    var fields = '';
    var files = '';
    $("#firstCheckForm #field_info li input").each(function (index) {
        if ($(this).is(":checked")) {
            fields = fields + $(this).val() + ",";
        }
    });
    $("#field_file li input").each(function (index) {
        if ($(this).is(":checked")) {
            files = files + $(this).val() + ",";
        }
    });
    if (fields == '' && files == '') {
        Feng.info("请选择可修改的字段或附件！");
        return;
    }
    var ajax = new $ax("/enterprise/talent/updateFieldsAndFiles", function (data) {
        if (data.code == 200) {
            layer.close(index);
            Feng.success(data.msg);
        } else {
            Feng.error(data.msg);
        }
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.setData({"id": id, "fields": fields, "files": files})
    ajax.start();
}

/**
 * 导出提交
 */
TalentInfo.export = function (process, exportAll) {
    exportAll = typeof exportAll == "undefined" || exportAll == false ? 0 : 1;
    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 = TalentInfo.formParams();
    var process = parseInt($("#process").val());
    switch (process) {
        case 1:
            commonExport = "baseVerifyListExport";
            break;
        case 2:
            commonExport = "baseReverifyListExport";
            break;
        case 3:
            commonExport = "fstVerifyListExport";
            break;
        case 4:
            commonExport = "deptVerifyListExport";
            break;
        case 5:
            commonExport = "reVerifyListExport";
            break;
        case 6:
            commonExport = "preListExport";
            break;
        case 7:
            commonExport = "libraryListExport";
            break;
    }
    $("#commonExportModal").modal('hide');
    var params = $("#exportForm").serialize();
    var url = "/admin/talent/" + commonExport + "?" + params;
    Object.keys(queryData).forEach(function (key, index) {
        if (typeof queryData[key] != "undefined" && queryData[key]) {
            url += "&" + key + "=" + queryData[key];
        }
    })
    url += "&all=" + exportAll;
    window.location.href = url;
}

/**
 * 页面初始化
 */
TalentInfo.init = function () {
    //批量加载字典表数据
    var arr = [
        {"name": "nation", "code": "nation"},
        {"name": "nationality", "code": "nationality"},
        {"name": "politics", "code": "politics"},
        {"name": "industry_field", "code": "industry_field"},
        {"name": "import_way", "code": "import_way"},
        {"name": "highest_degree", "code": "highest_degree"},
        {"name": "talent_type", "code": "talent_type"},
        {"name": "talent_arrange", "code": "talent_arrange"},
        {"name": "source", "code": "source"},
        {"name": "street", "code": "street"},
        {"name": "enterprise_tag", "code": "enterprise_tag"}, ];
    Feng.findChildDictBatch(JSON.stringify(arr));
    //TalentInfo.getIdentifyCondition();
    TalentInfo.getProvince();
    $("#talent_condition,#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');
    $("#talent_condition,#enterprise_id").chosen({
        search_contains: true,       //关键字模糊搜索。设置为true，只要选项包含搜索词就会显示；设置为false，则要求从选项开头开始匹配
        disable_search: false,
        width: "100%",
        enable_split_word_search: true
    });
    if ($("#company_id").length > 0) {
        $("#company_id").on('chosen:ready', function (e, params) {
            $(".chosen-container-single .chosen-single").css("padding", "4px 0px 0px 4px");
        });
        $("#company_id").val("");
        $("#company_id").trigger('chosen:updated');
        $("#company_id").chosen({
            search_contains: true,       //关键字模糊搜索。设置为true，只要选项包含搜索词就会显示；设置为false，则要求从选项开头开始匹配
            disable_search: false,
            width: "100%",
            enable_split_word_search: true
        });
    }
}

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

/**
 * 批量下载头像
 */
TalentInfo.downloadPhoto = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }
    var ids = "";
    for (let i = 0; i < selected.length; i++) {
        ids = ids + selected[i].id + ",";
    }
    window.location.href = encodeURI(encodeURI(Feng.ctxPath + "/api/commonDownload/downloadPhotos?type=1&ids=" + ids));
}

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

