/**
 * 子女就学管理初始化
 */
var EducationSchool = {
    id: "EducationSchoolTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
EducationSchool.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '申报年度', field: 'year', visible: true, align: 'center', valign: 'middle',width:"80px",'class': 'uitd_showTip'},
        {title: '申报项目', field: 'projectName', visible: true, align: 'center', valign: 'middle',width:"80px",'class': 'uitd_showTip'},
        {title: '父母姓名', field: 'pName', visible: true, align: 'center', valign: 'middle',width:"80px",'class': 'uitd_showTip'},
        {title: '父母性别', field: 'pSex', visible: true, align: 'center', valign: 'middle',width:"80px",'class': 'uitd_showTip',
            formatter : function (value,row,index) {
                if(value==1){
                    return '男';
                }if(value==2){
                    return '女';
                }
            }
        },
        {title: '父母证件号码', field: 'pIdcard', visible: true, align: 'center', valign: 'middle',width:"150px",'class': 'uitd_showTip'},
        {title: '工作单位', field: 'enterpriseName', visible: true, align: 'center', valign: 'middle',width:"120px",'class': 'uitd_showTip'},
        {title: '人才层次', field: 'talentArrangeName', visible: true, align: 'center', valign: 'middle',width:"80px",'class': 'uitd_showTip'},
        {title: '认定条件', field: 'identifyCondition', visible: true, align: 'center', valign: 'middle',width:"120px",'class': 'uitd_showTip'},
        {title: '人才证书有效期', field: 'certificateNo', visible: true, align: 'center', valign: 'middle',width:"140px",'class': 'uitd_showTip',
            formatter : function (value,row,index) {
                return row.certificateStartTime + "至" + row.qzgccrcActiveTime;
            }
        },
        {title: '联系电话', field: 'phone', visible: true, align: 'center', valign: 'middle',width:"80px",'class': 'uitd_showTip'},
        {title: '子女姓名', field: 'cName', visible: true, align: 'center', valign: 'middle',width:"100px",'class': 'uitd_showTip'},
        {title: '子女性别', field: 'cSex', visible: true, align: 'center', valign: 'middle',width:"80px",
            formatter : function (value,row,index) {
                if(value==1){
                    return '男';
                }if(value==2){
                    return '女';
                }
            }
        },
        {title: '与申报人关系', field: 'cRelationName', visible: true, align: 'center', valign: 'middle',width:"100px",'class': 'uitd_showTip'},
        {title: '现就读学校', field: 'nowSchool', visible: true, align: 'center', valign: 'middle',width:"100px",'class': 'uitd_showTip'},
        {title: '现就读年级', field: 'nowGradeName', visible: true, align: 'center', valign: 'middle',width:"100px",'class': 'uitd_showTip'},
        {title: '拟申请学校', field: 'applySchoolName', visible: true, align: 'center', valign: 'middle',width:"100px",'class': 'uitd_showTip'},
        {title: '审核状态', field: 'checkState', visible: true, align: 'center', valign: 'middle',width:"100px",
            formatter : function (value,row,index) {
                if(value==-2){
                    return '<span class="label label-default">待提交</span>';
                }if(value==-1){
                    return '<span class="label label-danger">审核不通过</span>';
                }if(value==1){
                    return '<span class="label label-success">待审核</span>';
                }if(value==2){
                    return '<span class="label label-danger">审核驳回</span>';
                }if(value==3){
                    return '<span class="label label-primary">审核通过</span>';
                }if(value==9){
                    return '<span class="label label-warning">重新提交</span>';
                }
            }
        },
        {title: '操作', field: 'id', visible: true, align: 'center', valign: 'middle',width:"80px",
            formatter : function (value,row,index){
                return "<span class='label label-success' onclick=\"EducationSchool.showLog('"+value+"')\" >" +
                    "<i class=\"fa fa-book\"></i>日志" +
                    "</span>";
            }
        }
    ];
};

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

/**
 * 点击添加子女就学
 */
EducationSchool.openAddEducationSchool = function () {
    var ajax = new $ax(Feng.ctxPath + "/api/educationSchool/valiateIsAdd", function (data) {
        if (data.code == 200) {
            var index = layer.open({
                type: 2,
                title: '子女就学申报',
                area: 'auto', //宽高
                fix: false, //不固定
                maxmin: true,
                content: Feng.ctxPath + '/api/educationSchool/educationSchool_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.EducationSchoolInfoDlg.addSubmit();
                },btn2: function(index, layero){
                    var obj = layero.find("iframe")[0].contentWindow;
                    obj.EducationSchoolInfoDlg.submitToCheck();
                    return false;
                },
                success :function (layero, index) {
                    layer.tips('添加基本信息并上传附件后点击','.layui-layer-btn1',{tips:[1,"#78BA32"],time:0,closeBtn :2});
                },
                end :function () {
                    layer.closeAll('tips');
                }
            });
            EducationSchool.layerIndex = index;
            layer.full(index);
        }else{
            Feng.info(data.msg);
        }
    }, function (data) {
        Feng.error("校验失败!" + data.responseJSON.message + "!");
    });
    // ajax.set("type",CONFIG.batch_school);
    ajax.start();
};

/**
 * 打开查看子女就学详情
 */
EducationSchool.openEducationSchoolDetail = 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: 'auto', //宽高
                    fix: false, //不固定
                    maxmin: true,
                    content: Feng.ctxPath + '/api/educationSchool/educationSchool_update/' + EducationSchool.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.EducationSchoolInfoDlg.addSubmit();
                    },btn2: function(index, layero){
                        var obj = layero.find("iframe")[0].contentWindow;
                        obj.EducationSchoolInfoDlg.submitToCheck();
                        return false;
                    },
                    success :function (layero, index) {
                        layer.tips('添加基本信息并上传附件后点击','.layui-layer-btn1',{tips:[1,"#78BA32"],time:0,closeBtn :2});
                    },
                    end :function () {
                        layer.closeAll('tips');
                    }
                });
                EducationSchool.layerIndex = index;
                layer.full(index);
            }else{
                Feng.info(data.msg);
            }
        }, function (data) {
            Feng.error("校验失败!" + data.responseJSON.message + "!");
        });
        ajax.set("type",CONFIG.project_school);
        ajax.set("year",EducationSchool.seItem.year)
        ajax.start();
    }
};

EducationSchool.openEducationSchoolSelect = function(){
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '子女择校申报详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/api/educationSchool/educationSchool_select/' + EducationSchool.seItem.id,
            btn: ['<i class="fa fa-eraser"></i>&nbsp;&nbsp;取消'],
            btnAlign: 'c',
        });
        layer.full(index);
        EducationSchool.layerIndex = index;
    }
}

/**
 * 删除子女就学
 */
EducationSchool.delete = function () {
    if (this.check()) {
        if(EducationSchool.seItem.checkState != -2){
            Feng.error("仅能删除未提交的数据");
            return  ;
        }
        var operation = function() {
            var ajax = new $ax(Feng.ctxPath + "/api/educationSchool/delete", function (data) {
                if(data.code == 200){
                    Feng.success(data.msg);
                    EducationSchool.table.refresh();
                }else{
                    Feng.info(data.msg);
                }
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("educationSchoolId",EducationSchool.seItem.id);
            ajax.start();
        }
        Feng.confirm("删除后无法恢复，确认删除吗?", operation);
    }
};

/**
 *
 */
EducationSchool.print = function(){
    if (this.check()) {
        if(EducationSchool.seItem.checkState != 3){
            Feng.error("暂未审核通过，无法打印");
            return  ;
        }
        window.open("/webroot/decision/view/report?viewlet=educationSchool.cpt&&id="+EducationSchool.seItem.id);
    }
}

/**
 * 收集数据
 */
EducationSchool.formParams = function(){
    var queryData = {};
    queryData['year'] = $("#year").val();
    queryData['enterpriseName'] = $("#enterpriseName").val();
    queryData['pName'] = $("#pName").val();
    queryData['pSex'] = $("#pSex").val();
    queryData['pIdcard'] = $("#pIdcard").val();
    queryData['talentArrange'] = $("#talentArrange").val();
    queryData['certificateNo'] = $("#certificateNo").val();
    queryData['address'] = $("#address").val();
    queryData['phone'] = $("#phone").val();
    queryData['cName'] = $("#cName").val();
    queryData['cSex'] = $("#cSex").val();
    queryData['cIdcard'] = $("#cIdcard").val();
    queryData['cRelation'] = $("#cRelation").val();
    queryData['nowSchool'] = $("#nowSchool").val();
    queryData['nowGrade'] = $("#nowGrade").val();
    queryData['applySchool'] = $("#applySchool").val();
    queryData['companyStreet'] = $("#companyStreet").val();
    queryData['houseStreet'] = $("#houseStreet").val();
    queryData['checkState'] = $("#checkState").val();
    queryData['project'] = $("#project").val();
    return queryData;
}

/**
 * 重置
 */
EducationSchool.reset = function(){
    $("#year").val("");
    $("#enterpriseName").val("");
    $("#pName").val("");
    $("#pSex").val("");
    $("#pIdcard").val("");
    $("#talentArrange").val("");
    $("#certificateNo").val("");
    $("#address").val("");
    $("#phone").val("");
    $("#cName").val("");
    $("#cSex").val("");
    $("#cIdcard").val("");
    $("#cRelation").val("");
    $("#nowSchool").val("");
    $("#nowGrade").val("");
    $("#applySchool").val("").trigger("chosen:updated");
    $("#companyStreet").val("");
    $("#houseStreet").val("");
    $("#checkState").val("");
    $("#project").val("");
}

/**
 * 查询子女就学列表
 */
EducationSchool.search = function () {
    EducationSchool.table.refresh({query: EducationSchool.formParams()});
};

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


$(function () {
    var defaultColunms = EducationSchool.initColumn();
    var table = new BSTable(EducationSchool.id, "/api/educationSchool/list", defaultColunms);
    table.setPaginationType("server");
    table.setOnDblClickRow(function () {
        EducationSchool.openEducationSchoolDetail();
    });
    EducationSchool.table = table.init();
    //批量加载字典表数据
    var arr = [
        {"name":"companyStreet","code":"un_street"},
        {"name":"houseStreet","code":"un_street"},
        {"name":"talentArrange","code":"un_talentLevel"},
        {"name":"cRelation","code":"un_education_relation"},
        {"name":"nowGrade","code":"un_grade"},
        {"name":"applySchool","code":"un_school_pool"}];
    Feng.findChildDictBatch(JSON.stringify(arr));
    $("#applySchool").on('chosen:ready', function(e, params) {
        $(".chosen-container-single .chosen-single").css("padding","4px 0px 0px 4px");
    });
    $("#applySchool").chosen({
        search_contains:true,       //关键字模糊搜索。设置为true，只要选项包含搜索词就会显示；设置为false，则要求从选项开头开始匹配
        disable_search: false,
        width:"100%",
        enable_split_word_search: true
    });
});
