@extends('admin.layouts.app')
@section('panel')
    @include('admin.components.tabs.owner')
    <div class="row gy-4">
        <div class="col-xxl-12">
            <div class="card b-radius--10 ">
                <div class="card-body p-0">
                    <div class="table-responsive--md  table-responsive">
                        <table class="table table--light style--two">
                            <thead>
                                <tr>
                                    <th class="text-center">@lang('#')</th>
                                    <th>@lang('Preview')</th>
                                    <th>@lang('Owner')</th>
                                    <th>@lang('Reviewer')</th>
                                    <th>@lang('Title')</th>
                                    <th>@lang('Category')</th>
                                    <th>@lang('Submitted On')</th>
                                    <th>@lang('Is Premium?')</th>
                                    <th>@lang('Status')</th>
                                    @if (Request::routeIs('admin.file.my.all'))
                                        <th>@lang('Action')</th>
                                    @endif
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($allFiles as $item)
                                    <tr>
                                        <td data-label="#" class="text-center">
                                            {{ $loop->iteration }}
                                        </td>
                                        <td data-label="preview" class="info"><img
                                                class="{{ $item->height > $item->width ? 'image-preview-h' : 'image-preview-w' }}"
                                                src="{{ getImage(getFilePath('media') . $item->file) }}" alt="image">
                                        </td>
                                        <td data-label="preview" class="info">
                                            @if (isset($item->user))
                                                <a
                                                    href="{{ route('admin.users.detail', $item->user->id) }}">{{ $item->user->fullname }}</a>
                                            @else
                                                @lang('Admin')
                                            @endif
                                        </td>
                                        <td data-label="preview" class="info">
                                            @if (isset($item->reviewer))
                                                <a
                                                    href="{{ route('admin.reviewer.edit', $item->reviewer->id) }}">{{ $item->reviewer->name }}</a>
                                            @else
                                                <h6 class="text-danger">@lang('Admin')</h6>
                                            @endif
                                        </td>
                                        <td data-label="preview" class="info">
                                            @if (strlen($item->title) < 21)
                                                <span class="fw-bold">
                                                    {{ $item->title }}
                                                </span>
                                            @else
                                                <span class="fw-bold">
                                                    {{ substr($item->title, 0, 20) }}...
                                                </span>
                                            @endif
                                        </td>
                                        <td data-label="published">
                                            {{ $item->category->name }}
                                        </td>
                                        <td data-label="published">
                                            {{ showDateTime($item->created_at, 'h:i A, M Y') }}
                                        </td>
                                        <td data-label="download">
                                            @if (isset($item->is_premium))
                                                @lang('Premium')<i class="text--primary fas fa-crown mx-2"></i>
                                            @else
                                                @lang('Free')<i class="text--primary fas fa-check-circle mx-2"></i>
                                            @endif
                                        </td>
                                        <td data-label="download">
                                            @php
                                                echo $item->statusBadge;
                                            @endphp
                                        </td>
                                        @if (Request::routeIs('admin.file.my.all'))
                                            <td data-label="Action" class="table-dropdown">
                                                <a href="{{ route('admin.file.edit', $item->id) }}"
                                                    class="btn btn--primary ms-1"><i class="las la-edit"></i></a>
                                                <button class="confirmationBtn btn btn--danger ms-1"
                                                    data-question="@lang('Are you sure to delete this image?')"
                                                    data-action="{{ route('admin.file.delete', $item->id) }}"><i
                                                        class="las la-trash"></i></button>
                                            </td>
                                        @endif
                                    </tr>
                                @empty
                                    <tr>
                                        <td class="text-muted text-center" colspan="100%">{{ __($emptyMessage) }}</td>
                                    </tr>
                                @endforelse

                            </tbody>
                        </table><!-- table end -->
                    </div>
                </div>
                @if ($allFiles->hasPages())
                    <div class="card-footer py-4">
                        {{ paginateLinks($allFiles) }}
                    </div>
                @endif
            </div>
        </div>
    </div>
    <x-confirmation-modal></x-confirmation-modal>
@endsection

@push('style-lib')
    <link href="{{ asset('assets/admin/css/fontawesome-iconpicker.min.css') }}" rel="stylesheet">
@endpush
@push('script-lib')
    <script src="{{ asset('assets/admin/js/fontawesome-iconpicker.js') }}"></script>
@endpush

@push('script')
    <script>
        (function($) {
            "use strict";

            $('.createCatBtn').on('click', function() {
                var modal = $('#createCat');
                modal.modal('show');
            });

            $('.editCatBtn').on('click', function() {
                var modal = $('#editCat');
                let url = $(this).data('action');
                let base = "{{ url('/') }}";
                let img = $(this).data('image');
                let imgPath = base + `/{{ getFilePath('category') }}/${img}`;

                $('#editForm').attr('action', url);
                modal.find('input[name="name"]').val($(this).data('name'));
                $('.editCatPreview').css('background', 'url(' + imgPath + ') no-repeat center center/cover');

                if ($(this).data('status') == 1) {
                    modal.find('input[name="catstatus"]').prop('checked', true);
                }

                if ($(this).data('status') == 0) {
                    modal.find('input[name="catstatus"]').prop('checked', false);
                }
                modal.modal('show');
            });
        })(jQuery);
    </script>

    <script>
        $('.iconPicker').iconpicker().on('iconpickerSelected', function(e) {
            $(this).closest('.form-group').find('.iconpicker-input').val(`<i class="${e.iconpickerValue}"></i>`);
        });
    </script>
@endpush

@push('breadcrumb-plugins')
    <div class="text-end mb-2">
        <a href="{{ route('admin.file.create') }}" class="btn btn-sm btn--primary createCatBtn"><i
                class="las la-plus"></i>@lang('Add New')</a>
    </div>
@endpush
