/**
 * 人才类别变更管理初始化
 */
var TalentTypeChange = {
    id: "TalentTypeChangeTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
TalentTypeChange.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '原申报年度', field: 'oldYear', visible: true, align: 'center', valign: 'middle',width:'80px'},
        {title: '工作单位', field: 'enterpriseName', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"100px"},
        {title: '姓名', field: 'talentName', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"100px"},
        {title: '原人才层次', field: 'oldTalentArrangeName', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"100px"},
        {title: '原认定条件', field: 'oldIdentifyConditionCH', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"130px"},
        {title: '原认定条件名称', field: 'oldIdentifyConditionName', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"140px"},
        {title: '原证书有效期', field: 'oldCertificateStartTime', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"140px",
            formatter : function (value,row,index) {
                return row.oldCertificateStartTime + "至" + row.oldCertificateOutTime
            }
        },
        {title: '原公布入选月份', field: 'oldIdentifyMonth', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"100px"},
        {title: '新申报年度', field: 'newYear', visible: true, align: 'center', valign: 'middle',width:'80px'},
        {title: '新人才层次', field: 'newTalentArrangeName', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"100px"},
        {title: '新认定条件', field: 'newIdentifyConditionCH', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"140px"},
        // {title: '新认定条件证书取得时间', field: 'newIdentifyGetTime', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"140px"},
        {title: '新认定条件名称', field: 'newIdentifyConditionName', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"130px"},
        // {title: '新公布入选月份', field: 'newIdentifyMonth', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:"100px"},
        {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','class': 'uitd_showTip',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=\"TalentTypeChange.showLog('"+value+"')\" >" +
                    "<i class=\"fa fa-book\"></i>日志" +
                    "</span>";
            }
        }
    ];
};

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

TalentTypeChange.updateFieldsAndFiles = function(){
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/talentTypeChange/findFieldsAndFiles?id="+TalentTypeChange.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: TalentTypeChange.creatFieldCheckModal(data.obj.obj),
                    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) {
                        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 + "/talentTypeChange/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":TalentTypeChange.seItem.id,"fields":fields,"files":files})
                        ajax.start();
                    }
                });
            }else{
                Feng.error(data.msg);
            }
        }, function (data) {
            Feng.error("查询失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    }
}


TalentTypeChange.creatFieldCheckModal = function(obj){
    var field = obj.type==1?'<li style="width: 33%"><input type="checkbox" value="newSource"><span>新申报来源</span></li>\n' +
        '                                    <li style="width: 33%"><input type="checkbox" value="newOurCitySource"><span>新公布入选来源</span></li>\n' +
        '                                    <li style="width: 33%"><input type="checkbox" value="newFromCity"><span>新入选来源县市</span></li>\n' +
        '                                    <li style="width: 33%"><input type="checkbox" value="newQzBath"><span>新入选名单批次</span></li>\n' +
        '                                    <li style="width: 33%"><input type="checkbox" value="newTalentArrange"><span>新人才层次</span></li>\n' +
        '                                    <li style="width: 33%"><input type="checkbox" value="newIdentifyCondition"><span>新认定条件</span></li>\n' +
        '                                    <li style="width: 33%"><input type="checkbox" value="newIdentifyGetTime"><span>新认定条件证书取得时间</span></li>\n' +
        '                                    <li style="width: 33%"><input type="checkbox" value="newIdentifyConditionName"><span>新认定条件名称</span></li>\n' +
        '                                    <li style="width: 33%"><input type="checkbox" value="newCertificateStartTime"><span>新泉州高层次人才证书发证日期</span></li>\n' +
        '                                    <li style="width: 33%"><input type="checkbox" value="newCertificateOutTime"><span>新泉州高层次人才证书的有效期</span></li>\n' +
        '                                    <li style="width: 33%"><input type="checkbox" value="newIntroductionMode"><span>新引进方式</span></li>\n' +
        '                                    <li style="width: 33%"><input type="checkbox" value="newGygb"><span>新是否为我市本级国有股比超过50</span></li>\n' +
        '                                    <li style="width: 33%"><input type="checkbox" value="newLetterTime"><span>新首次来晋行政介绍信时间</span></li>':
        '                                    <li style="width: 33%"><input type="checkbox" value="newTalentArrange"><span>新人才层次</span></li>\n' +
        '                                    <li style="width: 33%"><input type="checkbox" value="newIdentifyCondition"><span>新认定条件</span></li>\n' +
        '                                    <li style="width: 33%"><input type="checkbox" value="newIdentifyGetTime"><span>新认定条件证书取得时间</span></li>\n' +
        '                                    <li style="width: 33%"><input type="checkbox" value="newIdentifyConditionName"><span>新认定条件名称</span></li>';
    return '<form id="firstCheckForm">\n' +
        '                    <div class="form-group" style="margin: 10px;">\n' +
        '                        <div id="field">\n' +
        '                            <label for="checkMsg" class="control-label">可修改字段</label>\n' +
        '                            <div id="field_info">\n' +
        '                                <ul>\n' + field+
        '                                </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="Feng.checkAll("field")">全选</button>\n' +
        '                                <button type="button" class="btn btn-success" onclick="Feng.unCheckAll("field")">反选</button>\n' +
        '                            </div>\n' +
        '                        </div>\n' +
        '                    </div>\n' +
        '                </form>';
}

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

/**
 * 打开查看人才类别变更详情
 */
TalentTypeChange.openTalentTypeChangeDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '人才层次变更详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/talentTypeChange/talentTypeChange_detail/' + TalentTypeChange.seItem.id,
        });
        this.layerIndex = index;
        layer.full(index);
    }
};


TalentTypeChange.getPhones = function(){
    var ajax = new $ax(Feng.ctxPath + "/talentTypeChange/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(TalentTypeChange.formParams());
    ajax.start();
}


TalentTypeChange.getEnterprisePhones = function(){
    var ajax = new $ax(Feng.ctxPath + "/talentTypeChange/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(TalentTypeChange.formParams());
    ajax.start();
}


$(function () {
    var defaultColunms = TalentTypeChange.initColumn();
    var table = new BSTable(TalentTypeChange.id, "/talentTypeChange/list/1", defaultColunms);
    table.setPaginationType("server");
    // table.setHeight(665);
    table.setOnDblClickRow(function () {
        TalentTypeChange.openTalentTypeChangeCheck();
    });
    TalentTypeChange.table = table.init();
    TalentTypeChange.init();
});
