/**
 * 学杂费补助管理初始化
 */
var ChildschoolFees = {
    id: "ChildschoolFeesTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
ChildschoolFees.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {
            title: '申报年度',
            field: 'year',
            visible: true,
            align: 'center',
            valign: 'middle',
            width: "80px",
            'class': 'uitd_showTip'
        },
        {
            title: '企业名称',
            field: 'enterpriseName',
            visible: true,
            align: 'center',
            valign: 'middle',
            width: "120px",
            'class': 'uitd_showTip'
        },
        {
            title: '父母姓名',
            field: 'pName',
            visible: true,
            align: 'center',
            valign: 'middle',
            width: "150px",
            'class': 'uitd_showTip',
            formatter: function (value, row, index) {
                if (row.pSex == 1) {
                    return value + '<span style="color:#6495ED">【男】</span>';
                }
                if (row.pSex == 2) {
                    return value + '<span style="color:#FF82AB">【女】</span>';
                }
            }
        },
        {
            title: '父母证件号码',
            field: 'pIdcard',
            visible: true,
            align: 'center',
            valign: 'middle',
            width: "150px",
            'class': 'uitd_showTip'
        },
        {
            title: '人才层次',
            field: 'talentArrangeName',
            visible: true,
            align: 'center',
            valign: 'middle',
            width: "100px",
            'class': 'uitd_showTip'
        },
        // {title: '人才编号', field: 'certificateNo', visible: true, align: 'center', valign: 'middle',width:"100px",'class': 'uitd_showTip'},
        {
            title: '籍贯',
            field: 'nativePlace',
            visible: true,
            align: 'center',
            valign: 'middle',
            width: "100px",
            'class': 'uitd_showTip'
        },
        {
            title: '联系电话',
            field: 'phone',
            visible: true,
            align: 'center',
            valign: 'middle',
            width: "100px",
            'class': 'uitd_showTip'
        },
        {
            title: '子女姓名',
            field: 'cName',
            visible: true,
            align: 'center',
            valign: 'middle',
            width: "120px",
            'class': 'uitd_showTip',
            formatter: function (value, row, index) {
                if (row.cSex == 1) {
                    return value + '<span style="color:#6495ED">【男】</span>';
                }
                if (row.cSex == 2) {
                    return value + '<span style="color:#FF82AB">【女】</span>';
                }
            }
        },
        {
            title: '子女证件号码',
            field: 'cIdcard',
            visible: true,
            align: 'center',
            valign: 'middle',
            width: "150px",
            'class': 'uitd_showTip'
        },
        // {title: '子女出生日期', field: 'cBirthday', visible: true, align: 'center', valign: 'middle',width:"120px",'class': 'uitd_showTip'},
        {
            title: '与申报人关系',
            field: 'cRelationName',
            visible: true,
            align: 'center',
            valign: 'middle',
            width: "120px",
            'class': 'uitd_showTip'
        },
        {
            title: '现就读学校',
            field: 'nowSchool',
            visible: true,
            align: 'center',
            valign: 'middle',
            width: "100px",
            'class': 'uitd_showTip'
        },
        {
            title: '现就读年级',
            field: 'nowGradeName',
            visible: true,
            align: 'center',
            valign: 'middle',
            width: "100px",
            'class': 'uitd_showTip'
        },
        {
            title: '实缴学杂费金额',
            field: 'paidInMoeny',
            visible: true,
            align: 'center',
            valign: 'middle',
            width: "120px",
            'class': 'uitd_showTip'
        },
        {
            title: '开户银行网点',
            field: 'bankAddress',
            visible: true,
            align: 'center',
            valign: 'middle',
            width: "120px",
            'class': 'uitd_showTip'
        },
        {
            title: '银行账号',
            field: 'bankNumber',
            visible: true,
            align: 'center',
            valign: 'middle',
            width: "130px",
            'class': 'uitd_showTip'
        },
        {
            title: '兑现金额',
            field: 'cashMoney',
            visible: true,
            align: 'center',
            valign: 'middle',
            width: "100px",
            'class': 'uitd_showTip'
        },
        {
            title: '审核状态',
            field: 'checkState',
            visible: true,
            align: 'center',
            valign: 'middle',
            width: "100px",
            'class': 'uitd_showTip',
            formatter: function (value, row, index) {
                if (value == -1) {
                    return '<span class=\'label label-danger\'>审核不通过</span>';
                }
                if (value == 1) {
                    return '<span class=\'label\'>待提交</span>';
                }
                if (value == 2) {
                    return '<span class=\'label label-info\'>待审核</span>';
                }
                if (value == 3) {
                    return '<span class=\'label label-success\'>重新提交</span>';
                }
                if (value == 4) {
                    return '<span class=\'label label-danger\'>审核驳回</span>';
                }
                if (value == 5) {
                    return '<span class=\'label label-primary\'>审核通过</span>';
                }
            }
        },
        {
            title: '公示状态',
            field: 'publicState',
            visible: true,
            align: 'center',
            valign: 'middle',
            width: "100px",
            'class': 'uitd_showTip',
            formatter: function (value, row, index) {
                if (value == 1) {
                    return '<span class=\'label label-success\'>待公示</span>';
                }
                if (value == 2) {
                    return '<span class=\'label label-danger\'>公示中</span>';
                }
                if (value == 3) {
                    return '<span class=\'label label-info\'>待兑现</span>';
                }
                if (value == 4) {
                    return '<span class=\'label label-primary\'>已兑现</span>';
                }
            }
        },
        {
            title: '操作', field: 'id', visible: true, align: 'center', valign: 'middle', width: "80px",
            formatter: function (value, row, index) {
                return "<span class='label label-success' onclick=\"ChildschoolFees.showLog('" + value + "')\" >" +
                    "<i class=\"fa fa-book\"></i>日志" +
                    "</span>";
            }
        }
    ];
};

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

/**
 * 收集数据
 */
ChildschoolFees.formParams = function () {
    var queryData = {};
    queryData['year'] = $("#year").val();
    queryData['enterpriseName'] = $("#enterpriseName").val();
    queryData['pName'] = $("#pName").val();
    queryData['pSex'] = $("#pSex").val();
    queryData['pCardType'] = $("#pCardType").val();
    queryData['pIdcard'] = $("#pIdcard").val();
    queryData['talentArrange'] = $("#talentArrange").val();
    queryData['certificateNo'] = $("#certificateNo").val();
    queryData['address'] = $("#address").val();
    queryData['phone'] = $("#phone").val();
    queryData['cName'] = $("#cName").val();
    queryData['cSex'] = $("#cSex").val();
    queryData['cCardType'] = $("#cCardType").val();
    queryData['cIdcard'] = $("#cIdcard").val();
    queryData['cRelation'] = $("#cRelation").val();
    queryData['nowSchool'] = $("#nowSchool").val();
    queryData['nowGrade'] = $("#nowGrade").val();
    queryData['checkState'] = $("#checkState").val();
    return queryData;
}

/**
 * 重置
 */
ChildschoolFees.reset = function () {
    $("#year").val("");
    $("#enterpriseName").val("");
    $("#pName").val("");
    $("#pSex").val("");
    $("#pCardType").val("");
    $("#pIdcard").val("");
    $("#talentArrange").val("");
    $("#certificateNo").val("");
    $("#address").val("");
    $("#phone").val("");
    $("#cName").val("");
    $("#cSex").val("");
    $("#cCardType").val("");
    $("#cIdcard").val("");
    $("#cRelation").val("");
    $("#nowSchool").val("");
    $("#nowGrade").val("");
    $("#checkState").val("");
}

/**
 * 查询学杂费补助列表
 */
ChildschoolFees.search = function () {
    ChildschoolFees.table.refresh({query: ChildschoolFees.formParams()});
};

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

ChildschoolFees.openCheckChildschoolFees = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '学杂费补助审核',
            area: 'auto', //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/childschoolFees/toCheckPage/' + ChildschoolFees.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.ChildschoolFeesInfoDlg.showCheckModal();
            }, btn2: function (index, layero) {
                var obj = layero.find("iframe")[0].contentWindow;
                obj.ChildschoolFeesInfoDlg.submitCheck();
                return false;
            },
            success: function (layero, index) {
                layer.tips('审核完成并确认无误后点击', '.layui-layer-btn1', {tips: [1, "#78BA32"], time: 0, closeBtn: 2});
            },
            end: function () {
                layer.closeAll('tips');
            }
        });
        ChildschoolFees.layerIndex = index;
        layer.full(index);
    }
}

/**
 * 显示需要处理的数据
 * @param type
 */
ChildschoolFees.showDataCheckModal = function (type) {
    $("#hczxForm").css("display", "none");
    switch (type) {
        case 1:                     //公式预览
            $("#hczxButton").attr("onclick", "ChildschoolFees.publicExport()").text("导出");
            $("#exportCommonModalLabel").text("公式预览");
            break;
        case 2:                     //待核查征信名单-核查征信通过
            $("#hczxButton").attr("onclick", "ChildschoolFees.public()").text("提交");
            $("#exportCommonModalLabel").text("公示");
            $("#hczxForm").css("display", "block");
            $(".time").each(function () {
                laydate.render({
                    elem: "#" + $(this).attr("id")
                    , type: 'date'
                    , format: 'yyyy年MM月dd日'
                });
            });
            break;
        case 3:                     //公示通过
            $("#hczxButton").attr("onclick", "ChildschoolFees.publicPass()").text("提交");
            $("#exportCommonModalLabel").text("批量公示通过");
            break;
        case 4:                     //兑现
            $("#hczxButton").attr("onclick", "ChildschoolFees.cash()").text("兑现");
            $("#exportCommonModalLabel").text("兑现");
            break;
    }
    $('#dataTable').bootstrapTable('destroy');
    $('#dataTable').bootstrapTable({
        url: Feng.ctxPath + "/childschoolFees/selectNeedCheckData?type=" + type,
        method: 'POST',
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        search: false,					// 是否显示表格搜索，此搜索是客户端搜索，不会进服务端
        showRefresh: false,				// 是否显示刷新按钮
        clickToSelect: true,			// 是否启用点击选中行
        singleSelect: false,				// 设置True 将禁止多选
        striped: true,  				// 是否显示行间隔色
        pagination: true,   			// 设置为 true 会在表格底部显示分页条
        paginationHAlign: "left",
        paginationDetailHAlign: "right",
        sidePagination: "client",   	// 设置在哪里进行分页，可选值为 'client' 或者 'server'
        pageNumber: 1,                       //初始化加载第一页，默认第一页
        pageSize: 10,                       //每页的记录行数（*）
        pageList: [10, 25, 50, 100, 500, 1000, 1500],        //可供选择的每页的行数（*）
        maintainSelected: true,              //全表全选需要开启
        showColumns: false,
        responseHandler: function (res) {
            $("#exportCommonModal").modal("show");
            return res.obj.rows;
        },
        columns:
            [
                {field: "selectItem", checkbox: true},
                {title: '父母姓名', field: 'pName', visible: true, align: 'center', valign: 'middle', width: "10%"},
                {title: '父母证件号码', field: 'pIdcard', visible: true, align: 'center', valign: 'middle', width: "30%"},
                {title: '子女姓名', field: 'cName', visible: true, align: 'center', valign: 'middle', width: "10%"},
                {title: '子女证件号码', field: 'cIdcard', visible: true, align: 'center', valign: 'middle', width: "30%"},
                {
                    title: '审核状态', field: 'checkState', visible: true, align: 'center', valign: 'middle', width: "10%",
                    formatter: function (value, row, index) {
                        if (value == -1) {
                            return "<span style='color: #ed5565;'>审核不通过</span>";
                        }
                        if (value == 5) {
                            return "<span style='color: #1ab394;'>审核通过</span>";
                        }
                    }
                },
            ]

    });
}

/**
 * 是否发送短信
 */
ChildschoolFees.toggleMessage = function () {
    var isMessage = $("input[name='isSend']:checked").val();
    if (isMessage == 1) {
        $("#messageEdit").css("display", "block");
    } else if (isMessage == 2) {
        $("#messageEdit").css("display", "none");
    }
}

ChildschoolFees.prepareSearch = function () {
    var sex = $("#pub_sex").val();
    var checkState = $("#pub_checkState").val();
    var name = $("#pub_name").val();
    $('#dataTable').bootstrapTable("refresh", {"query": {"cSex": sex, "checkState": checkState, "cName": name}});
}

ChildschoolFees.prepareReset = function () {
    $("#pub_sex").val("");
    $("#pub_checkState").val("");
    $("#pub_name").val("");
}

/**
 * 公式预览
 */
ChildschoolFees.publicExport = 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 + ",";
    }
    var operation = function () {
        $("#exportCommonModal").modal("hide");
        window.location.href = encodeURI(encodeURI(Feng.ctxPath + "/childschoolFees/exportPublic?ids=" + ids));
    }
    Feng.confirm("确定要公示预览吗?", operation);
}

/**
 * 公示
 */
ChildschoolFees.public = 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 + ",";
    }
    var isMessage = $("input[name='isSend']:checked").val();
    var typeName = $("#typeName").val();
    var address = $("#web").val();
    var publicStartTime = $("#publicStartTime").val();
    var publicEndTime = $("#publicEndTime").val();
    var dep = $("#dep").val();
    var phone = $("#fyphone").val();
    var email = $("#fyemail").val();
    if (isMessage == 1) {
        if (typeName == null || typeName == '') {
            Feng.info("请填写公示类型");
            return;
        }
        if (address == null || address == '') {
            Feng.info("请填写公示平台");
            return;
        }
        if (publicStartTime == null || publicStartTime == '') {
            Feng.info("请填写公示开始时间");
            return;
        }
        if (publicEndTime == null || publicEndTime == '') {
            Feng.info("请填写公示截止时间");
            return;
        }
        if (dep == null || dep == '') {
            Feng.info("请填写反映单位");
            return;
        }
        if (phone == null || phone == '') {
            Feng.info("请填写联系电话");
            return;
        }
        if (email == null || email == '') {
            Feng.info("请填写联系邮箱");
            return;
        }
    }
    var operation = function () {
        var ajax = new $ax(Feng.ctxPath + "/childschoolFees/publicBatch", function (data) {
            if (data.code == 200) {
                Feng.success(data.msg);
                ChildschoolFees.table.refresh();
                $("#exportCommonModal").modal("hide");
            } else {
                Feng.error(data.msg);
            }
        }, function (data) {
            Feng.error("公示失败!" + data.responseJSON.message + "!");
        });
        ajax.set("ids", ids);
        ajax.set("typeName", typeName);
        ajax.set("address", address);
        ajax.set("publicStartTime", publicStartTime);
        ajax.set("publicEndTime", publicEndTime);
        ajax.set("dep", dep);
        ajax.set("phone", phone);
        ajax.set("email", email);
        ajax.set("isMessage", isMessage);
        ajax.start();
    }
    Feng.confirm("确定要公示吗?", operation);
}

/**
 * 公示后审核
 */
ChildschoolFees.afterCheck = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/childschoolFees/validateIsCheck", function (data) {
            if (data.code == 200) {
                layer.open({
                    type: 1,
                    id: "neewFieldFormModel",
                    title: '公示再审核',
                    area: ['800px', '450px'], //宽高
                    fix: false, //不固定
                    shade: 0,
                    maxmin: true,
                    content: ChildschoolFees.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) {
                        layer.setTop(layero);
                    },
                    yes: function (index, layero) {
                        var checkState = $("#checkStateModel").val();
                        var checkMsg = $("#checkMsg").val();
                        if (checkState == null || checkState == '') {
                            Feng.info("请选择审核状态");
                            return;
                        }
                        if (checkMsg == null || checkMsg == '') {
                            Feng.info("请填写审核意见");
                            return;
                        }
                        var ajax = new $ax(Feng.ctxPath + "/childschoolFees/afterCheck", function (data) {
                            if (data.code == 200) {
                                layer.close(index);
                                ChildschoolFees.table.refresh();
                                Feng.success(data.msg);
                            } else {
                                Feng.error(data.msg);
                            }
                        }, function (data) {
                            Feng.error("审核失败!" + data.responseJSON.message + "!");
                        });
                        ajax.setData({"id": ChildschoolFees.seItem.id, "checkState": checkState, "checkMsg": checkMsg});
                        ajax.start();
                    }
                });
            } else {
                Feng.error(data.msg);
            }
        }, function (data) {
            Feng.error("校验失败!" + data.responseJSON.message + "!");
        });
        ajax.set("id", ChildschoolFees.seItem.id);
        ajax.set("process", 2);
        ajax.start();
    }
}

/**
 *
 */
ChildschoolFees.creatFieldCheckModal = function () {
    return '<form id="checkForm">\n' +
        '                    <div class="form-group" style="margin: 10px;">\n' +
        '                        <label for="checkState" class="control-label">审核状态</label>\n' +
        '                        <select class="form-control" id="checkStateModel" >\n' +
        '                            <option value="">请选择</option>\n' +
        '                            <option value="4">审核驳回</option>\n' +
        '                            <option value="-1">审核不通过</option>\n' +
        '                        </select>\n' +
        '                    </div>\n' +
        '                    <div class="form-group" style="margin: 10px;">\n' +
        '                        <label for="checkMsg" class="control-label" >审核意见</label>\n' +
        '                        <textarea class="form-control" id="checkMsg"  rows="6"></textarea>\n' +
        '                    </div>\n' +
        '                </form>';
}


/**
 * 公示通过
 */
ChildschoolFees.publicPass = 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 + ",";
    }
    var operation = function () {
        var ajax = new $ax(Feng.ctxPath + "/childschoolFees/publicPass", function (data) {
            if (data.code == 200) {
                Feng.success(data.msg);
                ChildschoolFees.table.refresh();
                $("#exportCommonModal").modal("hide");
            } else {
                Feng.error(data.msg);
            }
        }, function (data) {
            Feng.error("公示通过失败!" + data.responseJSON.message + "!");
        });
        ajax.set("ids", ids);
        ajax.start();
    }
    Feng.confirm("一旦提交无法修改，确定公示通过吗?", operation);
}

/**
 * 兑现
 */
ChildschoolFees.cash = 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 + ",";
    }
    var operation = function () {
        var ajax = new $ax(Feng.ctxPath + "/childschoolFees/cash", function (data) {
            if (data.code == 200) {
                Feng.success(data.msg);
                ChildschoolFees.table.refresh();
                $("#exportCommonModal").modal("hide");
            } else {
                Feng.error(data.msg);
            }
        }, function (data) {
            Feng.error("兑现失败!" + data.responseJSON.message + "!");
        });
        ajax.set("ids", ids);
        ajax.start();
    }
    Feng.confirm("一旦兑现无法修改，确定兑现吗?", operation);
}

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

/**
 * 导出基础信息
 */
ChildschoolFees.export = function(){
    var names = '';
    var values = '';
    $("#field_info li input").each(function(index){
        if($(this).is(":checked")){
            values = values + $(this).val() + ",";
            names = names + $(this).next().text() + ",";
        }
    });
    var queryData = ChildschoolFees.formParams();
    queryData['names'] = names;
    queryData['values'] = values;
    var url = Feng.setUrlParam(Feng.ctxPath + "/childschoolFees/exportBasicInfo",queryData);
    $("#basicExportModal").modal('hide');
    window.location.href = url;
}


$(function () {
    var defaultColunms = ChildschoolFees.initColumn();
    var table = new BSTable(ChildschoolFees.id, "/childschoolFees/list", defaultColunms);
    table.setPaginationType("server");
    table.setOnDblClickRow(function () {
        ChildschoolFees.openCheckChildschoolFees();
    });
    ChildschoolFees.table = table.init();
    //批量加载字典表数据
    var arr = [
        {"name": "talentArrange", "code": "un_talentLevel"},
        {"name": "cRelation", "code": "un_education_relation"},
        {"name": "nowGrade", "code": "un_grade"},
        {"name": "pCardType", "code": "un_cardType"},
        {"name": "cCardType", "code": "un_cardType"}];
    Feng.findChildDictBatch(JSON.stringify(arr));
    $('#checkAll').click(function () {
        $("#dataTable").bootstrapTable('togglePagination').bootstrapTable('checkAll').bootstrapTable('togglePagination');
    })
    $('#uncheckAll').click(function () {
        $("#dataTable").bootstrapTable('togglePagination').bootstrapTable('uncheckAll').bootstrapTable('togglePagination')
    })
});
