@extends('base.base')
@section('base')
    <div class="main-panel">
        <div class="content-wrapper">
            <div class="page-header">
                <h3 class="page-title">
                     <span class="page-title-icon bg-gradient-primary text-white mr-2">
                        <i class="mdi mdi-image-multiple"></i>
                    </span>
                    {{ __('auto.Banner管理') }}
                </h3>
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="#">{{ __('auto.内容管理') }}</a></li>
                        <li class="breadcrumb-item active" aria-current="page">{{ __('auto.Banner管理') }}</li>
                    </ol>
                </nav>
            </div>
            <div class="row">
                <div class="col-lg-12 grid-margin stretch-card">
                    <div class="card">
                        <div class="card-body">
                            <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
                                <h4 class="card-title">{{ __('auto.Banner列表') }}</h4>
                                <a href="{{ url('admin/banner/add') }}" class="btn btn-gradient-primary">{{ __('auto.添加Banner') }}</a>
                            </div>

                            <!-- 筛选 -->
                            <form method="GET" action="{{ url('admin/banner') }}" class="mb-3">
                                <div class="form-row">
                                    <div class="col-md-3">
                                        <select class="form-control" name="theme_key">
                                            <option value="">-- 全部主题 --</option>
                                            @foreach($themes as $theme)
                                                <option value="{{$theme->ThemeKey}}" {{ $theme_key == $theme->ThemeKey ? 'selected' : '' }}>
                                                    {{$theme->ThemeKey}}
                                                </option>
                                            @endforeach
                                        </select>
                                    </div>
                                    <div class="col-md-2">
                                        <button type="submit" class="btn btn-primary">{{ __('auto.搜索') }}</button>
                                    </div>
                                </div>
                            </form>

                            <table class="table table-bordered">
                                <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>{{ __('auto.图片') }}</th>
                                    <th>Alt</th>
                                    <th>Link Game</th>
                                    <th>Link Module</th>
                                    <th>{{ __('auto.事件') }}(link)</th>
                                    <th>{{ __('auto.排序') }}</th>
                                    <th>{{ __('auto.状态') }}</th>
                                    <th>{{ __('auto.主题KEY') }}</th>
                                    <th>{{ __('auto.操作') }}</th>
                                </tr>
                                </thead>
                                <tbody>
                                @foreach($list as $v)
                                    <tr>
                                        <td>{{$v->bid}}</td>
                                        <td>
                                            @if($v->img)
                                                <img src="{{$v->img}}" style="max-width: 200px; height: auto;" alt="Banner">
                                            @endif
                                        </td>
                                        <td>{{$v->alt}}</td>
                                        <td>{{$v->link_game}}</td>
                                        <td>{{$v->link_module}}</td>
                                        <td style="max-width: 200px; word-break: break-all;">{{$v->link}}</td>
                                        <td>{{$v->b_order}}</td>
                                        <td>{{$v->state}}</td>
                                        <td>{{$v->theme_key}}</td>
                                        <td style="min-width: 260px;">
                                            <a href="{{ url('admin/banner/edit/'.$v->bid) }}" class="btn btn-sm btn-gradient-info">{{ __('auto.编辑') }}</a>
                                            <button class="btn btn-sm btn-gradient-success" onclick="syncBanner({{$v->bid}})" title="其他主题若没有此事件(link)则复制过去">{{ __('auto.同步到其他主题') }}</button>
                                            <button class="btn btn-sm btn-gradient-danger" onclick="deleteBanner({{$v->bid}})">{{ __('auto.删除') }}</button>
                                        </td>
                                    </tr>
                                @endforeach
                                </tbody>
                            </table>
                            <div class="box-footer clearfix" id="pages">
                                {{ __('auto.总共') }} <b>{{ $list->total() }}</b> {{ __('auto.条,分为') }}<b>{{ $list->lastPage() }}</b>{{ __('auto.页') }}
                                {!! $list->links() !!}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        function deleteBanner(id) {
            if (confirm('确认删除此Banner？')) {
                $.ajax({
                    url: '/admin/banner/delete/' + id,
                    type: 'POST',
                    success: function(res) {
                        if (res.code === 200) {
                            alert('删除成功');
                            location.reload();
                        } else {
                            alert(res.msg || '删除失败');
                        }
                    }
                });
            }
        }

        function syncBanner(id) {
            if (!confirm('确认将此 Banner(事件) 同步到所有其他主题吗？\n规则：其他主题若不存在相同 link 的 Banner，则复制过去。')) {
                return;
            }
            $.ajax({
                url: '/admin/banner/sync/' + id,
                type: 'POST',
                success: function(res) {
                    if (res.code === 200) {
                        alert(res.msg || '同步成功');
                        location.reload();
                    } else {
                        alert(res.msg || '同步失败');
                    }
                },
                error: function(xhr) {
                    alert('请求失败：' + (xhr.responseJSON && xhr.responseJSON.msg ? xhr.responseJSON.msg : xhr.status));
                }
            });
        }
    </script>
@endsection
