@extends('layouts.admin.theme')

@section('content')

    <div class="card shadow-sm">
        <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
            <h5 class="my-2">Gerenciador de URLS (Redirecionamentos)</h5>
        </div>

        <div class="card-body">
            @if (session('success'))
                <div class="row">
                    <div class="col-12 my-3">
                        <div class="alert alert-success text-center">{{ session('success') }}</div>
                    </div>
                </div>
            @endif
            @if (session('error'))
                <div class="row">
                    <div class="col-12 my-3">
                        <div class="alert alert-danger text-center">{{ session('error') }}</div>
                    </div>
                </div>
            @endif
            <div class="card shadow-sm rounded-4">
                <div class="card-header">
                    <h6 class="card-subtitle mb-2 text-body-secondary">
                        @include('components.buttons._newButton', ['url' => 'admin.urls.create', 'text' => 'Nova URL'])
                    </h6>
                </div>

                <div class="card-body">
                    @if(session('success'))
                        <div class="alert alert-success">{{ session('success') }}</div>
                    @endif

                    <div class="table-responsive">
                        <table class="table table-bordered table-hover table-striped align-middle text-center">
                            @include('components.tables._thead', ['thead' => ['Origem', 'Destino', 'Status', 'Ações']])
                            <tbody>
                                @forelse($urls as $url)
                                    <tr>
                                        <td>/{{ $url->source  }}</td>
                                        <td class="text-truncate" style="max-width: 300px;">{{ $url->destination }}</td>
                                        <td>
                                            <form action="{{ route('admin.urls.toggle', $url->id) }}" method="POST"
                                                class="d-inline">
                                                @csrf
                                                <button type="submit"
                                                    class="btn btn-sm btn-{{ $url->active ? 'success text-dark fw-bold' : 'secondary' }}">
                                                    {{ $url->active ? 'Ativo' : 'Inativo' }}
                                                </button>
                                            </form>
                                        </td>
                                        <td class="text-center">
                                            <div class="d-flex justify-content-center">
                                                <a href="{{ route('admin.urls.edit', $url->id) }}" class="m-2 btn btn-warning">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                                <form action="{{ route('admin.urls.destroy', $url->id) }}" method="POST"
                                                    class="d-inline">
                                                    @csrf
                                                    @method('DELETE')
                                                    <button type="submit" class="m-2 btn btn-danger"
                                                        onclick="return confirm('Tem certeza?')">
                                                        <i class="fas fa-trash"></i>
                                                    </button>
                                                </form>
                                            </div>
                                        </td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="4" class="text-center">Nenhuma URL cadastrada</td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-12 my-3">
                    <div class="d-flex justify-content-center">{!! $urls->links() !!} </div>
                </div>
            </div>
        </div>
    </div>
@endsection