<!--
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">Penalties 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
    <div class="row">
        <div class="col-lg-10 col-12 mx-auto">
            <div class="container">
                @if (Auth::user()->hasRole('Administrator'))
                <div class="row">
                    <form class="col-xl-6 col-lg-8 col-md-10 col-12 mx-auto" action="{{ route('penalties.search') }}" method="POST">
                        @csrf
                        <input class="form-control" name="search" type="text" value="{{ isset($oldSearch) ? $oldSearch : '' }}" placeholder="Search by user name and email"/>
                    </form>
                </div>
                @endif
                @if (count($penalties) !== 0)
                @if (Auth::user()->hasRole('Administrator'))
                <div class="row mt-3 p-3 d-md-flex d-none">
                @else
                <div class="row p-3 d-md-flex d-none">
                @endif
                    <div class='col-3 my-auto text-center'>
                        <span class="fs-5">User</span>
                    </div>
                    <div class="col my-auto">
                    </div>
                    <div class='col-3 my-auto text-center'>
                        <span class="fs-5">End</span>
                    </div>
                    <div class="col my-auto">
                    </div>
                    <div class='col-3 my-auto text-center'>
                        <span class="fs-5">Actions</span>
                    </div>
                </div>
                @foreach ($penalties as $penalty)
                <div class="row mt-3 p-3 border border-secondary rounded">
                    <div class='col-md-3 col-12 my-md-auto mb-3 text-center'>
                        <span class="fs-5">{{ $penalty->user->name }}</span>
                    </div>
                    <div class="col my-md-auto my-0">
                    </div>
                    <div class='col-md-3 col-12 my-md-auto mb-3 text-center'>
                        <span>{{ \Carbon\Carbon::parse($penalty->end)->format('d/m/Y') }}</span>
                    </div>
                    <div class="col my-md-auto my-0">
                    </div>
                    <div class='col-md-3 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('penalties.show', $penalty->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('penalties.edit', $penalty->id) }}" class="btn btn-warning">Edit</a>
                                    </div>
                                </div>                   
                            </div>
                            <div class="row mt-2">
                                <form action="{{ route('penalties.destroy', $penalty->id) }}" method="POST" class="col-12" onsubmit="return window.confirm('Do you want to remove this penalty?')">
                                    @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">
                        {{ $penalties->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 penalties.</span>
                    </div>
                </div>
                @endif
            </div>
        </div>
    </div>
</div>
@endsection
