<div class="layui-row">
    @if(checkBtn('/admin/'.$path.'/add', $permissionsList))
        <button class="layui-btn" onclick="addItem()"><i class="layui-icon"></i>增加</button>
    @endif
    <div style="float: right;">
        @if(isset($displayActionOthers) && count($displayActionOthers) > 0)
            @foreach($displayActionOthers as $displayActionOther)
                @if(checkBtn('/admin/'.$displayActionOther['path'], $permissionsList))
                    @if(isset($displayActionOther['isShow']))
                        @if(isset($displayActionOther['isJump']))
                            <button class="layui-btn {{ $displayActionOther['class'] }}" onclick="jumpBtn(this)"
                                    data-jumptype="{{ $displayActionOther['isJump'] }}"
                                    data-href="{{ $displayActionOther['path'] }}">{{ $displayActionOther['name'] }}
                            </button>
                        @else
                            <a href="/admin/{{ $displayActionOther['path'] }}"
                               class="layui-btn {{ $displayActionOther['class'] }}">{{ $displayActionOther['name'] }}</a>
                        @endif

                    @endif
                @endif
            @endforeach
        @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>
</div>

<table class="layui-table">
    <thead>
    <tr>
        @foreach($columns as $column)
            <th>{{ $column['name'] }}</th>
        @endforeach
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    @if(count($items) > 0)
        @include('admin::component.template_tree_item', ['items' => $items, 'columns' => $columns, 'actionBtns' => $actionBtns,'path' => $path])
    @endif
    </tbody>
</table>

<script>
    $(function () {
        editItem();
        delItem();
    });

    function addItem() {
        $.get('/admin/{{ $path }}/add', function (res) {
            layer.open({
                type: 1,
                area: '700px',
                title: false,
                content: res
            })
        });

    }

    function editItem() {
        $('.editItem').on('click', function () {
            var self = $(this);
            $.get('/admin/{{ $path }}/edit', {id: self.data('itemid')}, function (res) {
                layer.open({
                    type: 1,
                    area: '700px',
                    title: false,
                    content: res
                });
            });
        })

    }

    function delItem() {
        $('.delItem').on('click', function () {
            var self = $(this);
            layer.confirm('你确定删除？', function () {
                $.post('/admin/{{ $path }}/del', {id: self.data('itemid')}, function (res) {
                    console.log(res);
                    if (res.code === 0) {
                        layer.msg('完成！', {anim: 0}, function () {
                            location.reload();
                        });
                    }
                });
            });

        })

    }

    function jumpBtn(el) {
        var self = $(el);
        var path = self.data('href');
        var type = self.data('jumptype');
        var jvalue = self.data('value') || '';
        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: '请输入排序',
            }, 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);
                        });
                    } else {
                        layer.msg('失败：' + res.message);
                    }
                });
            });
        } else {
            $.get('/admin/' + path, function (res) {
                console.log(res);
                if (res.code === 0) {
                    layer.msg('完成！', {anim: 0}, function () {
                        location.reload();
                    });
                }
            });
        }
    }

    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) {
                    location.reload();
                } 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>
