<!--
Copyright (C) 2021 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">Loans list</h1>
    </div>
    @if (Session::get('success'))
    @php $message = Session::get('success'); @endphp
    <div class = "row mb-3">
        <span class="col-xl-6 col-lg-8 col-md-10 col-12 mx-auto mb-0 alert alert-success">{{ $message }}</span>
    </div>
    @endif
    @if (Session::get('danger'))
    @php $message = Session::get('danger'); @endphp
    <div class = "row mb-3">
        <span class="col-xl-6 col-lg-8 col-md-10 col-12 mx-auto mb-0 alert alert-danger">{{ $message }}</span>
    </div>
    @endif
    <div class="row">
        <div class="col-lg-10 col-12 mx-auto">
            <div class="container">
                <div class="row">
                    <form class="col-xl-6 col-lg-8 col-md-10 col-12 mx-auto" action="{{ route('loans.search') }}" method="POST">
                        @csrf
                        
                        @if (Auth::user()->hasRole('Administrator'))
                        <input class="form-control" name="search" type="text" value="{{ isset($oldSearch) ? $oldSearch : '' }}" placeholder="Search by user name and email, or by book title, authors and editorial"/>
                        @else
                        <input class="form-control" name="search" type="text" value="{{ isset($oldSearch) ? $oldSearch : '' }}" placeholder="Search by book title, authors and editorial"/>
                        @endif
                    </form>
                </div>
                @if (count($loans) !== 0)
                <div class="row mt-3 p-3 d-md-flex d-none">
                    <div class='col-2 my-auto text-center'>
                        <span class="fs-5">User</span>
                    </div>
                    <div class='col-2 my-auto text-center'>
                        <span class="fs-5">Book title</span>
                    </div>
                    <div class='col-2 my-auto text-center'>
                        <span class="fs-5">Book authors</span>
                    </div>
                    <div class='col-2 my-auto text-center'>
                        <span class="fs-5">Start</span>
                    </div>
                    <div class='col-2 my-auto text-center'>
                        <span class="fs-5">End</span>
                    </div>
                    <div class='col-2 my-auto text-center'>
                        <span class="fs-5">Actions</span>
                    </div>
                </div>
                @foreach ($loans as $loan)
                <div class="row mt-3 p-3 border border-secondary rounded">
                    <div class='col-md-2 col-12 my-md-auto mb-3 text-center'>
                        <span class="fs-5">{{ $loan->user->name }}</span>
                    </div>
                    <div class='col-md-2 col-12 my-md-auto mb-3 text-center'>
                        <span class="fs-5">{{ $loan->book->title }}</span>
                    </div>
                    <div class='col-md-2 col-12 my-md-auto mb-3 text-center'>
                        <span>{{ $loan->book->authors }}</span>
                    </div>
                    <div class='col-md-2 col-12 my-md-auto mb-3 text-center'>
                        <span>{{ \Carbon\Carbon::parse($loan->start)->format('d/m/Y') }}</span>
                    </div>
                    <div class='col-md-2 col-12 my-md-auto mb-3 text-center'>
                        <span>{{ $loan->end ? \Carbon\Carbon::parse($loan->end)->format('d/m/Y') : 'Undelivered'  }}</span>
                    </div>
                    <div class='col-md-2 col-12 px-0'>
                        <div class="container px-0">
                            <div class="row">
                                <div class="col-12">
                                    <div class="d-grid gap-2">
                                        <a href="{{ route('loans.show', $loan->id) }}" class="btn btn-primary">Show</a>
                                    </div>
                                </div>                      
                            </div>
                            @if (Auth::user()->hasRole('Administrator'))
                            <div class="row mt-2">
                                <div class="col-12">
                                    <div class="d-grid gap-2">
                                        <a href="{{ route('loans.edit', $loan->id) }}" class="btn btn-warning">Edit</a>
                                    </div>
                                </div>                   
                            </div>
                            <div class="row mt-2">
                                <form action="{{ route('loans.destroy', $loan->id) }}" method="POST" class="col-12" onsubmit="return window.confirm('Do you want to remove this loan?')">
                                    @csrf
                                    @method('DELETE')
                                    <div class="d-grid gap-2">
                                        <button type="submit" class="btn btn-danger">Delete</button>
                                    </div>
                                </form>                    
                            </div>
                            @endif
                        </div>
                    </div>
                </div>
                @endforeach
                <div class="row mt-3">
                    <div class="d-flex col-12 justify-content-center">
                        {{ $loans->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 loans.</span>
                    </div>
                </div>
                @endif
            </div>
        </div>
    </div>
</div>
@endsection
