/**
 * 医疗补助管理初始化
 */
var MedicalSubsidy = {
    id: "MedicalSubsidyTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
MedicalSubsidy.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '申报年度', field: 'year', visible: true, align: 'center', valign: 'middle',width:"80px",'class': 'uitd_showTip'},
        {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:"150px",'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: 'address', visible: true, align: 'center', valign: 'middle',width:"120px",'class': 'uitd_showTip'},
        {title: '联系电话', field: 'phone', visible: true, align: 'center', valign: 'middle',width:"120px",'class': 'uitd_showTip'},
        {title: '开户银行网点', field: 'bankAddress', visible: true, align: 'center', valign: 'middle',width:"130px",'class': 'uitd_showTip'},
        {title: '银行账号', field: 'bankNumber', visible: true, align: 'center', valign: 'middle',width:"120px",'class': 'uitd_showTip'},
        {title: '兑现金额', field: 'cashMoney', visible: true, align: 'center', valign: 'middle',width:"100px",'class': 'uitd_showTip'},
        {title: '首次提交时间', field: 'firstSubmitTime', visible: true, align: 'center', valign: 'middle',width:"100px",'class': 'uitd_showTip'},
        {title: '最新提交时间', field: 'newSubmitTime', visible: true, align: 'center', valign: 'middle',width:"100px",'class': 'uitd_showTip'},
        {title: '审核状态', field: 'checkState', visible: true, align: 'center', valign: 'middle',width:"120px",'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: 'id', visible: true, align: 'center', valign: 'middle',width:"80px",
            formatter : function (value,row,index){
                return "<span class='label label-success' onclick=\"MedicalSubsidy.showLog('"+value+"')\" >" +
                    "<i class=\"fa fa-book\"></i>日志" +
                    "</span>";
            }
        }
    ];
};

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

/**
 * 点击添加医疗补助
 */
MedicalSubsidy.openAddMedicalSubsidy = function () {
    var ajax = new $ax(Feng.ctxPath + "/api/commonBatch/valiateIsAdd", function (data) {
        if (data.code == 200) {
            var index = layer.open({
                type: 2,
                title: '添加医疗补助',
                area: 'auto', //宽高
                fix: false, //不固定
                maxmin: true,
                content: Feng.ctxPath + '/api/medicalSubsidy/medicalSubsidy_add?year='+data.obj,
                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.MedicalSubsidyInfoDlg.addSubmit();
                },btn2: function(index, layero){
                    var obj = layero.find("iframe")[0].contentWindow;
                    obj.MedicalSubsidyInfoDlg.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');
                }
            });
            MedicalSubsidy.layerIndex = index;
            layer.full(index);
        }else{
            Feng.info(data.msg);
        }
    }, function (data) {
        Feng.error("校验失败!" + data.responseJSON.message + "!");
    });
    ajax.set("type",CONFIG.project_medical);
    ajax.start();
};

/**
 * 打开查看医疗补助详情
 */
MedicalSubsidy.openMedicalSubsidyDetail = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/api/commonBatch/valiateIsEditOrSubmit", function (data) {
            if (data.code == 200) {
                var index = layer.open({
                    type: 2,
                    title: '学杂费补助修改',
                    area: ['800px', '420px'], //宽高
                    fix: false, //不固定
                    maxmin: true,
                    content: Feng.ctxPath + '/api/medicalSubsidy/medicalSubsidy_update/1/' + MedicalSubsidy.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.MedicalSubsidyInfoDlg.editSubmit(1);
                    },btn2: function(index, layero){
                        var obj = layero.find("iframe")[0].contentWindow;
                        obj.MedicalSubsidyInfoDlg.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');
                        MedicalSubsidy.table.refresh();
                    }
                });
                MedicalSubsidy.layerIndex = index;
                layer.full(index);
            }else{
                Feng.info(data.msg);
            }
        }, function (data) {
            Feng.error("校验失败!" + data.responseJSON.message + "!");
        });
        ajax.set("type",CONFIG.project_medical);
        ajax.set("year",MedicalSubsidy.seItem.year);
        ajax.start();
    }
};

MedicalSubsidy.openMedicalSubsidySelect = function(){
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '医疗补助查看',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/api/medicalSubsidy/medicalSubsidy_update/2/' + MedicalSubsidy.seItem.id,
        });
        MedicalSubsidy.layerIndex = index;
        layer.full(index);
    }
}


/**
 * 删除医疗补助
 */
MedicalSubsidy.delete = function () {
    if (this.check()) {
        if(MedicalSubsidy.seItem.checkState != 1){
            Feng.error("仅能删除未提交的数据");
            return  ;
        }
        var operation = function() {
            var ajax = new $ax(Feng.ctxPath + "/api/medicalSubsidy/delete", function (data) {
                if(data.code == 200){
                    Feng.success(data.msg);
                    MedicalSubsidy.table.refresh();
                }else{
                    Feng.info(data.msg);
                }
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("medicalSubsidyId",MedicalSubsidy.seItem.id);
            ajax.start();
        }
        Feng.confirm("删除后无法恢复，确认删除吗?", operation);
    }
};

MedicalSubsidy.formParams = function(){
    var queryData = {};
    queryData['year'] = $("#year").val();
    queryData['name'] = $("#name").val();
    queryData['sex'] = $("#sex").val();
    queryData['cardType'] = $("#cardType").val();
    queryData['idcard'] = $("#idcard").val();
    queryData['talentArrange'] = $("#talentArrange").val();
    queryData['certificateNo'] = $("#certificateNo").val();
    queryData['address'] = $("#address").val();
    queryData['phone'] = $("#phone").val();
    queryData['checkState'] = $("#checkState").val();
    return queryData;
}


/**
 * 查询医疗补助列表
 */
MedicalSubsidy.search = function () {
    MedicalSubsidy.table.refresh({query: MedicalSubsidy.formParams()});
};


/**
 * 重置
 */
MedicalSubsidy.reset = function(){
    $("#year").val("");
    $("#name").val("");
    $("#sex").val("");
    $("#cardType").val("");
    $("#idcard").val("");
    $("#talentArrange").val("");
    $("#certificateNo").val("");
    $("#address").val("");
    $("#phone").val("");
    $("#checkState").val("");
}


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



$(function () {
    var defaultColunms = MedicalSubsidy.initColumn();
    var table = new BSTable(MedicalSubsidy.id, "/api/medicalSubsidy/list", defaultColunms);
    table.setPaginationType("server");
    table.setOnDblClickRow(function () {
        MedicalSubsidy.openMedicalSubsidyDetail();
    });
    MedicalSubsidy.table = table.init();
    var arr = [
        {"name":"talentArrange","code":"un_talentLevel"},
        {"name":"cardType","code":"un_cardType"}];
    Feng.findChildDictBatch(JSON.stringify(arr));
});
