<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="keywords" content="">
    <meta name="description" content="">
    <title text="权限管理"></title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css">
</head>
<body class="pear-container">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <label class="layui-form-label">权限名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="name" placeholder="" class="layui-input">
                    </div>
                    <button class="pear-btn pear-btn-md-0 pear-btn-primary" lay-submit lay-filter="btn-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md-0">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </form>
        </div>
    </div>
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="table" lay-filter="table"></table>
        </div>
    </div>
<script type="text/html" id="toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="pear-icon pear-icon-add"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-success pear-btn-md" lay-event="expand">
        <i class="layui-icon layui-icon-spread-left"></i>
        展开
    </button>
    <button class="pear-btn pear-btn-success pear-btn-md" lay-event="fold">
        <i class="layui-icon layui-icon-shrink-right"></i>
        折叠
    </button>
</script>
<script type="text/html" id="bar">
    <a class="cool-line-btn cool-bg-blue" lay-event="edit">编辑</a>
    <div class="cool-line-divider"></div>
    {{#  if (d.enable == 0) { }}
    <a class="cool-line-btn cool-bg-blue" lay-event="switch">启用</a>
    {{#  } else { }}
    <a class="cool-line-btn cool-bg-blue" lay-event="switch">停用</a>
    {{#  } }}
</script>
<script type="text/html" id="type">
    {{#if (d.type == '0') { }}
    <span>目录</span>
    {{# }else if(d.type == '1'){ }}
    <span>菜单</span>
    {{# }else if(d.type == '2'){ }}
    <span>按钮</span>
    {{# } }}
</script>
<script type="text/html" id="enable">
    {{#  if (d.enable == 0) { }}
    <span style="color: #f56c6c;">禁用</span>
    {{#  } else { }}
    <span>正常</span>
    {{#  } }}
</script>
<script type="text/html" id="icon">
    <i class="{{d.icon}}"></i>
</script>
<script src="../../../component/layui/layui.js"></script>
<script src="../../../component/pear/pear.js"></script>
<script>
    layui.use(['request', 'form', 'table', 'treetable', 'popup'], function () {
        var $ = layui.jquery,
            request = layui.request,
            form = layui.form,
            table = layui.table,
            treetable = layui.treetable,
            popup = layui.popup;

        var MODULE_PATH = "/system/menu/v1/";

        var cols = [
            [
                {type: 'checkbox'},
                {field: 'title', title: '权限名称', minWidth: 200},
                {field: 'icon', title: '权限图标', align: 'center', templet: '#icon'},
                {field: 'type', title: '权限类型', align: 'center', templet: '#type'},
                {field: 'code', title: '权限标识'},
                {field: 'enable', title: '权限状态', align: 'center', templet: '#enable'},
                {field: 'sort', title: '排序', align: 'center'},
                {title: '操作', width: 220, align: 'center', templet: '#bar'}
            ]
        ];

        window.render = function() {
            treetable.render({
                elem: '#table',
                url: MODULE_PATH,
                skin: 'line',
                page: false,
                treeColIndex: 1,
                treeSpid: 0,
                treeIdName: 'id',
                treePidName: 'parentId',
                treeDefaultClose: true,
                toolbar: '#toolbar',
                cols: cols
            });
        };

        // 表格树渲染，这里多包装一层是利于子页面调用重新渲染
        render();

        table.on('tool(table)', function (obj) {
            if (obj.event === 'edit') {f.edit(obj);
            }
            else if (obj.event === 'switch') {f.switch(obj);
            }
        });

        table.on('toolbar(table)', function (obj) {
            if (obj.event === 'add') {f.add();
            }
            else if (obj.event === 'refresh') {f.refresh();
            }
            else if (obj.event === 'expand') {treetable.expandAll('#table');
            }
            else if (obj.event === 'fold') {treetable.foldAll('#table');
            }
        });

        var f = {
            add: function() {
                layer.open({
                    type: 2,
                    title: '新增',
                    shade: 0.1,
                    area: ['450px', '500px'],
                    content: ['add.html', 'no']
                });
            },
            edit: function(obj) {
                layer.open({
                    type: 2,
                    title: '编辑',
                    shade: 0.1,
                    area: ['450px', '500px'],
                    content: ['add.html?id=' + obj.data.id, 'no']
                });
            },
            switch: function(obj) {
                var status = (obj.data.enable === 1 ? '禁用' : '启用') + obj.data.title;
                var warn = '确定要' + status +'权限?';
                layer.confirm(warn, {icon: 0, title: '提示'}, function(index) {
                    request.http({
                        url: MODULE_PATH + "switch",
                        type: 'put',
                        data: {id: obj.data.id, type: (obj.data.enable === 1 ? 0 : 1)},
                        success: function (result) {
                            if (result.success) {
                                popup.success(result.msg, function () {
                                    obj.del();
                                });
                            } else {
                                popup.failure(result.msg);
                            }
                        }
                    });
                });
            }
        };

        form.on('submit(btn-query)', function (data) {
            var keyword = data.field.name;
            var $tds = $('#table').next('.treeTable').find('.layui-table-body tbody tr td');
            if (!keyword) {
                $tds.css('background-color', 'transparent');
                layer.msg("请输入关键字", {icon: 5});
                return false;
            }
            var searchCount = 0;
            $tds.each(function() {
                $(this).css('background-color', 'transparent');
                if ($(this).text().indexOf(keyword) >= 0) {
                    $(this).css('background-color', 'rgba(250,230,160,0.5)');
                    if (searchCount === 0) {
                        $('body,html').stop(true);
                        $('body,html').animate({scrollTop: $(this).offset().top - 150}, 500);
                    }
                    searchCount++;
                }
            });
            if (searchCount === 0) {
                layer.msg("没有匹配结果", {icon: 5});
                return false;
            } else {
                treetable.expandAll('#table');
            }
            return false;
        });
    });
</script>
</body>
</html>
