let layer, laydate, table;
$(function () {
    layui.use(['layer', 'laydate', 'table'], function () {
        layer = layui.layer;
        laydate = layui.laydate;
        table = layui.table;
        initTable();
    });

});

function initTable() {
    //渲染表格
    table.render({
        elem: '#baseTable'
        , url: ctxPath + '/backstage/teamEvaluate/getTeamEvaluateList' //数据接口
        , method: 'get' //方式默认是get
        , toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
        , where: {
            operType: 3,
        } //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: '退场前所属项目'}
            , {
                fixed: 'right', title: '评价状态', align: 'center', templet: d => {
                    return setEvStatus(d);
                }
            },
            {field: 'manager', align: 'center', title: '项目经理'},
            {field: 'sendPhone', align: 'center', title: '项目经理手机号'},
            {field: 'level', align: 'center', title: '评价结果'}
            , {
                fixed: 'right', width: 180, title: '操作', align: 'center', templet: d => {
                    let text = "";
                    if (d.evStatus === '待评价' || d.checkStatus === '评价驳回') {
                        text += '<a lay-event="notification" style="color: #009688;cursor: pointer;font-size: 15px"' +
                            '  id="notification">通知</a>';
                    } else if (d.evStatus === '已评价') {
                        text += '<a lay-event="evaluateTable" style="color: #009688;cursor: pointer;font-size: 15px"' +
                            '  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 === 'notification') {
             console.log(data)
            notification(data);
        }
        if (layEvent === 'evaluateTable') {
            evaluateTable(1, data);
        }
    });
}


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

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

// 设置评价状态
function setEvStatus(obj) {
    if (obj.checkStatus === '评价驳回') {
        return '评价驳回';
    } else {
        return obj.evStatus;
    }
}

function rowFunction(obj) {
    let h = "";
    h += "<td style='vertical-align:middle;' class='center'>";
    h += "<div class='visible-md visible-lg hidden-sm hidden-xs btn-group'>";
    if (obj.checkStatus === '评价驳回') {
        h += "<a href='#' title='重新评价' onclick='evaluateTable(2," + JSON.stringify(obj) + ")' class='aBtnNoTD' ><i class='icon-edit color-p bigger-140'></i></a>";
    } else {
        if (obj.evStatus === '待评价') {
            h += "<a href='#' title='评价' onclick='evaluateTable(1," + JSON.stringify(obj) + ")' class='aBtnNoTD' ><i class='icon-edit color-p bigger-140'></i></a>";
        } else if (obj.evStatus === '已评价') {
            h += "<a href='#' title='查看评价' onclick='evaluateTable(3," + JSON.stringify(obj) + ")' class='aBtnNoTD' ><i class='icon-zoom-in color-p bigger-140'></i></a>";
        }
    }
    h += "</div>";
    h += "</td>";
    return h;
}

// 导出
function exportData() {
    let keyWord = $("#keyWord").val();
    let evStatus = $("#evStatus").val();
    let token = localStorage.getItem("token")
    let params = "?keyWord=" + keyWord + "&evStatus=" + evStatus + '&operType=' + 3 + '&token=' + token;
    $(location).attr("href", ctxPath + '/backstage/teamEvaluate/exportExcelData' + params);
}

// 预览评价
function evaluateTable(type, obj) {

    obj.type = type;
    localStorage.setItem("downEvaluateInfo", JSON.stringify(obj));
    let layerIndex = layer.open({
        type: 2,
        title: '预览评价',
        shade: 0,
        area: ['80%', '90%'],
        scrollbar: true,
        move: false,
        anim: 2,
        yes: function (index, layero) {
        },
        content: './viewEvaluateTable.html',
    });
}

// 发送通知
function notification(obj) {
    let layerIndex = layer.confirm('<h5>确定发送通知吗！</h5>', {title: '操作提示', move: false}, function () {
        layer.close(layerIndex);
        notificationAjax(obj);
    });
}

function notificationAjax(obj) {
    let loadingMsg = layer.msg('正在发送通知,请稍等...', {icon: 16, shade: 0.01, time: '0'});
    $.ajax({
        url: `${ctxPath}` + '/backstage/teamEvaluate/notification',
        type: 'POST',
        data: JSON.stringify({
            phone:obj.sendPhone
        }),
        contentType: "application/json",
        dataType: 'json',
        success: function (result) {
            layer.close(loadingMsg); // 关闭提示层
            console.error(result)
            if (result.res === 1) {
                layer.msg(result.resMsg, {icon: 1});
            } else {
                layer.msg(result.resMsg, {icon: 2});
            }
        },
        error: function (result) {
            layer.close(loadingMsg); // 关闭提示层
            layer.msg('服务异常，请稍后重试', {icon: 16, scrollbar: false, time: 2000});
        }
    });
}