/**
 * 培训补贴管理初始化
 */
var TrainingSubsidy = {
    id: "TrainingSubsidyTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
TrainingSubsidy.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: "80px",'class': 'uitd_showTip'},
            {title: '性别', field: 'sex', visible: true, align: 'center', valign: 'middle',width:"60px",'class': 'uitd_showTip',
                formatter : function (value,row,index){
                    if(value==1){
                        return '男';
                    }if(value==2){
                        return '女';
                    }
                }
            },
            {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: 'nationalityName', visible: true, align: 'center', valign: 'middle',width:"80px",'class': 'uitd_showTip'},
            {title: '入职时间', field: 'entryTime', visible: true, align: 'center', valign: 'middle',width:"100px",'class': 'uitd_showTip'},
            {title: '合同开始时间', field: 'startTime', visible: true, align: 'center', valign: 'middle',width:"110px",'class': 'uitd_showTip'},
            {title: '合同结束时间', field: 'endTime', visible: true, align: 'center', valign: 'middle',width:"110px",'class': 'uitd_showTip'},
            {title: '上一年度年薪', field: 'yearSalary', visible: true, align: 'center', valign: 'middle',width:"120px",'class': 'uitd_showTip'},
            {title: '上一年度缴纳个税总额', field: 'yearTax', visible: true, align: 'center', valign: 'middle',width:"150px",'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:"100px",'class': 'uitd_showTip'},
            {title: '联系地址', field: 'address', 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'},
            {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_training+"',null)\" >" +
                        "<i class=\"fa fa-book\"></i>日志" +
                        "</span>";
                }
            }
    ];
};

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

/**
 * 点击添加培训补贴
 */
TrainingSubsidy.openAddTrainingSubsidy = function () {
    var ajax = new $ax(Feng.ctxPath + "/api/commonBatch/valiateIsAdd", function (data) {
        if(data.code==200){
            var index = layer.open({
                type: 2,
                title: '添加培训补贴',
                area: ['800px', '420px'], //宽高
                fix: false, //不固定
                maxmin: true,
                content: Feng.ctxPath + '/api/trainingSubsidy/trainingSubsidy_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.TrainingSubsidyInfoDlg.addSubmit();
                },btn2: function(index, layero){
                    var obj = layero.find("iframe")[0].contentWindow;
                    obj.TrainingSubsidyInfoDlg.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');
                }
            });
            TrainingSubsidy.layerIndex = index;
            layer.full(index);
        }else{
            Feng.info(data.msg);
        }
    }, function (data) {
        Feng.error("校验失败!" + data.responseJSON.message + "!");
    });
    ajax.set("type",CONFIG.project_training);
    ajax.start();
};

/**
 * 打开修改培训补贴详情
 */
TrainingSubsidy.openTrainingSubsidyEdit = 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/trainingSubsidy/trainingSubsidy_update/' + TrainingSubsidy.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.TrainingSubsidyInfoDlg.editSubmit(1);
                    },btn2: function(index, layero){
                        var obj = layero.find("iframe")[0].contentWindow;
                        obj.TrainingSubsidyInfoDlg.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');
                    }
                });
                layer.full(index);
                TrainingSubsidy.layerIndex = index;
            }else{
                Feng.info(data.msg);
            }
        }, function (data) {
            Feng.error("校验失败!" + data.responseJSON.message + "!");
        });
        ajax.set("type",CONFIG.project_training);
        ajax.set("year",TrainingSubsidy.seItem.year);
        ajax.start();
    }
};

/**
 * 打开查看培训补贴详情
 */
TrainingSubsidy.openTrainingSubsidySelect = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '培训补贴详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/api/trainingSubsidy/trainingSubsidy_update/' + TrainingSubsidy.seItem.id
        });
        TrainingSubsidy.layerIndex = index;
        layer.full(index);
    }
};

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

/**
 * 收集数据
 */
TrainingSubsidy.formParams = function(){
    var queryData = {};
    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['checkState'] = $("#checkState").val();
    return queryData;
}

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

/**
 * 查询培训补贴列表
 */
TrainingSubsidy.search = function () {
    TrainingSubsidy.table.refresh({query: TrainingSubsidy.formParams()});
};

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