/**
 * 购房补助管理初始化
 */
var HouseRentingPurchaseFees = {
    id: "HouseRentingPurchaseFeesTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
HouseRentingPurchaseFees.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '姓名', field: 'name', visible: true, align: 'center', valign: 'middle',width:"120px",'class': 'uitd_showTip',
            formatter : function (value,row,index) {
                if(row.sex==1){
                    return value+'<span style="color:#6495ED">【男】</span>';
                }if(row.sex==2){
                    return value+'<span style="color:#FF82AB">【女】</span>';
                }
            }
        },
        {title: '证件类型', field: 'cardTypeName', visible: true, align: 'center', valign: 'middle',width:"80px",'class': 'uitd_showTip'},
        {title: '证件号码', field: 'idCard', visible: true, align: 'center', valign: 'middle',width:"120px",'class': 'uitd_showTip'},
        {title: '人才层次', field: 'talentArrangeName', 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: 'entryTime', visible: true, align: 'center', valign: 'middle',width:"100px",'class': 'uitd_showTip'},
        // {title: '岗位/职务', field: 'post', visible: true, align: 'center', valign: 'middle'},
        // {title: '工作单位地址', field: 'enterpriseAddress', visible: true, align: 'center', valign: 'middle'},
        // {title: '现居地址', field: 'address', visible: true, align: 'center', valign: 'middle'},
        {title: '联系电话', field: 'phone', visible: true, align: 'center', valign: 'middle',width:"120px",'class': 'uitd_showTip'},
        // {title: '电子邮箱', field: 'email', visible: true, align: 'center', valign: 'middle'},
        {title: '婚姻状态', field: 'marryStatusName', visible: true, align: 'center', valign: 'middle',width:"80px",'class': 'uitd_showTip'},
        {title: '配偶姓名', field: 'spouseName', visible: true, align: 'center', valign: 'middle',width:"120px",'class': 'uitd_showTip',
            formatter : function (value,row,index) {
                if(row.spouseSex==1){
                    return value+'<span style="color:#6495ED">【男】</span>';
                }if(row.spouseSex==2){
                    return value+'<span style="color:#FF82AB">【女】</span>';
                }
            }
        },
        {title: '配偶证件类型', field: 'spouseCardTypeName', visible: true, align: 'center', valign: 'middle',width:"100px",'class': 'uitd_showTip'},
        {title: '配偶证件号码', field: 'spouseIdCard', visible: true, align: 'center', valign: 'middle',width:"120px",'class': 'uitd_showTip'},
        {title: '配偶人才层次', field: 'spouseTalentArrangeName', visible: true, align: 'center', valign: 'middle',width:"100px",'class': 'uitd_showTip'},
        // {title: '配偶籍贯', field: 'spouseNativePlace', visible: true, align: 'center', valign: 'middle',width:"120px",'class': 'uitd_showTip'},
        // {title: '配偶入职时间', field: 'spouseEntryTime', visible: true, align: 'center', valign: 'middle'},
        // {title: '配偶岗位/职务', field: 'spousePost', visible: true, align: 'center', valign: 'middle'},
        // {title: '配偶工作单位', field: 'spouseEnterpriseName', visible: true, align: 'center', valign: 'middle',width:"120px",'class': 'uitd_showTip'},
        // {title: '配偶工作单位地址', field: 'spouseEnterpriseAddress', visible: true, align: 'center', valign: 'middle'},
        // {title: '配偶现居地址', field: 'spouseAddress', visible: true, align: 'center', valign: 'middle'},
        {title: '配偶联系电话', field: 'spousePhone', visible: true, align: 'center', valign: 'middle',width:"120px",'class': 'uitd_showTip'},
        // {title: '配偶电子邮箱', field: 'spouseEmail', visible: true, align: 'center', valign: 'middle'},
        {title: '房产人才社区', field: 'houseTypeName', visible: true, align: 'center', valign: 'middle',width:"150px",'class': 'uitd_showTip'},
        {title: '房产详细地址', field: 'houseAddress', visible: true, align: 'center', valign: 'middle',width:"150px",'class': 'uitd_showTip'},
        {title: '购房金额', field: 'houseMoney', visible: true, align: 'center', valign: 'middle',width:"100px",'class': 'uitd_showTip'},
        {title: '开户银行网点', field: 'bankAddress', visible: true, align: 'center', valign: 'middle',width:"120px",'class': 'uitd_showTip'},
        {title: '银行账号', field: 'bankAccount', visible: true, align: 'center', valign: 'middle',width:"120px",'class': 'uitd_showTip'},
        {title: '兑现金额', field: 'money', visible: true, align: 'center', valign: 'middle',width:"100px",'class': 'uitd_showTip'},
        {title: '审核状态', field: 'checkStateName', visible: true, align: 'center', valign: 'middle',width:"100px",'class': 'uitd_showTip',width:"100px"},
        {title: '公示状态', field: 'publicState', visible: true, align: 'center', valign: 'middle',width:"100px",'class': 'uitd_showTip',width:"100px",
            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=\"Feng.getCheckLogModel('"+value+"','"+CONFIG.project_renting_purchase_fees+"',null)\" >" +
                    "<i class=\"fa fa-book\"></i>日志" +
                    "</span>";
            }
        }
    ];
};

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

/**
 * 点击添加购房补助
 */
HouseRentingPurchaseFees.openCheckHouseRentingPurchaseFees = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '购房补助详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/houseRentingPurchaseFees/toCheckPage/' + HouseRentingPurchaseFees.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.HouseRentingPurchaseFeesInfoDlg.showCheckModal();
            },btn2: function(index, layero){
                var obj = layero.find("iframe")[0].contentWindow;
                obj.HouseRentingPurchaseFeesInfoDlg.submitCheck();
                return false;
            },
            success: function (layero, index) {
                layer.tips('审核完成并确认无误后点击', '.layui-layer-btn1', {tips: [1, "#78BA32"], time: 0, closeBtn: 2});
            },
            end :function () {
                layer.closeAll('tips');
                HouseRentingPurchaseFees.table.refresh();
            }
        });
        HouseRentingPurchaseFees.layerIndex = index;
        layer.full(index);
    }
};


/**
* 查询表单提交参数对象
* @returns {{}}
*/
HouseRentingPurchaseFees.formParams = function() {
    var queryData = {};
    queryData['enterpriseName'] = $("#enterpriseName").val();
    queryData['name'] = $("#name").val();
    queryData['sex'] = $("#sex").val();
    queryData['cardType'] = $("#cardType").val();
    queryData['idCard'] = $("#idCard").val();
    queryData['talentArrange'] = $("#talentArrange").val();
    queryData['marryStatus'] = $("#marryStatus").val();
    queryData['spouseName'] = $("#spouseName").val();
    queryData['spouseSex'] = $("#spouseSex").val();
    queryData['spouseCardType'] = $("#spouseCardType").val();
    queryData['spouseIdcard'] = $("#spouseIdcard").val();
    queryData['spouseTalentArrange'] = $("#spouseTalentArrange").val();
    queryData['houseType'] = $("#houseType").val();
    queryData['checkState'] = $("#checkState").val()
    queryData['houseAddress'] = $("#houseAddress").val();
    return queryData;
}

/**
 * 查询购房补贴列表
 */
HouseRentingPurchaseFees.search = function () {
    HouseRentingPurchaseFees.table.refresh({query: HouseRentingPurchaseFees.formParams()});
};

/**
 * 重置
 */
HouseRentingPurchaseFees.reset = function(){
    $("#enterpriseName").val("");
    $("#name").val("");
    $("#sex").val("");
    $("#cardType").val("");
    $("#idCard").val("");
    $("#talentArrange").val("");
    $("#marryStatus").val("");
    $("#spouseName").val("");
    $("#spouseSex").val("");
    $("#spouseCardType").val("");
    $("#spouseIdcard").val("");
    $("#spouseTalentArrange").val("");
    $("#houseType").val("");
    $("#checkState").val("");
    $("#houseAddress").val("");
}


/**
 * 显示需要处理的数据
 * @param type
 */
HouseRentingPurchaseFees.showDataCheckModal = function (type) {
    $("#hczxForm").css("display", "none");
    switch (type) {
        case 1:                     //公式预览
            $("#hczxButton").attr("onclick", "HouseRentingPurchaseFees.publicExport()").text("导出");
            $("#exportCommonModalLabel").text("公式预览");
            break;
        case 2:                     //待核查征信名单-核查征信通过
            $("#hczxButton").attr("onclick", "HouseRentingPurchaseFees.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", "HouseRentingPurchaseFees.publicPass()").text("提交");
            $("#exportCommonModalLabel").text("批量公示通过");
            break;
        case 4:                     //兑现
            $("#hczxButton").attr("onclick", "HouseRentingPurchaseFees.cash()").text("兑现");
            $("#exportCommonModalLabel").text("兑现");
            break;
    }
    $('#dataTable').bootstrapTable('destroy');
    $('#dataTable').bootstrapTable({
        url: Feng.ctxPath + "/houseRentingPurchaseFees/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: 'name', visible: true, align: 'center', valign: 'middle', width: "10%"},
                {title: '证件号码', field: 'idCard', visible: true, align: 'center', valign: 'middle', width: "30%"},
                {title: '人才层次', field: 'talentArrangeName', visible: true, align: 'center', valign: 'middle', width: "10%"},
                {
                    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>";
                        }
                    }
                },
            ]

    });
}

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

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

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

/**
 * 公式预览
 */
HouseRentingPurchaseFees.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 + "/houseRentingPurchaseFees/exportPublic?ids=" + ids));
    }
    Feng.confirm("确定要公示预览吗?", operation);
}

/**
 * 公示
 */
HouseRentingPurchaseFees.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 + "/houseRentingPurchaseFees/publicBatch", function (data) {
            if (data.code == 200) {
                Feng.success(data.msg);
                HouseRentingPurchaseFees.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);
}

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

/**
 *
 */
HouseRentingPurchaseFees.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>';
}


/**
 * 公示通过
 */
HouseRentingPurchaseFees.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 + "/houseRentingPurchaseFees/publicPass", function (data) {
            if (data.code == 200) {
                Feng.success(data.msg);
                HouseRentingPurchaseFees.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);
}

/**
 * 兑现
 */
HouseRentingPurchaseFees.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 + "/houseRentingPurchaseFees/cash", function (data) {
            if (data.code == 200) {
                Feng.success(data.msg);
                HouseRentingPurchaseFees.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);
}

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

/**
 * 导出基础信息
 */
HouseRentingPurchaseFees.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 = HouseRentingPurchaseFees.formParams();
    queryData['names'] = names;
    queryData['values'] = values;
    var url = Feng.setUrlParam(Feng.ctxPath + "/houseRentingPurchaseFees/exportBasicInfo",queryData);
    $("#basicExportModal").modal('hide');
    window.location.href = url;
}


$(function () {
    var defaultColunms = HouseRentingPurchaseFees.initColumn();
    var table = new BSTable(HouseRentingPurchaseFees.id, "/houseRentingPurchaseFees/list", defaultColunms);
    table.setPaginationType("server");
    HouseRentingPurchaseFees.table = table.init();
    //批量加载字典表数据
    var arr = [
        {"name":"marryStatus","code":"un_marryStatus"},
        {"name":"talentArrange","code":"un_talentLevel"},
        {"name":"cardType","code":"un_cardType"},
        {"name":"spouseCardType","code":"un_cardType"},
        {"name":"spouseTalentArrange","code":"un_talentLevel"},
        {"name":"houseType","code":"un_renting_houseType"},
        {"name":"childCardType","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')
    })
});
