@extends('layouts.admin.theme')

@section('title', 'Tábuas de Marés')

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

<div class="card shadow-sm border-0">
    <div class="card-header bg-info text-white d-flex justify-content-between align-items-center">
        <h5 class="my-2">
            <i class="fa fa-water"></i> Tábuas de Marés
        </h5>

        <a href="{{ route('admin.tabua.mares.create') }}" class="btn btn-success btn-sm">
            <i class="fa fa-upload"></i> Upload de Tábua
        </a>
    </div>

    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-hover table-bordered align-middle text-center">
                <thead class="table-dark">
                    <tr>
                        <th>Ano</th>
                        <th>Localidade</th>
                        <th>Nível Médio</th>
                        <th>Arquivo</th>
                        <th>Dias com Dados</th>
                        <th>Upload em</th>
                        <th>Ações</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($tabuasMares as $tabua)
                    <tr>
                        <td>
                            <span class="badge bg-secondary fs-6">{{ $tabua->ano ?? 'N/A' }}</span>
                        </td>
                        <td>
                            @if($tabua->localidade)
                                <small>{{ Str::limit($tabua->localidade, 30) }}</small>
                            @else
                                <span class="text-muted">N/A</span>
                            @endif
                        </td>
                        <td>
                            @if($tabua->nivel_medio)
                                <span class="badge bg-info">{{ $tabua->nivel_medio }} m</span>
                            @else
                                <span class="text-muted">N/A</span>
                            @endif
                        </td>
                        <td>
                            <small class="text-muted">
                                <i class="fa fa-file-pdf text-danger"></i>
                                {{ Str::limit($tabua->nome_arquivo_original, 20) ?? 'N/A' }}
                            </small>
                        </td>
                        <td>
                            @if(!empty($tabua->dados_completos))
                                @php
                                    $totalDias = 0;
                                    foreach ($tabua->dados_completos as $mes => $dias) {
                                        $totalDias += count($dias);
                                    }
                                @endphp
                                <span class="badge bg-success">
                                    {{ $totalDias }} dias
                                </span>
                            @else
                                <span class="badge bg-warning">Sem dados</span>
                            @endif
                        </td>
                        <td>
                            <small>{{ $tabua->created_at->format('d/m/Y H:i') }}</small>
                        </td>
                        <td>
                            <div class="d-flex justify-content-center gap-1">
                                <a href="{{ route('admin.tabua.mares.show', $tabua->id) }}" 
                                   class="btn btn-info btn-sm" title="Visualizar">
                                    <i class="fa fa-eye"></i>
                                </a>
                                
                                <a href="{{ route('admin.tabua.mares.download', $tabua->id) }}" 
                                   class="btn btn-primary btn-sm" title="Download PDF">
                                    <i class="fa fa-download"></i>
                                </a>

                                <a href="{{ route('admin.tabua.mares.edit', $tabua->id) }}"
                                   class="btn btn-warning btn-sm" title="Editar">
                                    <i class="fa fa-pen"></i>
                                </a>

                                <form action="{{ route('admin.tabua.mares.destroy', $tabua->id) }}"
                                      method="POST"
                                      onsubmit="return confirm('Deseja realmente excluir esta tábua de marés?')">
                                    @csrf
                                    @method('DELETE')
                                    <button class="btn btn-danger btn-sm" title="Excluir">
                                        <i class="fa fa-trash"></i>
                                    </button>
                                </form>
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="7" class="text-center text-muted py-4">
                            <i class="fa fa-info-circle fa-2x mb-3"></i><br>
                            Nenhuma tábua de marés cadastrada.<br>
                            <small>Clique em "Upload de Tábua" para adicionar a primeira.</small>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        <!-- Paginação -->
        <div class="d-flex justify-content-center mt-3">
            {!! $tabuasMares->links() !!}
        </div>

        <!-- Estatísticas -->
        @if($tabuasMares->count() > 0)
        <div class="row mt-4">
            <div class="col-md-12">
                <div class="card bg-light">
                    <div class="card-body py-2">
                        <div class="row text-center">
                            <div class="col-md-3">
                                <small class="text-muted">Total de Tábuas</small>
                                <h5 class="mb-0">{{ $tabuasMares->total() }}</h5>
                            </div>
                            <div class="col-md-3">
                                <small class="text-muted">Com Dados Estruturados</small>
                                <h5 class="mb-0 text-success">
                                    {{ $tabuasMares->whereNotNull('dados_completos')->count() }}
                                </h5>
                            </div>
                            <div class="col-md-3">
                                <small class="text-muted">Anos Diferentes</small>
                                <h5 class="mb-0 text-info">
                                    {{ $tabuasMares->pluck('ano')->unique()->filter()->count() }}
                                </h5>
                            </div>
                            <div class="col-md-3">
                                <small class="text-muted">Último Upload</small>
                                <h6 class="mb-0 text-primary">
                                    {{ $tabuasMares->first()->created_at->format('d/m/Y H:i') }}
                                </h6>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        @endif
    </div>
</div>
@endsection

@section('styles')
<style>
.table-hover tbody tr:hover {
    background-color: rgba(0, 123, 255, 0.05);
}
.badge {
    font-size: 0.75em;
}
</style>
@endsection