<!--
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">Categories 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">
                <div class="row">
                    <form class="col-xl-6 col-lg-8 col-md-10 col-12 mx-auto" action="{{ route('categories.search') }}" method="POST">
                        @csrf
                        <input class="form-control" name="search" type="text" value="{{ isset($oldSearch) ? $oldSearch : '' }}" placeholder="Search by name"/>
                    </form>
                </div>
                @if (count($categories) !== 0)
                @foreach ($categories as $category)
                <div class="row mt-3">
                    <div class="col-xl-7 col-lg-10 col-12 mx-auto p-3 border border-secondary rounded">
                        <div class="container px-0">
                            <div class="row mb-3">
                                <span class="col-12 text-center fs-4">{{ $category->name }}</span>
                            </div>
                            <div class="row">
                                @Auth
                                @if (Auth::user()->hasRole('Administrator'))
                                <div class="col-md-4 col-12 pe-md-2">
                                    <div class="d-grid gap-2">
                                        <a href="{{ route('categories.show', $category->id) }}" class="btn btn-primary">Show</a>
                                    </div>
                                </div>
                                <div class="col-md-4 col-12 mt-md-0 mt-2 ps-md-2 pe-md-2">
                                    <div class="d-grid gap-2">
                                        <a href="{{ route('categories.edit', $category->id) }}" class="btn btn-warning">Edit</a>
                                    </div>
                                </div>
                                <form action="{{ route('categories.destroy', $category->id) }}" method="POST" class="col-md-4 col-12 mt-md-0 mt-2 ps-md-2" onsubmit="return window.confirm('Do you want to delete this category?')">
                                    @csrf
                                    @method('DELETE')
                                    <div class="d-grid gap-2">
                                        <button type="submit" class="btn btn-danger">Delete</button>
                                    </div>
                                </form>
                                @else
                                <div class="col-md-4 col-12 mx-auto">
                                    <div class="d-grid gap-2">
                                        <a href="{{ route('categories.show', $category->id) }}" class="btn btn-primary">Show</a>
                                    </div>
                                </div>
                                @endif
                                @else
                                <div class="col-md-4 col-12 mx-auto">
                                    <div class="d-grid gap-2">
                                        <a href="{{ route('categories.show', $category->id) }}" class="btn btn-primary">Show</a>
                                    </div>
                                </div>
                                @endauth
                            </div>
                        </div>
                    </div>
                </div>
                @endforeach
                <div class="row mt-3">
                    <div class="d-flex col-12 justify-content-center">
                        {{ $categories->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 categories.</span>
                    </div>
                </div>
                @endif
            </div>
        </div>
    </div>
</div>
@endsection
