layui.config({
    base: '../../assets/js/' //你存放新模块的目录，注意，不是layui的模块目录
}).use('treeTable', function () {
    var treeTable = layui.treeTable;

    var insTb = treeTable.render({
        elem: '#treeTableId',
        toolbar: '#leftBar',
        defaultToolbar: ['filter', 'print', 'exports'],
        tree: {
            iconIndex: 2,           // 折叠图标显示在第几列
            isPidData: true,        // 是否是id、pid形式数据
            idName: 'id',  // id字段名称
            pidName: 'parentId'     // pid字段名称
        },
        cols: [[
            {type: 'numbers'},
            {type: 'checkbox'},
            {field: 'authName', title: '模块名称',width : "20%"},
            {field: 'authCode', title: '模块编码'},
            {
                field: 'authType', title: '模块类型', minWidth: 100, align: "center", templet: function (d) {
                    if (d.authType == "0") {
                        return "父节点";
                    } else if (d.userGender == "1") {
                        return "子节点";
                    } else {
                        return "异常节点";
                    }
                }
            },
            {field: 'permissionName', title: '权限名称', minWidth: 100, align: "center"},
            {field: 'permission', title: '权限标识', minWidth: 100, align: "center"},
            {field: 'linkUrl', title: '访问地址', minWidth: 100, align: "center"},
            {field: 'creator', title: '创建人', minWidth: 100, align: "center"},
            {
                field: 'createTime', title: '创建时间', minWidth: 100, align: "center", templet: function (d) {
                    return showTime(d.createTime);
                }
            },
            {
                field: 'updateTime', title: '更新时间', align: 'center', minWidth: 150, templet: function (d) {
                    return showTime(d.updateTime);
                }
            },
            {align: 'center', toolbar: '#tbBar', title: '操作'}

        ]],
        reqData: function (data, callback) {
            // 在这里写ajax请求，通过callback方法回调数据
            $.get('/upms/permission/getTreeTable', function (res) {
                if (res.code == 0) callback(res.data);
                else callback(res.msg);
            });
        }
    });

    $(".add_btn").click(function () {
        update();
    })

    //数据维护
    function update(obj) {
        let id = 0;
        if(obj != null){
            id = obj.data.id;
        }
        var index = layui.layer.open({
            title: "添加",
            type: 2,
            content: "/upms/permission/update/"+id,
            success: function (layero, index) {
                setTimeout(function () {
                    layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                }, 500)
            }
        })
        layui.layer.full(index);
        window.sessionStorage.setItem("index", index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize", function () {
            layui.layer.full(window.sessionStorage.getItem("index"));
        })
    }

    // 工具列点击事件
    treeTable.on('tool(treeTableId)', function (obj) {
        var event = obj.event;
        if (event === 'del') {
            deleteInfo(obj.data.id);
        } else if (event === 'edit') {
            update(obj);
        }
    });

    //批量删除
    $(".delAll_btn").click(function () {
        deleteInfo();
    })
    function deleteInfo(id){
        var  ids = [];
        checkStatus = insTb.checkStatus(false);
        if(id != null && id != ""){
            ids.push(id);
            layer.confirm('确定删除选中的条目？', {icon: 3, title: '提示信息'}, function (index) {
                $.ajax({
                    url: "/upms/permission/delete/" + ids,
                    type: "POST",
                    dataType: "json",
                    async: false,
                    contentType: 'application/json;charset=UTF-8',
                    success: function (res) {
                        insTb.refresh();
                        layer.close(index);
                    },
                    error: function (e) {
                        insTb.refresh();
                        layer.close(index);
                    }
                })
            })
        }else{
            if (checkStatus.length > 0) {
                for (var i in checkStatus) {
                    ids.push(checkStatus[i].id);
                }
                layer.confirm('确定删除选中的条目？', {icon: 3, title: '提示信息'}, function (index) {
                    $.ajax({
                        url: "/upms/permission/delete/" + ids,
                        type: "POST",
                        dataType: "json",
                        async: false,
                        contentType: 'application/json;charset=UTF-8',
                        success: function (res) {
                            insTb.refresh();
                            layer.close(index);
                        },
                        error: function (e) {
                            insTb.refresh();
                            layer.close(index);
                        }
                    })
                })
            } else {
                layer.msg("请选择需要删除的条目");
            }
        }
    }

    //搜索
    $(".search_btn").click(function () {
        var keyword = $('#keyword').val();
        // 搜索
        insTb.filterData(keyword);   // 根据关键字模糊搜索
    })

    $(".expandAll_btn").click(function () {
        insTb.expandAll();  // 展开全部
    })

    $(".foldAll_btn").click(function () {
        insTb.foldAll();  // 折叠全部
    })

    $(".refresh").click(function () {
        //insTb.clearFilter();
        insTb.refresh();  // 刷新(异步模式)
    })

    //时间转换函数
    function showTime(tempDate) {
        var d = new Date(tempDate);
        var year = d.getFullYear();
        var month = d.getMonth();
        month++;
        var day = d.getDate();
        var hours = d.getHours();

        var minutes = d.getMinutes();
        var seconds = d.getSeconds();
        month = month < 10 ? "0" + month : month;
        day = day < 10 ? "0" + day : day;
        hours = hours < 10 ? "0" + hours : hours;
        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;

        var time = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
        return time;
    }

});