<div @if(request()->ajax()) style="max-height: 600px;overflow-y: auto;" @endif>
    @if(count($searchs) > 0)
        <div class="layui-row">
            <form class="layui-form" method="get" action="/admin/{{ $path }}" style="margin-bottom: 10px;">
                @csrf
                @if(request()->has('id'))
                    <input type="hidden" name="id" value="{{ request()->input('id') }}">
                @endif
                @foreach($searchs as $var => $search)
                    @if($search['type'] == 'input')
                        <div class="layui-input-inline">
                            <input class="layui-input" type="text"
                                   placeholder="{{ $search['label'] }}"
                                   name="{{ $var }}"
                                   value="{{ ${$var} ?? '' }}">
                        </div>
                    @endif
                    @if($search['type'] == 'select')
                        <div class="layui-input-inline">
                            <select name="{{ $var }}">
                                <option value="0"
                                        @if(!isset($search['assist'])) selected @endif>{{ $search['label'] }}
                                </option>
                                @if(count($search['assist']) > 0)
                                    @foreach($search['assist'] as $k => $col)
                                        <option value="{{ $k }}"
                                                @if(isset(${$var}) && ${$var} == $k) selected @endif>{{ $col }}
                                        </option>
                                    @endforeach
                                @endif
                            </select>
                        </div>
                    @endif
                    @if($search['type'] == 'tree_select')
                        <div class="layui-input-inline">
                            <select name="{{ $var }}">
                                <option value="0"
                                        @if(!isset($search['assist'])) selected @endif>{{ $search['label'] }}
                                </option>
                                @if(count($search['assist']) > 0)
                                    @include('admin::component.multi_select_option', ['selects' => $search['assist'], 'selectPid' => request($var) ?? ''])
                                @endif
                            </select>
                        </div>
                    @endif
                    @if($search['type'] == 'date')
                        <div class="layui-input-inline">
                            <input class="layui-input date-{{ $var }}" type="text"
                                   placeholder="{{ $search['label'] ?? '日期' }}"
                                   name="{{ $var }}" autocomplete="off" readonly
                                   disableautocomplete value="{{ $search['assist'][0] ?? '' }}"
                            >
                        </div>
                        <script>
                            $('.date-{{ $var }}').datepicker({
                                language: 'zh-CN',
                                format: 'yyyy-mm-dd',
                                date: '{{ $search['assist'][0] ?? null }}',
                            });
                        </script>
                    @endif
                    @if($search['type'] == 'date_time')
                        <div class="layui-input-inline">
                            <input class="layui-input star-{{ $var }}" type="text"
                                   placeholder="{{ $search['label'][0] ?? '起始日期' }}"
                                   name="star_{{ $var }}" autocomplete="off" readonly
                                   disableautocomplete value="{{ $search['assist'][0] ?? '' }}">
                        </div> -
                        <div class="layui-input-inline">
                            <input class="layui-input end-{{ $var }}" type="text"
                                   placeholder="{{ $search['label'][1] ?? '结束日期' }}"
                                   name="end_{{ $var }}" autocomplete="off" readonly
                                   disableautocomplete value="{{ $search['assist'][1] ?? '' }}">
                        </div>
                        <script>
                            $('.star-{{ $var }}').datepicker({
                                language: 'zh-CN',
                                format: 'yyyy-mm-dd',
                                date: '{{ $search['assist'][0] ?? null }}',
                                pick: function (rtime) {
                                    $('.end-{{ $var }}').datepicker('setStartDate', rtime.date);
                                }
                            });
                            $('.end-{{ $var }}').datepicker({
                                language: 'zh-CN',
                                format: 'yyyy-mm-dd',
                                date: '{{ $search['assist'][0] ?? null }}'
                            });
                        </script>
                    @endif
                @endforeach
                <button class="layui-btn" type="submit"><i class="layui-icon">&#xe615;</i></button>
            </form>
        </div>
    @endif
    @if(count($topActions) > 0 || (isset($topActionOthers) && count($topActionOthers)) > 0)
        <div class="weadmin-block layui-clear">
            @if(in_array('goback', $topActions))
                @if(isset($topActions['href']))
                    <a class="layui-btn layui-btn-primary layui-layer-close" style="position:relative;"
                       href="{{ $topActions['href'] }}">返回
                    </a>
                @else
                    <button class="layui-btn layui-btn-primary layui-layer-close" style="position:relative;" type="button"
                            onclick="javascript:history.go(-1);">返回
                    </button>
                @endif
            @endif
            @if(checkBtn('/admin/'.$path.'/add', $permissionsList))
                @if(in_array('add', $topActions))
                    @if(isset($topActions['add_params']))
                        <a href="/admin/{{ $path }}/add?{{ $topActions['add_params']}} " class="layui-btn">添加</a>
                    @else
                        <a href="/admin/{{ $path }}/add" class="layui-btn">添加</a>
                    @endif
                @endif
            @endif

            @if(checkBtn('/admin/'.$path.'/export', $permissionsList))
                @if(in_array('export', $topActions))
                    <a href="/admin/{{ $path }}/export?{{ http_build_query(request()->except('_token')) }}"
                       class="layui-btn">Excel导出当前页</a>
                    <a href="/admin/{{ $path }}/export?type=all&{{ http_build_query(request()->except('_token')) }}"
                       class="layui-btn">Excel导出全部</a>
                @endif
            @endif

            @if(checkBtn('/admin/'.$path.'/importTemplate', $permissionsList))
                @if(in_array('importTemplate', $topActions))
                    <a href="/admin/{{ $path }}/importTemplate" class="layui-btn">下载导入模板</a>
                @endif
            @endif

            @if(checkBtn('/admin/'.$path.'/import', $permissionsList))
                @if(in_array('import', $topActions))
                    <button type="button" class="layui-btn" id="import">
                        <i class="layui-icon">&#xe67c;</i>Excel导入
                    </button>
                @endif
            @endif
            <div style="float: right;">
                @if(isset($topActionOthers) && count($topActionOthers) > 0)
                    @foreach($topActionOthers as $topActionOther)
                        @if(checkBtn('/admin/'.$topActionOther['path'], $permissionsList))
                            @if(isset($topActionOther['isShow']))
                                @if(isset($topActionOther['isJump']))
                                    <button class="layui-btn {{ $topActionOther['class'] }}"
                                            onclick="jumpBtn(this)"
                                            data-jumptype="{{ $topActionOther['isJump'] }}"
                                            data-href="{{ $topActionOther['path'] }}">{{ $topActionOther['name'] }}
                                    </button>
                                @else
                                    <a href="/admin/{{ $topActionOther['path'] }}"
                                       class="layui-btn {{ $topActionOther['class'] }}">{{ $topActionOther['name'] }}</a>
                                @endif

                            @endif
                        @endif
                    @endforeach
                @endif
            </div>
        </div>
    @endif
    <table class="layui-table">
        <thead>
        <tr>
            @foreach($columns as $column)
                <th>{{ $column['name'] }}</th>
            @endforeach
            @if((isset($displayActionOthers) && count($displayActionOthers) > 0) || count($actionBtns) > 0)
                <th>操作</th>
            @endif
        </tr>
        </thead>
        <tbody>
        @if(count($items) > 0)
            @foreach($items as $item)
                <tr>
                    @foreach($columns as $varName => $column)
                        <td>{!! isset($column['value']) ? $column['value']($item->$varName, $item) : $item->$varName !!}</td>
                    @endforeach
                    @if((isset($displayActionOthers) && count($displayActionOthers) > 0) || count($actionBtns) > 0)
                        <td>
                            @if(isset($displayActionOthers) && count($displayActionOthers) > 0)
                                @foreach($displayActionOthers as $displayActionOther)
                                    @if(checkBtn('/admin/'.$displayActionOther['path'], $permissionsList))
                                        @if(isset($displayActionOther['isShow']) ? $displayActionOther['isShow']($item) : 1)
                                            @if(isset($displayActionOther['isJump']))
                                                <button
                                                    class="layui-btn layui-btn-xs {{ $displayActionOther['class'] }} @if($displayActionOther['isJump'] == 'uploadimg') uploadimg @endif"
                                                    onclick="jumpBtn(this)"
                                                    data-title="{{ $displayActionOther['title'] ?? '' }}"
                                                    data-jumptype="{{ $displayActionOther['isJump'] }}"
                                                    data-href="{{ substr($displayActionOther['path'],-1) == '&' ? $displayActionOther['path']:$displayActionOther['path'].'?' }}{{ $primaryKey }}={{ $item->$primaryKey }}">{{ $displayActionOther['name'] }}
                                                </button>
                                            @else
                                                <a href="/admin/{{ substr($displayActionOther['path'],-1) == '&' ? $displayActionOther['path']:$displayActionOther['path'].'?' }}{{ $primaryKey }}={{ $item->$primaryKey }}"
                                                   class="layui-btn layui-btn-xs {{ $displayActionOther['class'] }}"
                                                   @if(isset($displayActionOther['fullWin'])) target="_blank" @endif>{{ $displayActionOther['name'] }}</a>
                                            @endif
                                        @endif
                                    @endif
                                @endforeach
                            @endif
                            @if(!is_array($item) || !in_array($item[$displayActions['name']], $displayActions['value']))
                                @if(checkBtn('/admin/'.$path.'/view', $permissionsList))
                                    @if(in_array('view', $actionBtns))
                                        <a href="/admin/{{ $path }}/view?{{ $primaryKey }}={{ $item->$primaryKey }}"
                                           class="layui-btn layui-btn-normal layui-btn-xs" {{ $actionBtnsAttribute['view'] ?? '' }} >查看</a>
                                    @endif
                                @endif
                                @if(checkBtn('/admin/'.$path.'/edit', $permissionsList))
                                    @if(in_array('edit', $actionBtns))
                                        <a href="/admin/{{ $path }}/edit?{{ $primaryKey }}={{ $item->$primaryKey }}"
                                           class="layui-btn layui-btn-xs" {{ $actionBtnsAttribute['edit'] ?? '' }}>编辑</a>
                                    @endif
                                @endif
                                @if(checkBtn('/admin/'.$path.'/del', $permissionsList))
                                    @if(in_array('del', $actionBtns))
                                        <button class="layui-btn layui-btn-xs layui-btn-danger delItem"
                                                data-itemid="{{ $item->$primaryKey }}" {{ $actionBtnsAttribute['del'] ?? '' }}>
                                            删除
                                        </button>
                                    @endif
                                @endif
                            @endif
                        </td>
                    @endif
                </tr>
            @endforeach
        @else
            <tr>
                <td colspan="{{ count($columns) + 1 }}">暂无数据</td>
            </tr>
        @endif
        </tbody>
    </table>
</div>
@if(method_exists($items, 'links'))
    {{ $items->appends(request()->except('_token'))->links() }}
@endif

<script>
    $(function () {
        $('.delItem').on('click', function () {
            var self = $(this);
            layer.confirm('你确定删除？', function () {
                layer.closeAll();
                $.post('/admin/{{ $path }}/del', {"{{ $primaryKey }}": self.data('itemid')}, function (res) {
                    if (res.code === 0) {
                        layer.msg('成功！', {anim: 0}, function () {
                            location.reload();
                        });
                    } else {
                        layer.msg(res.message);
                    }
                });
            });
        })
    });

    function jumpBtn(el) {
        var self = $(el);
        var path = self.data('href');
        var type = self.data('jumptype');
        var jvalue = self.data('value') || '';
        var title = self.data('title') || '';
        var text = self.text();
        if (type === 'confirm') {
            layer.confirm('确定' + text + '？', function (index) {
                layer.close(index);
                $.get('/admin/' + path, function (res) {
                    if (res.code === 0) {
                        layer.msg('成功！', {anim: 0}, function () {
                            location.reload();
                        });
                    } else {
                        layer.msg('失败：' + res.message);
                    }
                });
            });
        } else if (type === 'uploadimg') {

        } else if (type === 'prompt') {
            layer.prompt({
                value: jvalue,
                title: title,
            }, function (value, index, elem) {
                layer.close(index);
                $.get('/admin/' + path, {newValue: value}, function (res) {
                    if (res.code === 0) {
                        layer.msg('成功！', {anim: 0, time: 1000}, function () {
                            self.text(value).data('value', value);
                            location.reload();
                        });
                    } else {
                        layer.msg('失败：' + res.message);
                    }
                });
            });
        } else {
            $.get('/admin/' + path, function (res) {
                layer.open({
                    type: 1,
                    area: '700px',
                    offset: '150px',
                    title: false,
                    content: res
                });
            });
        }
    }

    layui.use(['upload', 'form'], function () {
        var upload = layui.upload, form = layui.form;

        form.on('switch', function (data) {
            var self = $(data.elem);
            var path = self.data('href');
            $.get('/admin/' + path, function (res) {
                console.log(res);
                if (res.code === 0) {
                } else {
                    layer.msg('失败：' + res.message);
                }
            });
        });
        //执行实例
        var uploadInst = upload.render({
            elem: '#import' //绑定元素
            , field: 'excel'
            , exts: 'xls|xlsx|csv'
            , url: '/admin/{{ $path }}/import' //上传接口
            , done: function (res) {
                if (res.code === 0) {
                    layer.msg('导入成功，加载请稍等待！', {anim: 0}, function () {
                        location.reload();
                    });
                } else {
                    layer.msg('导入失败，即将跳转到错误信息页。', {anim: 0}, function () {
                        location.href = '/admin/errorPage';
                    });
                }
            }
            , error: function (res) {
                layer.msg(res.message);
            }
        });

        //执行实例
        $('.uploadimg').each(function (index, elem) {
            upload.render({
                elem: elem //绑定元素
                , field: 'img'
                , url: '/admin/' + $(elem).data('href') //上传接口
                , done: function (res) {
                    if (res.code === 0) {
                        layer.msg('上传成功', {anim: 0}, function () {
                            location.reload();
                        });
                    } else {
                        layer.msg(res.message);
                    }
                }
                , error: function (res) {
                    layer.msg(res.message);
                }
            });
        });

    });
</script>
