<!--
Copyright (C) 2022 Echedey López Romero <elr@disroot.org>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
-->

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row mb-3">
        <h1 class="col-12 mb-0 text-center">Category information</h1>
    </div>
    <div class="row">
        <div class="col-lg-10 col-12 mx-auto">
            <div class="container">
                <div class="row mb-3">
                    <span class="col-xl-4 col-12 my-xl-auto mb-2">Name</span>
                    <span class="col-xl-8 col-12 mb-xl-0 mb-2">{{ $category->name }}</span>
                </div>
                <div class="row mb-3">
                    <span class="col-xl-4 col-12 my-xl-auto mb-2">Description</span>
                    <span class="col-xl-8 col-12 mb-xl-0 mb-2">{{ $category->description ? $category->description : 'No description' }}</span>
                </div>
                <div class="row">
                    <form class="col-xl-6 col-lg-8 col-md-10 col-12 mx-auto" action="{{ route('categories.searchBooks', $category->id) }}" method="POST">
                        @csrf
                        <input class="form-control" name="search" type="text" value="{{ isset($oldSearch) ? $oldSearch : '' }}" placeholder="Search by book name, authors or editorial"/>
                    </form>
                </div>
                @if (count($books) !== 0)
                <div class="row row-cols-lg-3 row-cols-md-2 row-cols-1">
                    @foreach ($books as $book)
                    <div class="col mt-3 mx-auto">
                        <div class="card">
                            <img src="{{ asset($book->image) }}" class="card-img-top" alt="{{ $book->title }}"/>
                            <div class="card-body">
                                <div class="card-title">
                                    <div class="container px-0">
                                        <div class="row mb-2">
                                            <span class="col-12 my-auto text-center fs-5">{{ $book->title }}</span>                      
                                        </div>
                                        <div class="row mb-2">
                                            <span class="col-12 my-auto text-center">{{ $book->authors }}</span>                      
                                        </div>
                                        <div class="row mb-2">
                                            <span class="col-12 my-auto text-center fs-5">{{ $book->year ? $book->year : 'Undated' }}</span>                      
                                        </div>
                                    </div>
                                </div>
                                <div class="card-text">
                                    <div class="container px-0">
                                        <div class="row">
                                            <div class="col-12">
                                                <div class="d-grid gap-2">
                                                    <a href="{{ route('books.show', $book->id) }}" class="btn btn-primary">Show</a>
                                                </div>
                                            </div>                      
                                        </div>
                                        @if($book->document)
                                        <div class="row mt-2">
                                            <div class="col-12">
                                                <div class="d-grid gap-2">
                                                    <a href="{{ $book->document }}" target="_blank" class="btn btn-info">Download</a>
                                                </div>
                                            </div>                      
                                        </div>
                                        @endif
                                        @Auth
                                        @if (Auth::user()->hasRole('Administrator'))
                                        <div class="row mt-2">
                                            <div class="col-12">
                                                <div class="d-grid gap-2">
                                                    <a href="{{ route('books.edit', $book->id) }}" class="btn btn-warning">Edit</a>
                                                </div>
                                            </div>                   
                                        </div>
                                        <div class="row mt-2">
                                            <form action="{{ route('books.destroy', $book->id) }}" method="POST" class="col-12" onsubmit="return window.confirm('Do you want to remove this book?')">
                                                @csrf
                                                @method('DELETE')
                                                <div class="d-grid gap-2">
                                                    <button type="submit" class="btn btn-danger">Delete</button>
                                                </div>
                                            </form>                    
                                        </div>
                                        @endif
                                        @endauth
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    @endforeach
                </div>
                <div class="row mt-3">
                    <div class="d-flex col-12 justify-content-center">
                        {{ $books->links() }}
                    </div>
                </div>
                @else
                <div class="row mt-3">
                    <div class='col-xl-6 col-lg-8 col-md-10 col-12 mx-auto text-center'>
                        <span>There are not books in this category.</span>
                    </div>
                </div>
                @endif
            </div>
        </div>
    </div>
</div>
@endsection
