@extends('admin.base')

@section('content')
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">
            <div class="layui-btn-group ">
                @can('config.gmenu.destroy')
                    <button class="layui-btn layui-btn-sm layui-btn-danger" id="listDelete">删除</button>
                @endcan
                @can('config.gmenu.create')
                    <a class="layui-btn layui-btn-sm" href="{{ route('admin.gmenu.create') }}">添加</a>
                @endcan
                <button class="layui-btn layui-btn-sm" id="memberSearch">搜索</button>
            </div>
            <div class="layui-form">
                <div class="layui-input-inline">
                    <input type="text" name="parent_id" id="parent_id" placeholder="父ID" class="layui-input">
                </div>

                <div class="layui-input-inline">
                    <input type="text" name="gname" id="gname" placeholder="名称" class="layui-input">
                </div>

                <div class="layui-input-inline">
                    <input type="text" name="status" id="status" placeholder="状态" class="layui-input">
                </div>

                <div class="layui-input-inline">
                    <input type="text" name="isleaf" id="isleaf" placeholder="节点" class="layui-input">
                </div>

            </div>
        </div>

        <div class="layui-card-body">
            <table id="dataTable" lay-filter="dataTable"></table>
            <script type="text/html" id="options">
                <div class="layui-btn-group">
                    @can('config.gmenu.create')
                        <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
                    @endcan
                    @can('config.gmenu.destroy')
                        <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a>
                    @endcan
                </div>
            </script>
            <script type="text/html" id="avatar">
                <a href="@{{d.avatar}}" target="_blank" title="点击查看"><img src="@{{d.avatar}}" alt="" width="28" height="28"></a>
            </script>
        </div>
    </div>
@endsection

@section('script')
    @can('config.gmenu')
        <script>
            layui.use(['layer','table','form'],function () {
                var layer = layui.layer;
                var form = layui.form;
                var table = layui.table;
                //用户表格初始化
                var dataTable = table.render({
                    elem: '#dataTable'
                    ,height: 500
                    ,url: "{{ route('admin.gmenu.data') }}" //数据接口
                    ,where:{model:"gmenu"}
                    ,page: true //开启分页
                    ,cols: [[ //表头
                        {checkbox: true,fixed: true}
                        ,{field: 'id', title: 'ID', sort: true,width:80}
                        ,{field: 'gname', title: '名称'}
                        ,{field: 'parent_id', title: '父ID'}
                        ,{field: 'sort', title: '排序'}
                        ,{field: 'created_at', title: '创建时间'}
                        ,{field: 'updated_at', title: '更新时间'}
                        ,{field: 'status', title: '状态'}
                        ,{field: 'iconUrl', title: '图片URL', templet:function (d) { if ( d.iconUrl==null || d.iconUrl=='')  { return '';}else{ return '<a href="'+d.iconUrl+'" target="_blank"><img src="'+d.iconUrl+'"></a>'; }} }
                        ,{field: 'gaction', title: '目标'}
                        ,{field: 'isleaf', title: '节点'}

                        ,{fixed: 'right', width: 120, align:'center', toolbar: '#options'}
                    ]]
                });

                //监听工具条
                table.on('tool(dataTable)', function(obj){ //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
                    var data = obj.data //获得当前行数据
                        ,layEvent = obj.event; //获得 lay-event 对应的值
                    if(layEvent === 'del'){
                        layer.confirm('确认删除吗？', function(index){
                            $.post("{{ route('admin.gmenu.destroy') }}",{_method:'delete',ids:[data.id]},function (result) {
                                if (result.code==0){
                                    obj.del(); //删除对应行（tr）的DOM结构
                                }
                                layer.close(index);
                                layer.msg(result.msg)
                            });
                        });
                    } else if(layEvent === 'edit'){
                        location.href = '/admin/gmenu/'+data.id+'/edit';
                    }
                });

                //按钮批量删除
                $("#listDelete").click(function () {
                    var ids = []
                    var hasCheck = table.checkStatus('dataTable')
                    var hasCheckData = hasCheck.data
                    if (hasCheckData.length>0){
                        $.each(hasCheckData,function (index,element) {
                            ids.push(element.id)
                        })
                    }
                    if (ids.length>0){
                        layer.confirm('确认删除吗？', function(index){
                            $.post("{{ route('admin.gmenu.destroy') }}",{_method:'delete',ids:ids},function (result) {
                                if (result.code==0){
                                    dataTable.reload()
                                }
                                layer.close(index);
                                layer.msg(result.msg)
                            });
                        })
                    }else {
                        layer.msg('请选择删除项')
                    }
                })
                //搜索
                $("#memberSearch").click(function () {
                    var parent_id = $("#parent_id").val();
                    var gname = $("#gname").val();
                    var status = $("#status").val();
                    var isleaf = $("#isleaf").val();
                    dataTable.reload({
                        where:{parent_id:parent_id,gname:gname,status:status,isleaf:isleaf},
                        page:{curr:1}
                    })
                })
            })
        </script>
    @endcan
@endsection



