@extends('layouts.admin.theme')

@section('title', 'Lista - Sobreavisos CEMADEN')

@section('content')
@include('errors.messageFlash')

@php
    $perms = auth()->user()?->getPermissaoDetalhes('GERENCIADOR_DE_BOLETINS_METEOROLOGICOS') ?? [];
    $canAdd = $perms['adicionar'] ?? false;
    $canEdit = $perms['editar'] ?? false;
    $canDelete = $perms['excluir'] ?? false;
@endphp

<div class="card shadow-sm border-0">
    @component('components.buttons._headLineButtonsBackAdd', [
        'headLine' => "<i class='fa fa-bell me-2'></i> " . __('Agentes de Sobreaviso CEMADEN'),
        'routeBack' => 'admin.boletim.meteorologico.index',
        'nameBack' => __('Voltar'),
        'routeAdd' => $canAdd ? 'admin.sobreavisos.create' : null,
        'nameAdd' => __('Novo agente de sobreaviso'),
        'permission' => 'GERENCIADOR_DE_BOLETINS_METEOROLOGICOS',
    ])
    @endcomponent

    <div class="card-body">
        {{-- Componente de busca --}}
        @include('components.buttons._searchAndClean', [
        'url' => 'admin.sobreavisos.index',
        'search' => 'busca',
        'label1' => 'Buscar',
        'label2' => 'Limpar',
        'placeholder' => 'Buscar sobreavisos ...'
        ])

        <x-tables._table :thead="[
            'ID',
            __('Nome'),
            __('Criado em'),
            __('Atualizado em'),
            __('Ações'),
        ]" :pagination="$sobreavisos->links()">
            <x-slot name="body">
                @forelse($sobreavisos as $sobreaviso)
                    <tr>
                        <td>{{ $sobreaviso->id }}</td>
                        <td>{{ $sobreaviso->nome }}</td>
                        <td>{{ $sobreaviso->created_at->format('d/m/Y H:i') }}</td>
                        <td>{{ $sobreaviso->updated_at->format('d/m/Y H:i') }}</td>
                        <td>
                            @if($canEdit || $canDelete)
                                <div class="d-flex justify-content-center gap-1">
                                    @if($canEdit)
                                        <a href="{{ route('admin.sobreavisos.edit', $sobreaviso->id) }}"
                                            class="btn btn-warning btn-sm" title="{{ __('Editar') }}">
                                            <i class="fa fa-pen"></i>
                                        </a>
                                    @endif
                                    @if($canDelete)
                                        <form action="{{ route('admin.sobreavisos.destroy', $sobreaviso->id) }}" method="POST"
                                            onsubmit="return confirm('{{ __('Deseja realmente excluir este sobreaviso?') }}')">
                                            @csrf
                                            @method('DELETE')
                                            <button class="btn btn-danger btn-sm" title="{{ __('Excluir') }}">
                                                <i class="fa fa-trash"></i>
                                            </button>
                                        </form>
                                    @endif
                                </div>
                            @else
                                <span class="text-muted">—</span>
                            @endif
                        </td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="5" class="text-center text-muted">
                            <i class="fa fa-info-circle"></i> {{ __('Nenhum agente de sobreaviso encontrado') }}
                        </td>
                    </tr>
                @endforelse
            </x-slot>
        </x-tables._table>
    </div>
</div>
@endsection