/**
 * 人才认定申报管理初始化
 */
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', checkbox:true},
        {title: '申报年度', field: 'year', visible: true, align: 'center', valign: 'middle',width:'80px'},
        {title: '企业名称', field: 'enterpriseName', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"120px"},
        {title: '姓名', field: 'name', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"120px",
            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: 'talentArrangeName', visible: true, align: 'center', valign: 'middle',width:"100px"},
        {title: '人才标签', field: 'talentTypeName', visible: isShow, align: 'center', valign: 'middle',width:"120px",'class': 'uitd_showTip'},
        {title: '证件号码', field: 'idCard', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"150px"},
        {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:"80px"},
        {title: '首次提交时间', field: 'firstSubmitTime', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"100px"},
        {title: '最新提交时间', field: 'newSubmitTime', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"100px"},
        {title: '审核状态', field: 'checkState', visible: true, align: 'center', valign: 'middle',width:"100px",
            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==7){
                    if(row.highProcess!=null && row.highProcess!='' && row.highProcess>=1){
                        return "<span class='label label-success'>重新提交</span>"
                    }else{
                        return "<span class='label label-success'>待审核</span>"
                    }
                }if(value==20){
                    if(row.highProcess!=null && row.highProcess!='' && row.highProcess>=1){
                        return "<span class='label label-success'>上级驳回</span>"
                    }else{
                        return "<span class='label label-success'>待审核</span>"
                    }
                }if(value==10){
                    return "<span class='label label-danger'>已驳回</span>"
                }if(value == 15 ||value>=25){
                    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=\"TalentInfo.showLog('"+value+"')\" >" +
                    "<i class=\"fa fa-book\"></i>日志" +
                    "</span>";
            }
        }
    ];
};
/**
 * 检查是否选中
 */
TalentInfo.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length != 1){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        TalentInfo.seItem = selected[0];
        return true;
    }
};


TalentInfo.openCheckTalentInfo = function (){
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '人才认定-初级审核',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/talentInfo/talentInfo_toCommonCheck/' + TalentInfo.seItem.id + '/1',
            btn: ['<i class="fa fa-eye"></i>&nbsp;&nbsp;保存未提交','<i class="fa fa-save"></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.TalentInfoInfoDlg.showFirstCheckModal();
            },btn2: function(index, layero){
                var obj = layero.find("iframe")[0].contentWindow;
                obj.TalentInfoInfoDlg.submitCheck();
                return false;
            }
        });
        layer.full(index);
        TalentInfo.layerIndex = index;
    }
}



/**
 * 打开查看人才认定-初级审核详情
 */
TalentInfo.openTalentInfoDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '人才认定申报详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/talentInfo/talentInfo_toDetail/' + TalentInfo.seItem.id+'/1'
        });
        layer.full(index);
        TalentInfo.layerIndex = index;
    }
};

/**
 * 修改驳回的字段及附件
 */
TalentInfo.updateFieldsAndFiles = function(){
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/talentInfo/findFieldsAndFiles?id="+TalentInfo.seItem.id, function (data) {
            var obj = data.obj.obj;
            if(data.code==200){
                layer.open({
                    type: 1,
                    id: "neewFieldFormModel",
                    title: '修改',
                    area: ['800px', '450px'], //宽高
                    fix: false, //不固定
                    shade: 0,
                    maxmin: true,
                    content: TalentInfo.creatFieldCheckModal(),
                    btn: ['<i class="fa fa-save"></i>&nbsp;&nbsp;提交', '<i class="fa fa-eraser"></i>&nbsp;&nbsp;关闭'],
                    btnAlign: 'c',
                    zIndex: layer.zIndex,
                    success: function (layero, index) {
                        var fileList = data.obj.fileList;
                        var html = '';
                        for(var key in fileList){
                            html = html + '<ul><li style="width: 100%"><input type="checkbox" value="'+fileList[key].id+'"><span>'+fileList[key].name+'</span></li></ul>';
                        }
                        $("#field_file").empty().append(html);
                        if(obj.fields!=null && obj.fields!=''){
                            $("#field_info input").each(function () {
                                var arr = obj.fields.split(",");
                                for(var key in arr){
                                    if($(this).val()==arr[key]){
                                        this.checked = true;
                                    }
                                }
                            });
                        }
                        if(obj.files!=null && obj.files!=''){
                            $("#field_file input").each(function () {
                                if(obj.files.indexOf($(this).val())!=-1){
                                    this.checked=true;
                                }
                            });
                        }
                    },
                    yes: function (index, layero) {
                        TalentInfo.submitFieldsAndFiles(index,obj.id);
                    }
                });
            }else{
                Feng.error(data.msg);
            }
        }, function (data) {
            Feng.error("查询失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    }
}

/**
 * 修改提交
 * @param index
 * @param id
 */
TalentInfo.submitFieldsAndFiles = function(index,id){
    var fields = '';
    var files = '';
    $("#field_info li input").each(function(index){
        if($(this).is(":checked")){
            fields = fields + $(this).val() + ",";
        }
    });
    $("#field_file li input").each(function(index){
        if($(this).is(":checked")){
            files = files + $(this).val() + ",";
        }
    });
    if( fields=='' && files==''){
        Feng.info("请选择可修改的字段或附件！");
        return ;
    }
    var ajax = new $ax(Feng.ctxPath + "/talentInfo/updateFieldsAndFiles", function (data) {
        if(data.code==200){
            layer.close(index);
            Feng.success(data.msg);
        }else{
            Feng.error(data.msg);
        }
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.setData({"id":id,"fields":fields,"files":files})
    ajax.start();
}

/**
 * 审核不通过
 */
TalentInfo.setNotPass = function(){
    var selecteds = $('#' + this.id).bootstrapTable('getSelections');
    if(selecteds.length == 0){
        Feng.info("请选择需要设置审核不通过的行");
        return ;
    }
    var ids = "";
    for(var key in selecteds){
        ids = ids + selecteds[key].id + ",";
    }
    ids = ids.substring(0,ids.length-1);
    layer.open({
        type: 1,
        id: "notPassModal",
        title: '修改',
        area: ['800px', '450px'], //宽高
        fix: false, //不固定
        shade: 0,
        maxmin: true,
        content: '<form id="checkNotPass">\n' +
            '                    <div class="form-group" style="margin: 10px;">\n' +
            '                        <label for="checkMsgNotPass" class="control-label" >审核不通过原因</label>\n' +
            '                        <textarea class="form-control" id="checkMsgNotPass" placeholder="此功能适用于未在申报提交截止时间内提交的数据"  rows="6"></textarea>\n' +
            '                    </div>\n' +
            '                </form>',

        btn: ['<i class="fa fa-save"></i>&nbsp;&nbsp;提交', '<i class="fa fa-eraser"></i>&nbsp;&nbsp;关闭'],
        btnAlign: 'c',
        zIndex: layer.zIndex,
        yes: function (index, layero) {
            var checkMsg = $("#checkMsgNotPass").val();
            if(Feng.isEmptyStr(checkMsg)){
                Feng.info("请填写审核不通过原因");
                return ;
            }
            var operation = function(){
                var ajax = new $ax(Feng.ctxPath + "/talentInfo/setNotPass", function (data) {
                    if(data.code==200){
                        Feng.success(data.msg);
                        TalentInfo.table.refresh();
                        layer.close(index);
                    }else{
                        Feng.error(data.msg);
                    }
                }, function (data) {
                    Feng.error("设置审核不通过失败!" + data.responseJSON.message + "!");
                });
                ajax.set("ids",ids);
                ajax.set("msg",checkMsg);
                ajax.start();
            }
            Feng.confirm("一旦提交无法修改，确定设置所选数据为审核不通过?", operation);
        }
    });

}


TalentInfo.creatFieldCheckModal = function () {
    return '<form id="firstCheckForm">\n' +
        '                    <div class="form-group" style="margin: 10px;">\n' +
        '                        <div >\n' +
        '                            <label for="checkMsg" class="control-label">可修改字段</label>\n' +
        '                            <div id="field_info">\n' +
        '                                <ul>\n' +
        '                                    <li style="width:10%"><input type="checkbox" value="name"><span>姓名</span></li>\n' +
        '                                    <li style="width:10%"><input type="checkbox" value="sex"><span>性别</span></li>\n' +
        '                                    <li style="width:10%"><input type="checkbox" value="birthday"><span>出生日期</span></li>\n' +
        '                                    <li style="width:10%"><input type="checkbox" value="nationality"><span>国籍/地区</span></li>\n' +
        '                                    <li style="width:10%"><input type="checkbox" value="provinceCode" onchange="TalentInfo.fieldCheckd(this)"><span>籍贯省</span></li>\n' +
        '                                    <li style="width:10%"><input type="checkbox" value="cityCode" onchange="TalentInfo.fieldCheckd(this)"><span>籍贯市</span></li>\n' +
        '                                    <li style="width:10%"><input type="checkbox" value="countyCode"><span>籍贯县</span></li>\n' +
        '                                    <li style="width:10%"><input type="checkbox" value="nation"><span>民族</span></li>\n' +
        '                                    <li style="width:10%"><input type="checkbox" value="politics"><span>政治面貌</span></li>\n' +
        '                                    <li style="width:10%"><input type="checkbox" value="cardType"><span>证件类型</span></li>\n' +
        '                                    <li style="width:10%"><input type="checkbox" value="idCard"><span>证件号码</span></li>\n' +
        '                                    <li style="width:20.5%"><input type="checkbox" value="firstInJJTime"><span>首次来晋工作时间</span></li>\n' +
        '                                    <li style="width:10%"><input type="checkbox" value="source" onchange="TalentInfo.sourceCheckd(this)"><span>申报来源</span></li>\n' +
        '                                    <li style="width:20.5%"><input type="checkbox" value="ourCitySource"><span>公布入选来源</span></li>\n' +
        '                                    <li style="width:20.5%"><input type="checkbox" value="fromCity"><span>入选来源县市</span></li>\n' +
        '                                    <li style="width:20.5%"><input type="checkbox" value="qzBatch"><span>入选名单的文件号及批次</span></li>\n' +
        '                                    <li style="width:31%"><input type="checkbox" value="certificateStartTime"><span>泉州高层次人才证书发证日期</span></li>\n' +
        '                                    <li style="width:31%"><input type="checkbox" value="qzgccrcActiveTime"><span>泉州高层次人才证书的有效期</span></li>\n' +
        '                                    <li style="width:10%"><input type="checkbox" id="talentArrangeCheckBox" value="talentArrange" onchange="TalentInfo.fieldCheckd(this)"><span>人才层次</span></li>\n' +
        '                                    <li style="width:10%"><input type="checkbox" value="identifyCondition"><span>认定条件</span></li>\n' +
        '                                    <li style="width:20.5%"><input type="checkbox" value="identifyConditionName"><span>认定条件名称</span></li>\n' +
        '                                    <li style="width:20.5%"><input type="checkbox" value="identifyGetTime"><span>认定条件证书取得时间</span></li>\n' +
        '                                    <li style="width:10%"><input type="checkbox" value="talentType"><span>人才标签</span></li>\n' +
        '                                    <li style="width:31%"><input type="checkbox" value="letterTime"><span>首次来晋行政介绍信时间</span></li>\n' +
        '                                    <li style="width:10%"><input type="checkbox" value="introductionMode"><span>引进方式</span></li>\n' +
        '                                    <li style="width:20.5%"><input type="checkbox" value="entryTime"><span>本单位入职时间</span></li>\n' +
        '                                    <li style="width:10%"><input type="checkbox" value="post"><span>职务</span></li>\n' +
        '                                    <li style="width:20.5%"><input type="checkbox" value="startTime"><span>工作合同开始时间</span></li>\n' +
        '                                    <li style="width:20.5%"><input type="checkbox" value="endTime"><span>工作合同结束时间</span></li>\n' +
        '                                    <li style="width:20.5%"><input type="checkbox" value="lastYearWages"><span>上一年度年薪</span></li>\n' +
        '                                    <li style="width:10%"><input type="checkbox" value="highEducation"><span>最高学历</span></li>\n' +
        '                                    <li style="width:10%"><input type="checkbox" value="graduateSchool"><span>毕业院校</span></li>\n' +
        '                                    <li style="width:10%"><input type="checkbox" value="major"><span>专业</span></li>\n' +
        '                                    <li style="width:10%"><input type="checkbox" value="title"><span>职称</span></li>\n' +
        '                                    <li style="width:20.5%"><input type="checkbox" value="professionalQualifications"><span>国家职业资格</span></li>\n' +
        '                                    <li style="width:20.5%"><input type="checkbox" value="studyAbroad"><span>是否有留学经历</span></li>\n' +
        '                                    <li style="width:10%"><input type="checkbox" value="phone"><span>手机号码</span></li>\n' +
        '                                    <li style="width:10%"><input type="checkbox" value="email"><span>电子邮箱</span></li>\n' +
        '                                    <li style="width:10%"><input type="checkbox" value="bank"><span>开户银行</span></li>\n' +
        '                                    <li style="width:20.5%"><input type="checkbox" value="bankNetwork"><span>开户银行网点</span></li>\n' +
        '                                    <li style="width:10%"><input type="checkbox" value="bankNumber"><span>银行行号</span></li>\n' +
        '                                    <li style="width:10%"><input type="checkbox" value="bankAccount"><span>银行账号</span></li>\n' +
        '                                    <li style="width:31%"><input type="checkbox" value="breakFaith"><span>曾被相关主管部门列为失信个人</span></li>\n' +
        '                                    <li style="width:20.5%"><input type="checkbox" value="educationAndResume"><span>教育背景及工作简历</span></li>\n' +
        '                                    <li style="width:20.5%"><input type="checkbox" value="mainHonours"><span>主要业绩及取得的荣誉</span></li>\n' +
        '                                </ul>\n' +
        '                            </div>\n' +
        '                            <label for="checkMsg" class="control-label">可修改附件</label>\n' +
        '                            <div id="field_file">\n' +
        '                            </div>\n' +
        '                            <div class="form-group" style="text-align: center">\n' +
        '                                <button type="button" class="btn btn-primary" onclick="TalentInfo.checkAll()">全选</button>\n' +
        '                                <button type="button" class="btn btn-success" onclick="TalentInfo.unCheckAll()">反选</button>\n' +
        '                            </div>\n' +
        '                        </div>\n' +
        '                    </div>\n' +
        '                </form>';
}

TalentInfo.fieldCheckd = function(context){
    if($(context).get(0).checked){
        $(context).parent().next().children()[0].checked=true;
        $(context).parent().next().children().eq(0).trigger("change");
    }
}

TalentInfo.sourceCheckd = function(context){
    if($(context).get(0).checked){
        $("#talentArrangeCheckBox").attr("checked",true);
        $("#talentArrangeCheckBox").trigger("change");
    }
}

TalentInfo.getPhones = function(){
    var ajax = new $ax(Feng.ctxPath + "/talentInfo/getPhones", function(data){
        if(data.code == 200){
            layer.open({
                type: 1,
                title:"手机号码",
                area: ['830px', '300px'], //宽高
                fix: false, //不固定
                maxmin: true,
                content: "<span style='word-break:break-all'>"+data.obj+"</span>"
            });
        }else{
            Feng.info(data.msg);
        }
    },function(data){
        Feng.error("操作失败!");
    });
    ajax.setData(TalentInfo.formParams());
    ajax.start();
}


TalentInfo.getEnterprisePhones = function(){
    var ajax = new $ax(Feng.ctxPath + "/talentInfo/getEnterprisePhones", function(data){
        if(data.code == 200){
            layer.open({
                type: 1,
                title:"手机号码",
                area: ['830px', '300px'], //宽高
                fix: false, //不固定
                maxmin: true,
                content: "<span style='word-break:break-all'>"+data.obj+"</span>"
            });
        }else{
            Feng.info(data.msg);
        }
    },function(data){
        Feng.error("操作失败!");
    });
    ajax.setData(TalentInfo.formParams());
    ajax.start();
}

$(function () {
    var defaultColunms = TalentInfo.initColumn();
    var table = new BSTable(TalentInfo.id, "/talentInfo/list/1", defaultColunms);
    table.setPaginationType("server");
    table.setSingleSelect(false);
    table.setOnDblClickRow(function () {
        TalentInfo.openCheckTalentInfo();
    });
    TalentInfo.table = table.init();
    TalentInfo.init();

    // var defaultColunms = TalentInfo.initColumn();
    // var table = new KDTable(TalentInfo.id, "/talentInfo/list/1", defaultColunms);
    // table.setPaginationType("server");
    // TalentInfo.table = table.init();
});
