let layer, laydate, table;
let token = localStorage.getItem("token")

$(function () {
    layui.use(['layer', 'laydate', 'table'], function () {
        layer = layui.layer;
        laydate = layui.laydate;
        table = layui.table;
        laydate.render({
            elem: '#timeRange',
            range: ['#startDate', '#endDate'],
            rangeLinked: true // 开启日期范围选择时的区间联动标注模式 ---  2.8+ 新增
        });
        initTable();
    });
});

function search(type) {
    if (type === 1) {

    } else {
        $('#keyWord').val('')
        $('#evStatus').val('')
        $('#startDate').val('');
        $('#endDate').val('');
        $('#timeRange').val('');
        layui.form.render();
    }
    table.reload('baseTable', {
        url: ctxPath + '/backstage/teamEvaluate/getTeamEvaluateList'
        , method: 'get' //方式默认是get
        , page: true
        , where: {
            operType: 2,
            keyWord: $("#keyWord").val(),
            evStatus: $("#evStatus").val(),
            startDate: $("#startDate").val(),
            endDate: $("#endDate").val(),
        } //设定异步数据接口的额外参数
    });
}

function initTable() {
    let loginUser = JSON.parse(localStorage.getItem("loginUser"));
    //渲染表格
    table.render({
        elem: '#baseTable'
        , url: ctxPath + '/backstage/teamEvaluate/getTeamEvaluateList' //数据接口
        , method: 'get' //方式默认是get
        , toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
        , where: {
            operType: 2,
        } //post请求必须加where ，post请求需要的参数
        , cellMinWidth: 80
        , cols: [[ //表头
            {
                field: 'number', width: 80, title: '序号', align: 'center', type: 'numbers'
            }
            , {field: 'teamName', align: 'center', title: '班组名称'}
            , {field: 'userName', align: 'center', title: '班长'}
            , {field: 'phone', align: 'center', title: '班长联系方式'}
            , {field: 'subName', align: 'center', title: '退场前所属外包商'}
            , {field: 'proName', align: 'center', title: '退场前所属项目'}
            , {field: 'checkStatus', align: 'center', title: '审核状态'},
            {field: 'manager', align: 'center', title: '项目经理'},
            {field: 'sendPhone', align: 'center', title: '项目经理手机号'},
            {
                fixed: 'right', title: '评价时间', align: 'center', templet: d => {
                    return handleTime(d);
                }
            },
            {
                fixed: 'right', width: 180, title: '操作', align: 'center', templet: d => {
                    let text = "";
                    if (loginUser.roleId == '1' || loginUser.roleId.split(',').indexOf('1') !== -1) {

                    } else {
                        if (loginUser.roleId == '5' || loginUser.roleId.split(',').indexOf('5') !== -1) {
                            if (d.status == '1') {
                                if (d.checkStatus === '待审核') {
                                    text += '<a lay-event="examine" style="color: #009688;cursor: pointer;font-size: 15px"' +
                                        '  id="examine">审核</a>';
                                }
                            }
                        }
                        if (loginUser.roleId == '6' || loginUser.roleId.split(',').indexOf('6') !== -1) {
                            if (d.status != '1') {
                                if (d.checkStatus === '待审核') {
                                    text += '<a lay-event="examine" style="color: #009688;cursor: pointer;font-size: 15px"' +
                                        '  id="examine">审核</a>';
                                }
                            }
                        }
                    }
                    text += '<a lay-event="evaluateTable" style="color: #009688;cursor: pointer;font-size:' +
                        ' 15px;margin-left: 10px"' +
                        '  id="evaluateTable">查看评价</a>';
                    return text;
                }
            },
        ]]
        , id: 'baseTable'
        , page: true //开启分页

        , loading: true //数据加载中。。。
        , limits: [10, 20, 100]  //一页选择显示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 rowIndex = obj.index;
        var layEvent = obj.event; //当前点击的事件名
        if (layEvent === 'examine') {
            evaluateTable(1, data);
        }
        if (layEvent === 'evaluateTable') {
            evaluateTable(2, data);
        }
    });
}

// 处理评价时间
function handleTime(obj) {
    if (obj.evStatus === '待评价') {
        return '';
    } else {
        return obj.evaluateTime;
    }
}

// 导出
function exportData() {
    let keyWord = $("#keyWord").val();
    let evStatus = $("#evStatus").val();
    let startDate = $("#startDate").val();
    let endDate = $("#endDate").val();
    let params = "?keyWord=" + keyWord + "&evStatus=" + evStatus + "&startDate=" + startDate + "&endDate=" + endDate + '&operType=' + 2 + '&token=' + token;
    $(location).attr("href", ctxPath + '/backstage/teamEvaluate/exportExcelData' + params);
}
// 评价/预览评价/重新评价
function evaluateTable(type, obj) {
    let title = '', content = '';
    if (type === 1) {
        title = '审核评价';
        content = './reEvaluateTable.html';
    } else if (type === 2) {
        title = '预览评价';
        content = './viewEvaluateTable.html';
    }
    obj.type = type;
    localStorage.setItem("downEvaluateInfo",JSON.stringify(obj));
    let layerIndex = layer.open({
        type: 2,
        title: title,
        shade: 0,
        area: ['80%', '90%'],
        scrollbar: true,
        move: false,
        anim: 2,
        yes: function (index, layero) {
        },
        content: content,
        success: function (layero, index) {
            let iframeWin = window["layui-layer-iframe" + layerIndex];
            iframeWin.setParams(JSON.stringify(obj));
        },
    });
}