/**
 * 优秀人才津补贴管理初始化
 */
var TalentAllowanceInfo = {
    id: "TalentAllowanceInfoTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
TalentAllowanceInfo.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '年度', field: 'year', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"60px"},
        {title: '单位名称', field: 'enterpriseName', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"120px"},
        // {title: '泉州证书有效期', field: 'qzgccrcActiveTime', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"150px"},
        // {title: '人才标签', field: 'talentTypeName', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"100px"},
        {title: '所属镇街', field: 'addressName', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"100px"},
        {title: '姓名', field: 'name', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"100px"},
        {title: '性别', field: 'sex', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"60px",
            formatter(value,row,index){
                if(value==1){
                    return "男";
                }
                if(value==2){
                    return "女";
                }
            }
        },
        {title: '证件号码', field: 'idCard', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"120px"},
        {title: '人才层次', field: 'talentArrangeName', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"100px"},
        {title: '认定条件', field: 'identifyConditionText', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"120px"},
        {title: '认定条件证书取得时间', field: 'identifyGetTime', visible: true, align: 'center', valign: 'middle',width:"150px"},
        {title: '认定条件名称', field: 'identifyConditionName', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"100px"},
        {title: '公布入选月份', field: 'identifyMonth', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"100px"},
        {title: '津补贴类型', field: 'allowanceType', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"120px",
            formatter(value,row,index){
                if(value==null || value=="")return "未判定";
                if(value==1)return "人才津贴";
                if(value==2)return "一次性交通补贴";
                if(value==3)return "不予兑现";
            }
        },
        {title: '兑现月份', field: 'months', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"150px"},
        {title: '兑现金额', field: 'money', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"80px"},
        {title: '金额说明', field: 'moneyDesc', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"80px"},
        {title: '审核状态', field: 'checkState', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"100px",
            formatter(value,row,index){
                var html = "";
                switch (value) {
                    case 1:
                        html = "<span class='label'>待提交</span>"
                        break;
                    case 5:
                    case 13:
                    case 15:
                    case 20:
                    case 25:
                    case 35:
                        html = "<span class='label label-success'>审核中</span>";
                        break;
                    case 10:
                        html = "<span class='label label-danger'>已驳回</span>"
                        break;
                    case -1:
                        if(row.publicState >= 3){
                            html = "<span class='label label-danger'>审核不通过</span>";
                        }else{
                            html = "<span class='label label-success'>审核中</span>";
                        }
                        break;
                    case 30:
                        if(row.publicState < 3){
                            html = "<span class='label label-success'>审核中</span>"
                        }
                        if(row.publicState == 3){
                            html = "<span class='label label-primary'>公示中</span>"
                        }else if(row.publicState == 4){
                            html = row.allowanceType != 3? "<span class='label label-success'>待兑现</span>":"<span class='label label-danger'>不予兑现</span>";
                        }else if(row.publicState == 5){
                            html = "<span class='label label-primary'>已兑现</span>"
                        }
                        break;
                }
                return html;
            }
        },
        {title: '操作', field: 'id', visible: true, align: 'center', valign: 'middle',width:"80px",
            formatter : function (value,row,index){
                return "<span class='label label-success' onclick=\"TalentAllowanceInfo.showLog('"+value+"')\" >" +
                    "<i class=\"fa fa-book\"></i>日志" +
                    "</span>";
            }
        }
    ];
};

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

/**
 * 点击添加优秀人才津补贴
 */
TalentAllowanceInfo.openAddTalentAllowanceInfo = function () {
    var ajax = new $ax(Feng.ctxPath + "/api/commonBatch/valiateIsAdd", function (data) {
        if(data.code==200){
            var index = layer.open({
                type: 2,
                title: '津补贴申报',
                fix: false, //不固定
                maxmin: true,
                content: Feng.ctxPath + '/api/talentAllowance/talentAllowanceInfo_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.TalentAllowanceInfoDlg.addSubmit();
                },btn2: function(index, layero){
                    var obj = layero.find("iframe")[0].contentWindow;
                    obj.TalentAllowanceInfoDlg.submitToCheck();
                    return false;
                },
                success :function (layero, index) {
                    layer.tips('添加基本信息并上传附件后点击','.layui-layer-btn1',{tips:[1,"#78BA32"],time:0,closeBtn :2});
                },
                end :function () {
                    layer.closeAll('tips');
                    TalentAllowanceInfo.table.refresh();
                }
            });
            layer.full(index);
            TalentAllowanceInfo.layerIndex = index;
        }else{
            Feng.error(data.msg);
        }
    }, function (data) {
        Feng.error("查询失败!" + data.responseJSON.message + "!");
    });
    ajax.set("type",CONFIG.project_jbt);
    ajax.start();
};

/**
 * 打开查看优秀人才津补贴详情
 */
TalentAllowanceInfo.openTalentAllowanceInfoDetail = 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: '津补贴申报',
                    fix: false, //不固定
                    maxmin: true,
                    content: Feng.ctxPath + '/api/talentAllowance/talentAllowanceInfo_update/'+TalentAllowanceInfo.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.TalentAllowanceInfoDlg.editSubmit();
                    },
                    btn2: function(index, layero){
                        var obj = layero.find("iframe")[0].contentWindow;
                        obj.TalentAllowanceInfoDlg.submitToCheck();
                        return false;
                    },
                    success :function (layero, index) {
                        layer.tips('添加基本信息并上传附件后点击','.layui-layer-btn1',{tips:[1,"#78BA32"],time:0,closeBtn :2});
                    },
                    end :function () {
                        layer.closeAll('tips');
                        TalentAllowanceInfo.table.refresh();
                    }
                });
                layer.full(index);
                TalentAllowanceInfo.layerIndex = index;
            }else{
                Feng.info(data.msg);
            }
        }, function (data) {
            Feng.error("校验失败!" + data.responseJSON.message + "!");
        });
        ajax.set("type",CONFIG.project_jbt);
        ajax.set("year",TalentAllowanceInfo.seItem.year);
        ajax.start();
    }
};

/**
 * 公示期补录材料
 */
TalentAllowanceInfo.openTalentAllowanceInfoSupple = function(){
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/api/commonBatch/valiateIsSupple", function (data) {
            if (data.code == 200) {
                var index = layer.open({
                    type: 2,
                    title: '津补贴补件',
                    fix: false, //不固定
                    maxmin: true,
                    content: Feng.ctxPath + '/api/talentAllowance/talentAllowanceInfoSupple/'+TalentAllowanceInfo.seItem.id,
                    btn: ['<i class="fa fa-eraser"></i>&nbsp;&nbsp;取消'],
                    btnAlign: 'c',

                });
                layer.full(index);
                TalentAllowanceInfo.layerIndex = index;
            }else{
                Feng.info(data.msg);
            }
        }, function (data) {
            Feng.error("校验失败!" + data.responseJSON.message + "!");
        });
        ajax.set("type",CONFIG.project_jbt);
        ajax.set("year",TalentAllowanceInfo.seItem.year);
        ajax.start();
    }
}


TalentAllowanceInfo.openTalentAllowanceInfoSelect = function(){
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '津补贴查看',
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/api/talentAllowance/talentAllowanceInfoSelect/'+TalentAllowanceInfo.seItem.id,
            btn: ['<i class="fa fa-eraser"></i>&nbsp;&nbsp;取消'],
            btnAlign: 'c',

        });
        layer.full(index);
        TalentAllowanceInfo.layerIndex = index;
    }
}


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

/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
TalentAllowanceInfo.formParams = function() {
    var queryData = {};
    queryData['year'] = $("#year").val();
    queryData['enterpriseName'] = $("#enterpriseName").val();
    queryData['name'] = $("#name").val();
    queryData['idCard'] = $("#idCard").val();
    queryData['talentType'] = $("#talentType").val();
    queryData['talentArrange'] = $("#talentArrange").val();
    queryData['address'] = $("#address").val();
    queryData['identifyCondition'] = $("#identifyCondition").val();
    return queryData;
}

/**
 * 查询人才认定申报列表
 */
TalentAllowanceInfo.search = function () {
    TalentAllowanceInfo.table.refresh({query: TalentAllowanceInfo.formParams()});
};

/**
 * 重置
 */
TalentAllowanceInfo.reset = function (){
    $("#year").val("");
    $("#enterpriseName").val("");
    $("#name").val("");
    $("#idCard").val("");
    $("#talentType").val("");
    $("#talentArrange").val("");
    $("#address").val("");
    $("#identifyCondition").val("");
}

/**
 * 获取人才认定
 */
TalentAllowanceInfo.getIdentifyCondition = function() {
    var level = $("#talentArrange").val();
    if(level==null||level==''){
        $("#identifyCondition").empty();
        $("#identifyCondition").trigger('chosen:updated');
        return;
    }
    Feng.addAjaxSelect({
        "id": "identifyCondition",
        "displayCode": "id",
        "displayName": "name",
        "type": "GET",
        "url": Feng.ctxPath + "/api/common/findIdentifyConditionByLevel?talentLevel="+level+"&type=1"
    });
    $("#identifyCondition").trigger('chosen:updated');
}


/**
 * 显示审核日志
 */
TalentAllowanceInfo.showLog = function (id){
    layer.open({
        type: 1,
        title:"日志",
        fixed:false,
        content: '<table id="'+id+'"></table>',
        area: ['80%', '80%'],
        maxmin: true,
        success :function (layero, index) {
            $('#'+id).bootstrapTable({
                url: Feng.ctxPath + "/api/talentInfo/getCheckLog",
                method: 'POST',
                contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                search: false,					// 是否显示表格搜索，此搜索是客户端搜索，不会进服务端
                showRefresh: false,				// 是否显示刷新按钮
                clickToSelect: true,			// 是否启用点击选中行
                singleSelect: true,				// 设置True 将禁止多选
                striped: true,  				// 是否显示行间隔色
                pagination: false,   			// 设置为 true 会在表格底部显示分页条
                paginationHAlign: "left",
                paginationDetailHAlign: "right",
                sidePagination: "server",   	// 设置在哪里进行分页，可选值为 'client' 或者 'server'
                showColumns: false,
                queryParams: function (params) {
                    return {"type":CONFIG.project_jbt,"mainId":id,"typeFileId":"","active":1}
                },
                rowStyle : function(row,index){
                    return {
                        css: {
                            "word-break": "break-word",
                            "white-space": "inherit"
                        }
                    }
                },
                columns:
                    [
                        {title: '步骤', field: 'stepName', visible: true, align: 'center', valign: 'middle',width:"10%",
                            formatter : function (value,row,index) {
                                return ""+value;
                            }
                        },
                        {title: '操作人', field: 'createUser', visible: false, align: 'center', valign: 'middle',width:"15%"},
                        {title: '操作时间', field: 'createTime', visible: true, align: 'center', valign: 'middle',width:"20%"},
                        {title: '描述', field: 'description', visible: true, align: 'center', valign: 'middle',width:"65%",
                            formatter : function (value,row,index) {
                                return '<span data-toggle="tooltip" title="'+ value +'">"'+value+'"</span>';
                            }
                        }
                    ]
                ,
                onPostBody: function () {
                    $('#'+id+"td.uitd_showTip").bind("mouseover", function () {
                        var htm = $(this).html();
                        $(this).webuiPopover({title: '详情', content: htm, trigger: 'hover'}).webuiPopover('show');
                    });
                }
            });
        }
    });
}

$(function () {
    var defaultColunms = TalentAllowanceInfo.initColumn();
    var table = new BSTable(TalentAllowanceInfo.id, "/api/talentAllowance/list", defaultColunms);
    table.setPaginationType("server");
    table.setOnDblClickRow(function () {
        TalentAllowanceInfo.openTalentAllowanceInfoDetail();
    });
    TalentAllowanceInfo.table = table.init();
    //批量加载字典表数据
    var arr = [
        {"name":"address","code":"un_street"},
        {"name":"talentArrange","code":"un_talentLevel"},
        {"name":"nationality","code":"un_nationality"},
        {"name":"talentType","code":"un_jbt_talentType"}
    ];
    Feng.findChildDictBatch(JSON.stringify(arr));
    $("#identifyCondition").chosen({
        search_contains:true,       //关键字模糊搜索。设置为true，只要选项包含搜索词就会显示；设置为false，则要求从选项开头开始匹配
        disable_search: false,
        width:"100%",
        enable_split_word_search: true
    });
});
