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

/**
 * 初始化表格的列
 */
Housepurchase.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '申报年度', field: 'year', visible: true, align: 'center', valign: 'middle', width: "80px"},
        {title: '申报类型', field: 'declareType', visible: true, align: 'center', valign: 'middle', width: "100px",
            formatter(value, row, index) {
                if (value == 1) {
                    return "购房补贴";
                } else if (value == 2) {
                    return "免租入住";
                }
            }
        },
        {title: '姓名', field: 'name', visible: true, align: 'center', valign: 'middle', width: "100px"},
        {title: '证件号码', field: 'idCard', visible: true, align: 'center', valign: 'middle', width: "130px"},
        {title: '人才层次', field: 'talentArrangeName', visible: true, align: 'center', valign: 'middle', width: "90px"},
        {title: '联系电话', field: 'phone', visible: true, align: 'center', valign: 'middle', width: "100px"},
        {title: '婚姻状态', field: 'marryStatusName', visible: true, align: 'center', valign: 'middle', width: "80px"},
        {title: '配偶姓名', field: 'spouseName', visible: true, align: 'center', valign: 'middle', width: "100px"},
        {title: '配偶证件号码', field: 'spouseIdcard', visible: true, align: 'center', valign: 'middle', width: "150px"},
        {title: '房屋坐落地址', field: 'houseAddress', visible: true, align: 'center', valign: 'middle', width: "120px"},
        {title: '房屋建筑面积', field: 'houseArea', visible: true, align: 'center', valign: 'middle', width: "120px"},
        {title: '不动产权证编号', field: 'realEstateNo', visible: true, align: 'center', valign: 'middle', width: "120px"},
        {title: '房屋成交金额', field: 'houseMoney', visible: true, align: 'center', valign: 'middle', width: "120px"},
        {title: '兑现状态', field: "cashType", visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip', width: "80px",
            formatter: function (value, row, index) {
                if (Feng.isEmptyStr(value))
                    return "<span style='color: black'>未判定</span>";
                if (row.publicState >= 3) {
                    if (value == 1)
                        return "<span style='color: green'>兑现</span>";
                    if (value == 2)
                        return "<span style='color: red'>不予兑现</span>";
                } else {
                    return "<span style='color: black'>未判定</span>";
                }
            }
        },
        {title: '审核状态', field: 'checkState', visible: true, align: 'center', valign: 'middle', width: "100px",
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "<span class='label'>待提交</span>"
                } else if (value == 10) {
                    return "<span class='label label-danger'>已驳回</span>"
                } else {
                    if (row.publicState >= 3) {
                        if (value == -1) {
                            return "<span class='label label-danger'>审核不通过</span>"
                        } else if (value == 40) {
                            return "<span class='label label-primary'>审核通过</span>"
                        } else {
                            return "<span class='label label-success'>审核中</span>"
                        }
                    } else {
                        return "<span class='label label-success'>审核中</span>"
                    }
                }
            }
        },
        {title: '是否兑现', field: "publicState", visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip', width: "80px",
            formatter: function (value, row, index) {
                if (value < 3) {
                    return "<span class='label label-default'>未知</span>";
                }
                if (value == 3) {
                    if (row.cashType == 1)
                        return "<span class='label label-danger'>待兑现</span>";
                    if (row.cashType == 2)
                        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_house + "',null)\" >" +
                        "<i class=\"fa fa-book\"></i>日志" +
                        "</span>";
            }
        }
    ];
};

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

/**
 * 点击添加购房补贴
 */
Housepurchase.openAddHousepurchase = function () {
    var ajax = new $ax("/common/batch/checkBatchValid", function (data) {
        if (data.code == 200) {
            var index = layer.open({
                type: 2,
                title: '添加购房补贴',
                area: ['800px', '420px'], //宽高
                fix: false, //不固定
                maxmin: true,
                content: Feng.ctxPath + '/enterprise/house/apply?year=' + data.batch,
                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.HousepurchaseInfoDlg.addSubmit();
                }, btn2: function (index, layero) {
                    var obj = layero.find("iframe")[0].contentWindow;
                    obj.HousepurchaseInfoDlg.editSubmit(2);
                    return false;
                },
                success: function (layero, index) {
                    layer.tips('添加基本信息并上传附件后点击', '.layui-layer-btn1', {tips: [1, "#78BA32"], time: 0, closeBtn: 2});
                },
                end: function () {
                    layer.closeAll('tips');
                    Housepurchase.table.refresh();
                }
            });
            Housepurchase.layerIndex = index;
            layer.full(index);
        } else {
            Feng.info(data.msg);
        }
    }, function (data) {
        Feng.error("校验失败!" + data.responseJSON.message + "!");
    });
    ajax.set("type", CONFIG.project_house);
    ajax.start();
};

/**
 * 打开查看购房补贴详情
 */
Housepurchase.openHousepurchaseDetail = function () {
    if (this.check()) {
        var ajax = new $ax("/common/batch/checkBatchValid", function (data) {
            if (data.code == 200) {
                var index = layer.open({
                    type: 2,
                    title: '购房补贴详情',
                    area: ['800px', '420px'], //宽高
                    fix: false, //不固定
                    maxmin: true,
                    content: Feng.ctxPath + '/enterprise/house/apply/id/' + Housepurchase.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.HousepurchaseInfoDlg.addSubmit();
                    }, btn2: function (index, layero) {
                        var obj = layero.find("iframe")[0].contentWindow;
                        obj.HousepurchaseInfoDlg.editSubmit(2);
                        return false;
                    },
                    success: function (layero, index) {
                        layer.tips('添加基本信息并上传附件后点击', '.layui-layer-btn1', {tips: [1, "#78BA32"], time: 0, closeBtn: 2});
                    },
                    end: function () {
                        layer.closeAll('tips');
                        Housepurchase.table.refresh();
                    }
                });
                Housepurchase.layerIndex = index;
                layer.full(index);
            } else {
                Feng.info(data.msg);
            }
        }, function (data) {
            Feng.error("校验失败!" + data.responseJSON.message + "!");
        });
        ajax.set("type", CONFIG.project_house);
        ajax.set("year", Housepurchase.seItem.year);
        ajax.set("first_submit_time", Housepurchase.seItem.firstSubmitTime);
        ajax.start();
    }
};


Housepurchase.openHousepurchaseSelect = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '购房补贴详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/enterprise/house/detail/id/' + Housepurchase.seItem.id,
            btn: ['<i class="fa fa-eraser"></i>&nbsp;&nbsp;取消'],
            btnAlign: 'c',
        });
        Housepurchase.layerIndex = index;
        layer.full(index);
    }
}

/**
 * 删除购房补贴
 */
Housepurchase.delete = function () {
    if (this.check()) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/enterprise/house/delete", function (data) {
                if (data.code == 200) {
                    Feng.success(data.msg);
                    Housepurchase.table.refresh();
                } else {
                    Feng.info(data.msg);
                }
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", Housepurchase.seItem.id);
            ajax.start();
        }
        Feng.confirm("删除后无法恢复，确认删除吗?", operation);
    }
};

/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
Housepurchase.formParams = function () {
    var queryData = {};
    queryData['year'] = $("#year").val();
    queryData['name'] = $("#name").val();
    queryData['idCard'] = $("#idCard").val();
    queryData['talentArrange'] = $("#talentArrange").val();
    queryData['spouseName'] = $("#spouseName").val();
    queryData['spouseIdcard'] = $("#spouseIdcard").val();
    queryData['childName'] = $("#childName").val();
    queryData['childIdCard'] = $("#childIdCard").val();
    queryData['marryStatus'] = $("#marryStatus").val();
    return queryData;
}

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

/**
 * 重置
 */
Housepurchase.reset = function () {
    $("#year").val("");
    $("#name").val("");
    $("#idCard").val("");
    $("#talentArrange").val("");
    $("#spouseName").val("");
    $("#spouseIdcard").val("");
    $("#childName").val("");
    $("#childIdCard").val("");
    $("#marryStatus").val("");
}

$(function () {
    var defaultColunms = Housepurchase.initColumn();
    var table = new BSTable(Housepurchase.id, "/enterprise/house/list", defaultColunms);
    table.setPaginationType("server");
    Housepurchase.table = table.init();
    //批量加载字典表数据
    var arr = [
        {"name": "marryStatus", "code": "marry_status"},
        {"name": "talentArrange", "code": "talent_arrange"}];
    Feng.findChildDictBatch(JSON.stringify(arr));
});
