var oldYear;
var oldKeyWord;
var oldStatus;
var laydate;
var upload;
var token = localStorage.getItem("token");
layui.use(['table', 'layer', 'laydate', 'jquery', 'form', 'upload'], function () {
    var table = layui.table;
    laydate = layui.laydate;
    upload = layui.upload;
    let form = layui.form;
    //渲染table
    table.render({
        elem: '#demo'
        , url: ctxPath + '/career/getMsgContent' //数据接口
        , method: 'post' //方式默认是get
        , toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
        , where: {
            physicalTime: $("#physicalTime").val()
        } //post请求必须加where ，post请求需要的参数
        , cellMinWidth: 80
        , cols: [[ //表头
            {field: 'number', width:120,title: '序号', align: 'center', type: 'numbers'}
            , {field: 'personName',width: 120, align: 'center', title: '姓名'}
            , {field: 'sex', width: 80, align: 'center', title: '性别'}
            , {field: 'age', width: 80, align: 'center', title: '年龄'}
            ,{field: 'idNumber', align: 'center',width : 220,title: '身份证号码',templet:function(res){
                    return res.idNumber + "\t";
                }}
            , {field: 'unit', width: 200, align: 'center', title: '部门'}
            , {field: 'postName', width: 150, align: 'center', title: '岗位'}
            , {field: 'conclusion',  align: 'center', title: '结论'}
            ,{field: 'status', width : 100,title: '状态',templet:function(res){
                    switch (res.status) {
                        case '正常':
                            return '<span style="color:green;">正常</span>';
                        case '异常':
                            return '<span style="color:red;">异常</span>';
                        case '调岗':
                            return '<span style="color:blue;">调岗</span>';
                        case '复查':
                            return '<span style="color:orange;">复检</span>';
                        default:
                            return '<span>'+''+'</span>';
                    }
                }}
            // , {field: 'personType', width: 150, align: 'center', title: '体检时间'}
            // , {field: 'physicalFree', width: 150, align: 'center', title: '体检次数'}
            ,{field: 'counts', width : 150,title: '体检次数',templet:function(res){
                if (isNull(res.counts)){
                    return "";
                }else{
                    return '<span style="color:blue;cursor: pointer" onclick="openViewForm(\''+ res.personName +'\',\''+ $("#physicalTime").val() +'\')">'+res.counts+'</span>';
                }
                }}
            , {fixed: 'right', title: '操作', width: 200, align: 'center', toolbar: '#toolsBar'}
        ]]
        , id: 'menuTable'
        , page: true //开启分页
        , loading: true //数据加载中。。。
        , limits: [5, 10, 20]  //一页选择显示3,5或10条数据
        , limit: 10  //一页显示5条数据
        , response: {
            statusCode: 200 //规定成功的状态码，默认：0
        }, parseData: function (res) { //将原始数据解析成 table 组件所规定的数据，res为从url中get到的数据
            let result;
            if (res.data !== '' && res.data != null && res.data !== "null") {
                if (this.page.curr) {
                    result = res.data.slice(this.limit * (this.page.curr - 1), this.limit * this.page.curr);
                } else {
                    result = res.data.slice(0, this.limit);
                }
            }
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.count, //解析数据长度
                "data": result, //解析数据列表
            };
        },

        toolbar: "#toolbar"
    });

    // 行工具事件
    table.on('tool(test)', function (obj) {
        var data = obj.data //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值
        var id = data.id
        var statusId = data.status;
        if (layEvent === 'edit') {
            openForm(id,statusId,'修改');
        }
    });

    //头监听事件 -- checkBox 选中行和选中条数
    table.on('toolbar(test)', function (obj) {
        // 获取当前表格被选中的记录对象，返回数据
        var checkStatus = table.checkStatus(obj.config.id);
        //获取事件名，执行对应的代码
        var eventName = obj.event;
        switch (eventName) {
            case "searchBt":
                document.getElementById("searchBt").click();//按钮自动点击一次
                oldYear = $("#physicalTime").val();
                oldStatus = $("#status").val();
                oldKeyWord = $("#keyWord").val();
                table.reload('menuTable', {
                    url: ctxPath + '/career/getMsgContent'
                    , method: 'post' //方式默认是get
                    , page: true
                    , where: {
                        physicalTime: oldYear,
                        keyWord: oldKeyWord,
                        status: oldStatus
                    } //设定异步数据接口的额外参数
                });
                /**二次赋值-- 点击搜索按钮之后会进行一次刷新操作*/
                initTime();
                $("#physicalTime").val(oldYear);
                // $("#status").find('option[value=\'' + oldStatus + '\']').prop("selected", "selected");
                // form.render();
                $("#status").val(oldStatus);
                $("#keyWord").val(oldKeyWord);
                break;
            case "importBtn":
                importExcel();
                break;
            case "exportBtn":
                exportExcel();
                break;
            case "download":
                download();
                break;
        }
    });
    initTime();
});

function openForm(id,statusId,type){
    localStorage.setItem("id",id);
    localStorage.setItem("statusId",statusId);
    debugger;
    layerOpenForm("状态修改","./HealthReviewForm.html","60%","60%")
}

function openViewForm(name,year){
    localStorage.setItem("name",name);
    localStorage.setItem("year",year);
    layerOpenView("体检详情","./HealthExaminationReviewView.html","95%","90%")
}


function importExcel(){
    layerOpenViewNoBtn("上传数据", "./uploadExcel.html", "60%", "60%");
}

function initTime(){
    laydate.render({
        elem: '#physicalTime',
        type: 'year',
        value: getNowTime().substr(0,4),
    });
}

function exportExcel() {
    oldYear = $("#physicalTime").val();
    oldStatus = $("#status").val();
    oldKeyWord = $("#keyWord").val();
    window.location.href = ctxPath + "/export/careerExportReview?physicalTime=" + oldYear + "&status=" + oldStatus + "&keyWord=" + oldKeyWord + "&token=" + token;
}

function download(){
    window.location.href = ctxPath + "/download/download?filename=职业健康体检结果.xlsx"
}