/**
 * 人才认定申报管理初始化
 */
var TalentInfo = {
    id: "TalentInfoTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
TalentInfo.initColumn = function () {
    var type = $("#type").val();
    var isShow = true;
    if(type==2){
        isShow = false;
    }
    return [
        {field: 'selectItem', radio: true},
        {title: '申报年度', field: 'year', visible: true, align: 'center', valign: 'middle',width:'80px'},
        {title: '离职状态', field: 'active', visible: true, align: 'center', valign: 'middle',width:'80px',
            formatter : function (value,row,index) {
                if(value==1){
                    return '<span style="color:#6495ED">在职</span>';
                }if(value==2){
                    return '<span style="color:#FF82AB">离职</span>';
                }
            }
        },
        {title: '姓名', field: 'name', visible: true, align: 'center', valign: 'middle',width:"100px",
            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: 'type', visible: true, align: 'center', valign: 'middle',width:"100px",
            formatter : function (value,row,index){
                if(value==1){
                    return '晋江市现代产业体系人才';
                }if(value==2){
                    return '集成电路优秀人才';
                }
            }
        },
        {title: '人才层次', field: 'talentArrangeName', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"80px"},
        {title: '人才标签', field: 'talentTypeName', visible: isShow, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"80px"},
        {title: '证件号码', field: 'idCard', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"120px"},
        {title: '认定条件', field: 'identifyConditionText', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"120px"},
        {title: '认定条件名称', field: 'identifyConditionName', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"120px"},
        {title: '认定条件证书取得时间', field: 'identifyGetTime', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"120px"},
        {title: '人才证书有效期', field: 'identifyGetTime', visible: isShow, align: 'center', valign: 'middle','class': 'uitd_showTip',width:'150px',
            formatter:function (value,row,index) {
                return row.certificateStartTime + "至" + row.qzgccrcActiveTime ;
            }
        },
        {title: '公布入选月份', field: 'identifyMonth', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"100px"},
        {title: '人才编号', field: 'certificateNO', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"100px"},
        {title: '操作', field: 'id', visible: true, align: 'center', valign: 'middle',width:"80px",
            formatter : function (value,row,index){
                return "<span class='label label-success' onclick=\"TalentInfo.showLog('"+value+"')\" >" +
                    "<i class=\"fa fa-book\"></i>日志" +
                    "</span>";
            }
        }
    ];
};

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

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

}

/**
 * 打开查看人才认定申报详情
 */
TalentInfo.openTalentInfoDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '人才认定申报详情',
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/api/talentInfo/talentInfo_update/' + TalentInfo.seItem.id+"/select",
        });
        layer.full(index);
        TalentInfo.layerIndex = index;
    }
};

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

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

/**
 * 重置
 */
TalentInfo.reset = function (){
    $("#name").val("");
    $("#idCard").val("");
    $("#sex").val("");
    $("#nation").val("");
    $("#nationality").val("");
    $("#talentType").val("");
    $("#talentArrange").val("");
}

//显示修改手机号码模态框
TalentInfo.updatePhoneModal = function(){
    if(this.check()){
        $("#phoneForm")[0].reset();
        $("#updateModal").modal("show");
    }
}

//修改手机号码提交
TalentInfo.updatePhone = function(){
    var oldPhone = $("#oldPhone").val();
    var newPhone = $("#newPhone").val();
    if($.trim(oldPhone)!=TalentInfo.seItem.phone){
        Feng.info("原手机号码不正确");
        return ;
    }
    if(newPhone==null || newPhone==''){
        Feng.info("新手机号码不能为空");
        return;
    }
    var operation = function() {
        var ajax = new $ax(Feng.ctxPath + "/api/talentInfo/updatePhone", function (data) {
            if(data.code==200){
                Feng.success(data.msg);
                TalentInfo.table.refresh();
                $("#updateModal").modal("hide");
            }else{
                Feng.error(data.msg);
            }
        }, function (data) {
            Feng.error("修改手机号码失败!" + data.responseJSON.message + "!");
        });
        ajax.set("id",TalentInfo.seItem.id);
        ajax.set("oldPhone", oldPhone);
        ajax.set("newPhone", newPhone);
        ajax.start();
    };
    Feng.confirm("确定修改手机号码吗？", operation);
}

TalentInfo.updatePhoto = function(){
    if(this.check()){
        if (TalentInfo.seItem.isCertification == 2){
            Feng.info("已制证，无法修改");
            return ;
        }
        var index = layer.open({
            type: 1,
            title: '修改头像',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: "<form id=\"commonFileForm\" action=\""+Feng.ctxPath+"/api/talentInfo/updatePhoto\" method=\"post\" enctype=\"multipart/form-data\" target=\"hiddenIframe\">\n" +
                "                    <div  class=\"panel panel-default\">\n" +
                "                        <div class=\"panel-heading\" style=\"font-weight: bold\">头像</div>\n" +
                "                        <div class=\"panel-body\">\n" +
                "                            <input type=\"text\" style=\"display: none\" id=\"libId\" name=\"libId\" >\n" +
                "                            <input type=\"text\" style=\"display: none\" id=\"index\" name=\"index\" >\n" +
                "                            <input type=\"file\" style=\"display: none\" id=\"fileUrl\" name=\"fileUrl\" onchange=\"TalentInfo.fileChange(this)\">\n" +
                "                            <input class=\"form-control\" placeholder=\"请上传头像\" id=\"fileInput\" onclick=\"$('#fileUrl').click()\">\n" +
                "                        </div>\n" +
                "                    </div>\n" +
                "                </form>",
            btn: ['<i class="fa fa-eye"></i>&nbsp;&nbsp;提交' ,'<i class="fa fa-eraser"></i>&nbsp;&nbsp;关闭'],
            btnAlign: 'c',
            yes: function (index, layero) {
                $("#index").val(index);
                $("#libId").val(TalentInfo.seItem.id);
                $("#commonFileForm")[0].submit();
            }
        });
    }
}

TalentInfo.fileChange = function(context){
    var file  = $(context).val();
    var pos = file.lastIndexOf("\\");
    $("#fileInput").val(file.substring(pos+1));
}

TalentInfo.callback = function(data){
    if(data.code == 200){
        Feng.success(data.msg);
        layer.close(data.obj);
    }else{
        Feng.info(data.msg);
    }
}


//显示导出模态框
TalentInfo.showExportModal = function(){
    $("#exportForm")[0].reset();
    $("#exportModal").modal("show");
}


//导出
TalentInfo.export = function(){
    var operation = function() {
        var names = '';
        var fieldCodes = '';
        $("#field_info li input").each(function(index){
            if($(this).is(":checked")){
                fieldCodes = fieldCodes + $(this).val() + ",";
                names = names + $(this).next().text() + ",";
            }
        });
        var arr = { "names":names,"fieldCodes":fieldCodes,"name":$("#name").val(), "idCard":$("#idCard").val(), "sex":$("#sex").val(), "nation":$("#nation").val(),
            "nationality":$("#nationality").val(),"talentType":$("#talentType").val(), "talentArrange":$("#talentArrange").val()};
        window.location.href = Feng.setUrlParam(Feng.ctxPath + "/api/talentInfoExport/export", arr);
        // window.location.href = Feng.ctxPath + "/api/talentInfoExport/export?names=" + names + "&fieldCodes=" + fieldCodes + "&name=" + $("#name").val() + "&idCard=" + $("#idCard").val()
        //     + "&sex=" + $("#sex").val()+ "&nation=" + $("#nation").val()+ "&nationality=" + $("#nationality").val()+ "&talentType=" + $("#talentType").val()+ "&talentArrange=" + $("#talentArrange").val();
    };
    Feng.confirm("确定要导出数据吗？", operation);
}

/**
 * 全选
 */
TalentInfo.checkAll = function(){
    $("#field_info input").each(function () {
        this.checked = true;
    })
}
/**
 * 反选
 */
TalentInfo.unCheckAll = function(){
    $("#field_info input").each(function () {
        if(this.checked){
            this.checked = false;
        }else{
            this.checked = true;
        }
    })
}

$(function () {
    var defaultColunms = TalentInfo.initColumn();
    var table = new BSTable(TalentInfo.id, "/api/talentInfo/list/2", defaultColunms);
    table.setPaginationType("server");
    table.setOnDblClickRow(function () {
        TalentInfo.openTalentInfoDetail();
    });
    TalentInfo.table = table.init();
    //批量加载字典表数据
    var arr = [
        {"name":"nation","code":"un_nation"},
        {"name":"talentArrange","code":"un_talentLevel"},
        {"name":"nationality","code":"un_nationality"},
        {"name":"talentType","code":"un_jbt_talentType"}];
    Feng.findChildDictBatch(JSON.stringify(arr));
});

