{include file="common/header"/}
<div class="lemo-container">
    <div class="lemo-main">
        <div class="admin-main layui-anim layui-anim-upbit">
            <fieldset class="layui-elem-field layui-field-title">
                <legend>权限{:lang('list')}</legend>
            </fieldset>
            <blockquote class="layui-elem-quote">
                <a data-href="{:url('ruleAdd')}" class="layui-btn layui-btn-sm add">{:lang('add')}路由</a>
                <a class="layui-btn layui-btn-normal layui-btn-sm" onclick="openAll();">展开或折叠全部</a>
            </blockquote>
            <table class="layui-table" id="list" lay-filter="list"></table>
        </div>
    </div>
</div>

<script type="text/html" id="auth">
    <input type="checkbox" name="auth_open" data-href="{:url('ruleState')}" value="{{d.id}}" lay-skin="switch" lay-text="是|否" lay-filter="status" {{ d.auth_open == 0 ? 'checked' : '' }}>
</script>
<script type="text/html" id="status">
    <input type="checkbox" name="menu_status" data-href="{:url('ruleState')}" value="{{d.id}}" lay-skin="switch" lay-text="显示|隐藏" lay-filter="status" {{ d.menu_status == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="order">
    <input name="{{d.id}}" data-id="{{d.id}}" class="list_order layui-input" value=" {{d.sort}}" size="10"/>
</script>
<script type="text/html" id="icon">
    <span class="icon {{d.icon}}"></span>
</script>
<script type="text/html" id="action">
    <a class="layui-btn layui-btn-xs" data-href="{:url('ruleEdit')}?rule_id={{d.id}}&id={{d.id}}" lay-event="edit">{:lang('edit')}</a>
    <a class="layui-btn layui-btn-xs layui-btn-warm"  data-href="{:url('ruleAdd')}?rule_id={{d.id}}" lay-event="add">{:lang('add')}{:lang('child')}</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs"  data-href="{:url('ruleDel')}" lay-event="del">{:lang('del')}</a>
</script>
<script type="text/html" id="topBtn">
    <a data-href="{:url('ruleAdd')}" class="layui-btn layui-btn-sm" lay-event="add">{:lang('add')}权限</a>
</script>
{include file="common/footer"/}
<script>
    var tableIn=null, treeGrid=null,tableId='list',layer=null, Admin =null;

    layui.config({
        base: '/static/',
        version: true
    }).extend({
        treeGrid: 'plugins/layui/extend/treeGrid/treeGrid',
    }).use(['jquery','treeGrid','layer','form'], function(){
        var $=layui.jquery ,form = layui.form;
        treeGrid = layui.treeGrid;
        layer=layui.layer;
        tableIn=treeGrid.render({
            id:tableId
            ,elem: '#'+tableId
            ,idField:'id'
            ,url:'{:url("adminRule")}'
            ,cellMinWidth: 100
            ,treeId:'id'//树形id字段名称
            ,treeUpId:'pid'//树形父id字段名称
            ,treeShowName:'title'//以树形式显示的字段
            ,height:'full-140'
            ,isFilter:false
            ,iconOpen:true//是否显示图标【默认显示】
            // ,isOpenDefault:true//节点默认是展开还是折叠【默认展开】
            ,cols: [[
                {field: 'id', title: '{:lang("id")}', width: 70, fixed: true},
                {field: 'icon', align: 'center',title: '{:lang("icon")}', width: 60,templet: '#icon'},
                {field: 'title', title: '权限名称', width: 200},
                {field: 'href', title: '控制器/方法', minwidth: 200},
                {field: 'auth_open',align: 'center', title: '是否验证权限', width: 150,toolbar: '#auth'},
                {field: 'menu_status',align: 'center',title: '菜单{:lang("status")}', width: 150,toolbar: '#status'},
                {field: 'sort',align: 'center', title: '{:lang("order")}', width: 80, templet: '#order'},
                {title:'操作',width:200, toolbar: '#action',align:"center"},
            ]]
            ,page:false
        });
        treeGrid.on('tool(list)', function(obj){
            var data = obj.data;
            var url = $(this).attr('data-href');
            if(obj.event === 'del'){
                layer.confirm('Are you sure you want to delete it', function(index){
                    loading =layer.load(1, {shade: [0.1,'#fff']});
                    $.post(url?url:'delete',{ids:[data.id]},function(res){
                        layer.close(loading);
                        layer.close(index);
                        if(res.code>0){
                            layer.msg(res.msg,{time:1000,icon:1});
                            obj.del();
                        }else{
                            layer.msg(res.msg,{time:1000,icon:2});
                        }
                    });
                });
            }else if(obj.event === 'add'){
                var iframe = layer.open({
                    type: 2,
                    content: url?url:'add',
                    area: ['800px', '600px'],
                    maxmin: true
                });
                layer.full(iframe);

            }else if(obj.event === 'edit'){

                var iframe = layer.open({
                    type: 2,
                    content: url?url:'edit',
                    area: ['600px', '800px'],
                    maxmin: true
                });
                layer.full(iframe);

            }

        });

    });
    layui.config({
        base: '/static/',
        version: true
    }).extend({
        Admin: 'admin/js/Admin',
    }).use(['Admin'], function(){
        Admin = layui.Admin
    });
    function openAll() {
        var treedata=treeGrid.getDataTreeList(tableId);
        treeGrid.treeOpenAll(tableId,!treedata[0][treeGrid.config.cols.isOpen]);
    }

</script>