/**
 * 优秀人才津补贴管理初始化
 */
var TalentAllowanceInfo = {
    id: "TalentAllowanceInfoTable", //表格id
    seItem: null, //选中的条目
    table: null,
    layerIndex: -1
};
/**
 * 检查是否选中
 */
TalentAllowanceInfo.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length != 1) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        TalentAllowanceInfo.seItem = selected[0];
        return true;
    }
};
/**
 * 点击添加优秀人才津补贴
 */
TalentAllowanceInfo.openCheckTalentAllowanceInfo = function () {
    if (this.check()) {
        var index = null;
        var process = $("#process").val();
        index = layer.open({
            type: 2,
            title: '津补贴审核',
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/enterprise/talentAllowance/toCheckPage/id/' + TalentAllowanceInfo.seItem.id,
            btn: ['<i class="fa fa-eye"></i>&nbsp;&nbsp;保存未提交', '<i class="fa fa-save 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.TalentAllowanceInfoDlg.showCheckModal();
            },
            btn2: function (index, layero) {
                var obj = layero.find("iframe")[0].contentWindow;
                obj.TalentAllowanceInfoDlg.submitCheck();
                return false;
            },
            end: function () {
                layer.closeAll('tips');
                TalentAllowanceInfo.table.refresh();
            }
        });
        layer.full(index);
        TalentAllowanceInfo.layerIndex = index;
    }
};

/**
 * 查看
 */
TalentAllowanceInfo.select = function () {
    if (this.check()) {
        var process = $("#process").val();
        var url = Feng.ctxPath + '/enterprise/talentAllowance/toSelectPage/id/' + TalentAllowanceInfo.seItem.id;
        var index = layer.open({
            type: 2,
            title: '津补贴查看',
            fix: false, //不固定
            maxmin: true,
            shadeClose: false,
            content: url,
            btn: ['<i class="fa fa-eraser"></i>&nbsp;&nbsp;关闭'],
            btnAlign: 'c',
        });
        layer.full(index);
        TalentAllowanceInfo.layerIndex = index;
    }
}

TalentAllowanceInfo.updateFieldsAndFiles = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/enterprise/talentAllowance/findFieldsAndFiles?id=" + TalentAllowanceInfo.seItem.id, function (data) {
            if (data.code == 200) {
                var obj = data.obj.obj;
                layer.open({
                    type: 1,
                    id: "neewFieldFormModel",
                    title: '修改',
                    area: ['800px', '450px'], //宽高
                    fix: false, //不固定
                    shade: 0,
                    maxmin: true,
                    content: '<form id="checkForm">\n' +
                            '                    <div class="form-group" id="field" style="margin: 10px">\n' +
                            '                        <label for="field_concat" class="control-label">可修改合同时间</label>\n' +
                            '                        <div id="field_concat">\n' +
                            '                        </div>\n' +
                            '                        <label for="field_project" class="control-label">可修改字段</label>\n' +
                            '                        <div id="field_field">\n' +
                            '                           <ul><li style="width: 100%"><input type="checkbox" id="typeChange" class="icheckbox" value="allowanceType"><span>津补贴类型</span></li></ul>\n' +
                            '                           <!--<ul><li style="width: 100%"><input type="checkbox" value="wage"><span>上一年度年薪(元)</span></li></ul>-->' +
                            '                        </div>\n' +
                            '                        <label for="field_project" class="control-label">可修改项目</label>\n' +
                            '                        <div id="field_project">\n' +
                            '                        </div>\n' +
                            '                        <label for="field_file" 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="TalentAllowanceInfoSupple.checkAll(\'field\')">全选</button>\n' +
                            '                            <button type="button" class="btn btn-success" onclick="TalentAllowanceInfoSupple.unCheckAll(\'field\')">反选</button>\n' +
                            '                        </div>\n' +
                            '                    </div>\n' +
                            '                </form>',
                    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 projectList = data.obj.projects;
                        var fileList = data.obj.files;
                        var concatList = data.obj.concats;
                        var files = data.obj.info.files;
                        var projects = data.obj.info.projects;
                        var concats = data.obj.info.concats;
                        var fields = data.obj.info.fields;
                        //初始化附件、核查项目、合同
                        if (projectList != null && projectList.length != 0) {
                            var html = '<ul>';
                            for (var key in projectList) {
                                html = html + '<li style="width: 100%"><input type="checkbox" class="icheckbox" value="' + projectList[key].id + '"><span>' + projectList[key].projectName + '</span></li>';
                            }
                            html = html + "</ul>";
                            $("#field_project").empty().append(html);
                        }
                        if (fileList != null && fileList.length != 0) {
                            var html = '';
                            for (var key in fileList) {
                                html = html + '<ul><li style="width: 100%"><input type="checkbox" class="icheckbox" data-type="' + fileList[key].isConditionFile + '" value="' + fileList[key].id + '"><span>' + fileList[key].name + '</span></li></ul>';
                            }
                            $("#field_file").empty().append(html);
                        }
                        if (concatList != null && concatList.length != 0) {
                            var html = '';
                            for (var key in concatList) {
                                html = html + '<ul><li style="width: 100%"><input type="checkbox" class="icheckbox" value="' + concatList[key].id + '"><span>' + concatList[key].enterpriseName + '</span></li></ul>';
                            }
                            $("#field_concat").empty().append(html);
                        }
                        TalentAllowanceInfoSupple.initICheck();
                        if (Feng.isNotEmptyStr(projects)) {
                            $("#field_project input").each(function () {
                                if (projects.indexOf($(this).val()) != -1) {
                                    $(this).iCheck("check");
                                }
                            });
                        }
                        if (files != null && files != '') {
                            $("#field_file input").each(function () {
                                if (files.indexOf($(this).val()) != -1) {
                                    $(this).iCheck("check");
                                }
                            });
                        }
                        if (concats != null && concats != '') {
                            $("#field_concat input").each(function () {
                                if (concats.indexOf($(this).val()) != -1) {
                                    $(this).iCheck("check");
                                }
                            });
                        }
                        if (Feng.isNotEmptyStr(fields)) {
                            $("#field_field input").each(function () {
                                if (fields.indexOf($(this).val()) != -1) {
                                    $(this).iCheck("check");
                                }
                            });
                        }
                        $("input[type=checkbox][value=allowanceType]").on("ifChanged", function (e) {
                            var isChecked = $(this).is(":checked") ? "check" : "uncheck";
                            $("#field_file input").each(function () {
                                $(this).iCheck(isChecked);
                            })
                            $("#field_project input").each(function () {
                                $(this).iCheck(isChecked);
                            })
                        })
                    },
                    yes: function (index, layero) {
                        var projects = '', files = '', concats = '', fields = '';
                        $("#field_project li input").each(function (index) {
                            if ($(this).is(":checked")) {
                                projects = projects + $(this).val() + ",";
                            }
                        });
                        $("#field_file li input").each(function (index) {
                            if ($(this).is(":checked")) {
                                files = files + $(this).val() + ",";
                            }
                        });
                        $("#field_concat li input").each(function (index) {
                            if ($(this).is(":checked")) {
                                concats = concats + $(this).val() + ",";
                            }
                        });
                        $("#field_field li input").each(function (index) {
                            if ($(this).is(":checked")) {
                                fields = fields + $(this).val() + ",";
                            }
                        });
                        fields = fields.substring(0, fields.length - 1);
                        if (Feng.isEmptyStr(projects) && Feng.isEmptyStr(files) && Feng.isEmptyStr(concats) && Feng.isEmptyStr(fields)) {
                            Feng.info("请选择可修改的字段、附件或合同！");
                            return;
                        }
                        var ajax = new $ax(Feng.ctxPath + "/enterprise/talentAllowance/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": TalentAllowanceInfo.seItem.id,
                            "projects": projects,
                            "files": files,
                            "concats": concats,
                            "fields": fields
                        })
                        ajax.start();
                    }
                });
            } else {
                Feng.error(data.msg);
            }
        }, function (data) {
            Feng.error("查询失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    }
}

/**
 * 导出基础信息
 */
TalentAllowanceInfo.exportBasicInfo = function () {
    var process = $("#process").val();
    var queryData = TalentAllowanceInfo.formParams();
    queryData['process'] = process;
    var url = Feng.setUrlParam(Feng.ctxPath + "/enterprise/talentAllowance/exportBasicInfo", queryData);
    window.hiddenIframe.location.href = url;
}

/**
 * 回调
 */
TalentAllowanceInfo.callBack = function (data) {
    if (data.code == 200) {
        $("#importModal").modal("hide");
        Feng.success(data.msg);
    } else {
        Feng.error(data.msg);
    }
}
/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
TalentAllowanceInfo.formParams = function () {
    var queryData = {};
    queryData['year'] = $("#year").val();
    queryData['enterpriseName'] = $("#enterpriseName").val();
    queryData['name'] = $("#name").val();
    queryData['idCard'] = $("#idCard").val();
    queryData['talentType'] = $("#talentType").val();
    queryData['talentArrange'] = $("#talentArrange").val();
    queryData['address'] = $("#address").val();
    queryData['identifyCondition'] = $("#identifyCondition").val();
    queryData['isSupple'] = $("#isSupple").val();
    queryData['checkState'] = $("#checkState").val();
    queryData['isPublicCheck'] = $("#isPublicCheck").val();
    queryData['publicState'] = $("#publicState").val();
    queryData['allowanceType'] = $("#allowanceType").val();
    queryData['recommendAllowanceType'] = $("#recommendAllowanceType").val();
    queryData['companyName'] = $("#companyName").val();
    queryData['introductionMode'] = $("#introductionMode").val();
    queryData['firstJJStartTime'] = $("#firstJJStartTime").val();
    queryData['firstJJEndTime'] = $("#firstJJEndTime").val();
    return queryData;
}
/**
 * 查询人才认定申报列表
 */
TalentAllowanceInfo.search = function () {
    TalentAllowanceInfo.table.refresh({
        query: TalentAllowanceInfo.formParams()
    });
};
/**
 * 重置
 */
TalentAllowanceInfo.reset = function () {
    $("#year").val("");
    $("#enterpriseName").val("");
    $("#name").val("");
    $("#idCard").val("");
    $("#talentType").val("");
    $("#talentArrange").val("");
    $("#address").val("");
    $("#identifyCondition").val("");
    $("#isSupple").val("");
    $("#checkState").val("");
    $("#isPublicCheck").val("");
    $("#publicState").val("");
    $("#recommendAllowanceType").val("");
    $("#companyName").val("");
    $("#introductionMode").val("");
    $("#firstJJStartTime").val("");
    $("#firstJJEndTime").val("");
}
/**
 * 获取人才认定
 */
TalentAllowanceInfo.getIdentifyCondition = function () {
    var level = $("#talentArrange").val();
    if (level == null || level == '') {
        $("#identifyCondition").empty();
        $("#identifyCondition").trigger('chosen:updated');
        return;
    }
    Feng.addAjaxSelect({
        "id": "identifyCondition",
        "displayCode": "id",
        "displayName": "name",
        "type": "GET",
        "url": Feng.ctxPath + "/common/api/findIdentifyConditionByLevel?level=" + level
    });
    $("#identifyCondition").trigger('chosen:updated');
}


$(function () {
    var process = $("#process").val();
    var defaultColunms = TalentAllowanceInfoSupple.initColumn(process);
    var table = new BSTable(TalentAllowanceInfo.id, "/enterprise/talentAllowance/examineList", defaultColunms);
    table.setPaginationType("server");
    table.setOnDblClickRow(function () {
        TalentAllowanceInfo.openCheckTalentAllowanceInfo();
    });
    table.setSingleSelect(false);
    TalentAllowanceInfo.table = table.init();
    //批量加载字典表数据
    var arr = [{
            "name": "address",
            "code": "street"
        }, {
            "name": "talentArrange",
            "code": "talent_arrange"
        }, {
            "name": "nationality",
            "code": "nationality"
        }, {
            "name": "talentType",
            "code": "talent_type"
        }, {"name": "introductionMode", "code": "un_introduction_mode"}];
    Feng.findChildDictBatch(JSON.stringify(arr));
    $("#identifyCondition").on('chosen:ready', function (e, params) {
        $(".chosen-container-single .chosen-single").css("padding", "4px 0px 0px 4px");
    });
    $("#identifyCondition").chosen({
        search_contains: true,
        //关键字模糊搜索。设置为true，只要选项包含搜索词就会显示；设置为false，则要求从选项开头开始匹配
        disable_search: false,
        width: "100%",
        enable_split_word_search: true
    });
    $('#checkAll').click(function () {
        $("#dataTable").bootstrapTable('togglePagination').bootstrapTable('checkAll').bootstrapTable('togglePagination');
    })
    $('#uncheckAll').click(function () {
        $("#dataTable").bootstrapTable('togglePagination').bootstrapTable('uncheckAll').bootstrapTable('togglePagination')
    })
    $(".time").each(function () {
        laydate.render({
            elem: "#" + $(this).attr("id")
            , type: "date"
            , trigger: 'click'
        });
    });
});
