/**
 * 重大专项经费配套管理初始化
 */
var MajorProjects = {
    id: "MajorProjectsTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
MajorProjects.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '是否在库人才', field: 'source', visible: true, align: 'center', valign: 'middle',width:"90px",'class': 'uitd_showTip',
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "<span style='color: red'>是</span>";
                }
                if (value == 2) {
                    return "<span style='color: green'>否</span>";
                }
            }
        },
        {title: '姓名', field: 'name', visible: true, align: 'center', valign: 'middle',width:"100px",'class': 'uitd_showTip',
            formatter: function (value, row, index) {
                if (value == 1) {
                    return row.name+"<span style='color: red'>【男】</span>";
                }
                if (value == 2) {
                    return row.name+"<span style='color: green'>【女】</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:"150px",'class': 'uitd_showTip'},
        {title: '人才层次', field: 'talentArrangeName', visible: true, align: 'center', valign: 'middle',width:"90px",'class': 'uitd_showTip'},
        {title: '国家/地区', field: 'nationalityName', visible: true, align: 'center', valign: 'middle',width:"100px",'class': 'uitd_showTip'},
        {title: '岗位/职位', field: 'post', visible: true, align: 'center', valign: 'middle',width:"100px",'class': 'uitd_showTip'},
        {title: '开户银行', field: 'bankName', visible: true, align: 'center', valign: 'middle',width:"100px",'class': 'uitd_showTip'},
        {title: '开户银行网点', field: 'bankAddress', visible: true, align: 'center', valign: 'middle',width:"100px",'class': 'uitd_showTip'},
        {title: '银行账号', field: 'bankNumber', visible: true, align: 'center', valign: 'middle',width:"100px",'class': 'uitd_showTip'},
        {title: '联系电话', field: 'phone', visible: true, align: 'center', valign: 'middle',width:"120px",'class': 'uitd_showTip'},
        {title: '入选重大专项批准文件', field: 'documentName', visible: true, align: 'center', valign: 'middle',width:"150px",'class': 'uitd_showTip'},
        {title: '重大专项级别、名称', field: 'degree', visible: true, align: 'center', valign: 'middle',width:"150px",'class': 'uitd_showTip'},
        {title: '各级政府配套经费资助', field: 'fundsSupport', visible: true, align: 'center', valign: 'middle',width:"150px",'class': 'uitd_showTip'},
        {title: '配套额度比例', field: 'quotaProportion', visible: true, align: 'center', valign: 'middle',width:"120px",'class': 'uitd_showTip'},
        {title: '申请晋江市配套资助金额', field: 'applicationAmount', visible: true, align: 'center', valign: 'middle',width:"150px",'class': 'uitd_showTip'},
        {title: '审核状态', field: 'checkStateName', visible: true, align: 'center', valign: 'middle',width:"100px",'class': 'uitd_showTip'},
        {
            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_major + "',null)\" >" +
                    "<i class=\"fa fa-book\"></i>日志" +
                    "</span>";
            }
        }
    ];
};

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

/**
 * 点击添加重大专项经费配套
 */
MajorProjects.openAddMajorProjects = function () {
    var index = layer.open({
        type: 2,
        title: '添加重大专项经费配套',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/api/majorProjects/majorProjects_add',
        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.MajorProjectsInfoDlg.addSubmit();
        }, btn2: function (index, layero) {
            var obj = layero.find("iframe")[0].contentWindow;
            obj.MajorProjectsInfoDlg.submitToCheck();
            return false;
        },
        success: function (layero, index) {
            layer.tips('添加基本信息并上传附件后点击', '.layui-layer-btn1', {tips: [1, "#78BA32"], time: 0, closeBtn: 2});
        },
        end: function () {
            layer.closeAll('tips');
        }
    });
    MajorProjects.layerIndex = index;
    layer.full(index);
};

/**
 * 打开查看重大专项经费配套详情
 */
MajorProjects.openMajorProjectsDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '修改重大专项',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/api/majorProjects/majorProjects_update/' + MajorProjects.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.MajorProjectsInfoDlg.editSubmit();
            }, btn2: function (index, layero) {
                var obj = layero.find("iframe")[0].contentWindow;
                obj.MajorProjectsInfoDlg.submitToCheck();
                return false;
            },
            success: function (layero, index) {
                layer.tips('添加基本信息并上传附件后点击', '.layui-layer-btn1', {tips: [1, "#78BA32"], time: 0, closeBtn: 2});
            },
            end: function () {
                layer.closeAll('tips');
            }
        });
        MajorProjects.layerIndex = index;
        layer.full(index);
    }
};

/**
 * 删除重大专项经费配套
 */
MajorProjects.delete = function () {
    if (this.check()) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/api/majorProjects/delete", function (data) {
                if (data.code == 200) {
                    Feng.success(data.msg);
                    MajorProjects.table.refresh();
                } else {
                    Feng.info(data.msg);
                }
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("majorProjectsId", MajorProjects.seItem.id);
            ajax.start();
        }
        Feng.confirm("删除后无法恢复，确认删除吗?", operation);
    }
};

/**
 * 收集数据
 */
MajorProjects.formParams = function () {
    var queryData = {};
    queryData['source'] = $("#source").val();
    queryData['name'] = $("#name").val();
    queryData['sex'] = $("#sex").val();
    queryData['cardType'] = $("#cardType").val();
    queryData['idCard'] = $("#idCard").val();
    queryData['nationality'] = $("#nationality").val();
    queryData['phone"'] = $("#idCard").val();
    queryData['degree'] = $("#degree").val();
    queryData['checkState'] = $("#nationality").val();
    return queryData;
}

/**
 * 重置数据
 */
MajorProjects.reset = function () {
    $("#source").val("");
    $("#name").val("");
    $("#sex").val("");
    $("#cardType").val("");
    $("#idCard").val("");
    $("#nationality").val("");
    $("#phone").val("");
    $("#degree").val("");
    $("#checkState").val("");
}

/**
 * 查询重大专项经费配套列表
 */
MajorProjects.search = function () {
    MajorProjects.table.refresh({query: MajorProjects.formParams()});
};

$(function () {
    var defaultColunms = MajorProjects.initColumn();
    var table = new BSTable(MajorProjects.id, "/api/majorProjects/list", defaultColunms);
    table.setPaginationType("server");
    table.setOnDblClickRow(function () {
        MajorProjects.openMajorProjectsDetail();
    });
    MajorProjects.table = table.init();
    var arr = [
        {"name": "cardType", "code": "un_cardType"},
        {"name": "nationality", "code": "un_nationality"},
        {"name": "degree", "code": "un_major_degree"}];
    Feng.findChildDictBatch(JSON.stringify(arr));
});
