let layer, treeTable, tableIns, table;
const token = localStorage.getItem("token");

layui.use(['layer', 'treeTable', 'table'], function () {
    layer = layui.layer;
    treeTable = layui.treeTable;
    table = layui.table;
    initTreeTable()

});

// 搜索/重置
function searchTable(type) {
    if (type === 1) {

    } else if (type === 2) {
        $('#keyWord').val('')
    }
    table.reload('ID-treeTable-demo', {
        url: ctxPath + '/evaluatePerson/getPersonList',
        page: {
            curr: 1
        },
        where: {
            keyWord: $("#keyWord").val(),
        }
    });
}

// 初始化树表格
function initTreeTable() {
    tableIns = table.render({
        elem: '#ID-treeTable-demo',
        id: 'ID-treeTable-demo',
        url: `${ctxPath}` + '/evaluatePerson/getPersonList',
        method: 'get' //方式默认是get
        , toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
        , where: {} //post请求必须加where ，post请求需要的参数
        , cellMinWidth: 80,
        cols: [[
            { field: 'number', width: 80, title: '序号', align: 'center', type: 'numbers' },
            { field: 'name', title: '姓名', width: '10%', unresize: true, align: "center" },
            { field: 'sex', title: '性别', width: '10%', unresize: true, align: "center" },
            { field: 'idCard', title: '身份证号码', width: '20%', unresize: true, align: "center", templet: d => maskSensitiveInfo(d.idCard, 'idCard') },
            { field: 'phone', title: '联系电话', width: '15%', unresize: true, align: "center", templet: d => maskSensitiveInfo(d.phone, 'phone') },
            { field: 'deptName', title: '所属部门/项目', width: '15%', unresize: true, align: "center" },
            { field: 'roleName', title: '系统角色', width: '10%', unresize: true, align: "center" },
            {
                fixed: 'right', title: '操作', align: 'center', templet: d => {
                    let html = "";
                    html += '<a lay-event="edit" style="color: #009688;cursor: pointer;font-size: 15px" id="edit">修改</a>';
                    html += '<a lay-event="del" style="color: #009688;cursor: pointer;font-size: 15px;margin-left: 10px" id="del">删除</a>';
                    return html;
                }
            },
        ]]
        , 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 === 'edit') {
            edit(data);
        }
        if (layEvent === 'del') {
            del(data.id);
        }
    });
}

function del(data) {
    layer.confirm('确认删除吗？', function (index) {
        $.ajax({
            type: "POST",
            url: ctxPath + '/evaluatePerson/deletePersonById',
            data: {id: data},
            success: function (data) {
                layer.close(index);
                if (data.res === 1) {
                    layer.msg(data.resMsg, {icon: 1, time: 2000});
                    searchTable(1);
                } else {
                    layer.msg(data.resMsg, {icon: 2, time: 2000});
                }
            }
        });
    });
}

function add() {
    openPage('新增人员', './EvaluatePersonAdd.html?id=0&type=add');
}

function edit(data) {
    localStorage.setItem("personInfo", JSON.stringify(data));
    //如果是查看页面，不需要弹出层的确定按钮
    const layerOptions = {
        type: 2,
        title: '修改人员',
        content: './editPerson.html',
        maxmin: false,
        area: ['40%', '60%'],
        success: function (layero, index) {
        }
    };

    layerOptions.btn = ["确定"];
    layerOptions.yes = function (index, layero) {
        //提交子页面时执行
        // 获取弹出层中的form表单元素
        const formSubmit = layer.getChildFrame('form', index);
        // 查找class样式为submitBtn的按钮
        const submited = formSubmit.find('button.subBtn');
        // 触发点击事件，会对表单进行验证，验证成功则提交表单，失败则返回错误信息
        submited.click();
    };
    layer.open(layerOptions);
}

function openPage(title, url) {
    //如果是查看页面，不需要弹出层的确定按钮
    const layerOptions = {
        type: 2,
        title: title,
        content: url,
        maxmin: false,
        area: ['90%', '95%'],
        success: function (layero, index) {
        }
    };

    if (title === '查看人员') {
        layer.open(layerOptions);
    } else {
        layerOptions.btn = ["确定"];
        layerOptions.yes = function (index, layero) {
            //提交子页面时执行
            // 获取弹出层中的form表单元素
            const formSubmit = layer.getChildFrame('form', index);
            // 查找class样式为submitBtn的按钮
            const submited = formSubmit.find('button.subBtn');
            // 触发点击事件，会对表单进行验证，验证成功则提交表单，失败则返回错误信息
            submited.click();
        };
        layer.open(layerOptions);
    }
}