@extends(Theme::viewName('layout'))
@section('title', 'Gallery Admin')

@section('breadcrumb')
    <li class="breadcrumb-item"><a href="{{ route('home') }}"><i class="fa fa-fw fa-home"></i></a></li>
    <li class="breadcrumb-item"><a href="{{ route('admin') }}">@lang('navigation.breadcrumb.admin')</a></li>
    <li class="breadcrumb-item"><a href="{{ route('admin.settings') }}">@lang('navigation.breadcrumb.settings')</a></li>
    <li class="breadcrumb-item active">@lang('navigation.breadcrumb.default_album_permissions')</li>
@endsection

@section('content')
    <div class="container">
        <div class="row">
            <div class="col">
                <h1>@lang('admin.default_album_permissions.title')</h1>
                <div class="alert alert-info mb-4">
                    <p><i class="fa fa-fw fa-info"></i>@lang('admin.default_album_permissions.intro')</p>
                    <p class="mb-0">@lang('admin.default_album_permissions.intro_2')</p>
                </div>

                <hr/>
                <h5 style="font-weight: bold;">@lang('admin.security_groups_heading')</h5>

                <form action="{{ route('albums.set_default_group_permissions') }}" method="post">
                    {{ csrf_field() }}

                    @if (count($existing_groups) > 0)
                        <div id="groups-accordion" role="tablist" aria-multiselectable="true">
                            @foreach ($existing_groups as $group)
                                @include(Theme::viewName('partials.album_permissions'), [
                                    'key_id' => 'group_' . $group->id,
                                    'object_id' => $group->id,
                                    'title' => $group->name,
                                    'callback' => [\App\Http\Controllers\Admin\AlbumController::class, 'doesGroupHaveDefaultPermission'],
                                    'callback_object' => $group,
                                    'parent_id' => 'groups-accordion'
                                ])
                            @endforeach
                        </div>
                    @endif

                    <div class="row mt-3">
                        <div class="col-md-4">
                            <select class="form-control" name="group_id" style="margin-bottom: 2px;"@if (count($add_new_groups) == 0) disabled="disabled"@endif>
                                @foreach ($add_new_groups as $group)
                                    <option value="{{ $group->id }}">{{ $group->name }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-2">
                            <button type="submit" name="action" value="add_group" class="btn btn-primary">Assign Permissions</button>
                        </div>
                        <div class="col-md-6 text-right">
                            <button type="submit" name="action" value="update_group_permissions" class="btn btn-success">
                                <i class="fa fa-fw fa-check"></i> @lang('forms.save_action')
                            </button>
                        </div>
                    </div>
                </form>
                <hr/>

                <h5 style="font-weight: bold;">@lang('admin.security_users_heading')</h5>

                <form action="{{ route('albums.set_default_user_permissions') }}" method="post">
                    {{ csrf_field() }}

                    <div id="users-accordion" role="tablist" aria-multiselectable="true">
                        {{-- Anonymous users --}}
                        @include(Theme::viewName('partials.album_permissions'), [
                            'key_id' => 'anonymous',
                            'object_id' => 'anonymous',
                            'title' => trans('admin.anonymous_users'),
                            'callback' => [\App\Http\Controllers\Admin\AlbumController::class, 'doesUserHaveDefaultPermission'],
                            'callback_object' => null,
                            'parent_id' => 'users-accordion'
                        ])

                        @foreach ($existing_users as $user)
                            @include(Theme::viewName('partials.album_permissions'), [
                                'key_id' => 'user_' . $user->id,
                                'object_id' => $user->id,
                                'title' => $user->name,
                                'callback' => [\App\Http\Controllers\Admin\AlbumController::class, 'doesUserHaveDefaultPermission'],
                                'callback_object' => $user,
                                'parent_id' => 'users-accordion'
                            ])
                        @endforeach
                    </div>

                    <div class="row mt-3">
                        <div class="col-md-4">
                            <input class="form-control" name="user_name" id="user-search-textbox" size="20" style="margin-bottom: 2px;" />
                            <input type="hidden" name="user_id" id="user-id-field" />
                        </div>
                        <div class="col-md-2">
                            <button type="submit" name="action" value="add_user" class="btn btn-primary">Assign Permissions</button>
                        </div>
                        <div class="col-md-6 text-right">
                            <button type="submit" name="action" value="update_user_permissions" class="btn btn-success">
                                <i class="fa fa-fw fa-check"></i> @lang('forms.save_action')
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
@endsection

@push('scripts')
    <script type="text/javascript">
        {{-- Select All/None links on the permissions tab --}}
        $('a.select-all').click(function() {
            $('input:checkbox', $(this).closest('.card-body')).prop('checked', true);
            return false;
        });
        $('a.select-none').click(function() {
            $('input:checkbox', $(this).closest('.card-body')).prop('checked', false);
            return false;
        });

                {{-- Type-ahead support for users textbox on the permissions tab --}}
        var userDataSource = new Bloodhound({
                datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
                queryTokenizer: Bloodhound.tokenizers.whitespace,
                remote: {
                    url: '{{ route('users.searchJson') }}?q=%QUERY',
                    wildcard: '%QUERY'
                }
            });

        $('#user-search-textbox').typeahead(null, {
            name: 'user-search',
            display: 'name',
            source: userDataSource
        });
        $('#user-search-textbox').bind('typeahead:select', function(ev, suggestion) {
            $('#user-id-field').val(suggestion.id);
        });
    </script>
@endpush