@extends('layouts.page')

@section('nav')
<div class="x-nav">
          <span class="layui-breadcrumb">
            <a href="">首页</a>
            <a href="">{{$nav}}</a>
{{--            <a>--}}
{{--              <cite>导航元素</cite></a>--}}
          </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
</div>
@endsection

@section('content')
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <!-- 搜索 -->
                <div class="layui-card-body ">
                    @if ($isSearch)
                        @component('components.search_bar', ['search_fields' => $searchFields])
                        @endcomponent
                    @endif
                </div>


                    @if ($isCreate)
                    <div class="layui-card-header">
                        @component('components.action_add', ['route'=>"admin/{$model}/add"])
                        @endcomponent
                    </div>
                    @endif

                <!-- table -->
                <div class="layui-card-body layui-table-body layui-table-main">
                    <table class="layui-table layui-form">
                        <thead>
                        <tr>
                            <th width="30">
                                <input type="checkbox" lay-filter="checkall" name="" lay-skin="primary">
                            </th>
                            @foreach($titles as $title)
                                <th>
                                    @if (is_array($title))
                                        {{$title['name']}}
                                    @else
                                        {{$title}}
                                    @endif
                                </th>
                            @endforeach
                            <th>操作</th></tr>
                        </thead>
                        <tbody>
                        @foreach($list as $data)
                            <tr>
                                <td width="30">
                                    <input type="checkbox" name="id" value="1"   lay-skin="primary">
                                </td>
                                @foreach($titles as $name => $title)
                                    @if (is_array($title))
                                        @component('components.columns', ['type'=>$title['type'], 'value' => $data[$name]])
                                            @if (is_string($data[$name]))
                                                {{$data[$name]}}
                                            @endif
                                        @endcomponent
                                    @else
                                        @component('components.columns', ['type'=>'text'])
                                            {{$data[$name]}}
                                        @endcomponent
                                    @endif

                                @endforeach
                                <td class="td-manage">

                                    @foreach($data['extensionActions'] as $key => $action)
                                        @if(in_array($data[$action['name']],$action['in']))
                                        @component('components.action', ['key' => $key,'route'=>$action['route'],'icon'=>$action['icon'],'model'=>$action['model'], 'id'=>$data->id])
                                        @endcomponent
                                        @endif
                                    @endforeach

                                    @if ($isEdit)
                                        @component('components.action_edit', ['route'=>"admin/{$model}/edit", 'id'=>$data->id])
                                        @endcomponent
                                    @endif

                                    @if ($data['isDelete'])
                                        @component('components.action_delete', ['route'=>"admin/{$model}/delete", 'id'=>$data->id])
                                        @endcomponent
                                    @endif
                                </td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                </div>

                <!-- 分页 -->
                <div class="layui-card-body ">
                    <div class="page">
                        {{ $list->links() }}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@push('scripts')
    <script>
        layui.use(['laydate','form', 'layer'], function(){
            var laydate = layui.laydate;
            var  form = layui.form;
            // 监听全选
            form.on('checkbox(checkall)', function(data){

                if(data.elem.checked){
                    $('tbody input').prop('checked',true);
                }else{
                    $('tbody input').prop('checked',false);
                }
                form.render('checkbox');
            });

            //执行一个laydate实例
            laydate.render({
                elem: '#start' //指定元素
            });

            //执行一个laydate实例
            laydate.render({
                elem: '#end' //指定元素
            });


        });

        /*用户-停用*/
        function member_stop(obj,id){
            layer.confirm('确认要停用吗？',function(index){

                if($(obj).attr('title')=='启用'){

                    //发异步把用户状态进行更改
                    $(obj).attr('title','停用')
                    $(obj).find('i').html('&#xe62f;');

                    $(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('已停用');
                    layer.msg('已停用!',{icon: 5,time:1000});

                }else{
                    $(obj).attr('title','启用')
                    $(obj).find('i').html('&#xe601;');

                    $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('已启用');
                    layer.msg('已启用!',{icon: 5,time:1000});
                }

            });
        }

        function delAll (argument) {
            var ids = [];

            // 获取选中的id
            $('tbody input').each(function(index, el) {
                if($(this).prop('checked')){
                    ids.push($(this).val())
                }
            });

            layer.confirm('确认要删除吗？'+ids.toString(),function(index){
                //捉到所有被选中的，发异步进行删除
                layer.msg('删除成功', {icon: 1});
                $(".layui-form-checked").not('.header').parents('tr').remove();
            });
        }
    </script>
@endpush
