@extends('layouts.admin.theme')

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

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

<div class="container-fluid px-3">

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

    <div class="card shadow-sm border-0 rounded-3">
        @component('components.buttons._headLineButtonsBackAdd', [
            'headLine' => "<i class='fa fa-water me-2'></i> " . __('Tábua de Marés'),
            'routeBack' => 'admin.boletim.meteorologico.index',
            'nameBack' => __('Voltar'),
            'routeAdd' => $canAdd ? 'admin.tabua.index' : null,
            'nameAdd' => null,
            'permission' => 'GERENCIADOR_DE_BOLETINS_METEOROLOGICOS',
        ])
        @endcomponent

        <div class="card-body">
            {{-- UPLOAD DE PDF --}}
            <div class="card border-0 shadow-sm mb-4">
                <div class="card-header bg-secondary text-white py-2 text-center">
                    <h5 class="mb-0"><i class="fa fa-upload me-2"></i>{{ __('Carregar Nova Tábua') }}</h5>
                </div>
                <div class="card-body">
                    <form action="{{ route('admin.tabua.upload') }}" method="POST" enctype="multipart/form-data">
                        @csrf
                        <div class="row g-3 align-items-end">
                            <div class="col-12 col-xxl-3 col-xl-3 col-lg-3 col-md-12 col-sm-12">
                                <label class="form-label fw-semibold">{{ __('Arquivo PDF') }}</label>
                                <input type="file" name="pdf" accept="application/pdf"
                                    class="form-control @error('pdf') is-invalid @enderror" required {{ $canAdd ? '' : 'disabled' }}>
                                @error('pdf')
                                <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                            <div class="col-12 col-xxl-3 col-xl-3 col-lg-3 col-md-12 col-sm-12">
                                <label class="form-label fw-semibold">{{ __('Ano da Tábua') }}</label>
                                <input type="number" name="ano" class="form-control @error('ano') is-invalid @enderror"
                                    min="1900" max="2100" value="{{ old('ano', date('Y')) }}" required {{ $canAdd ? '' : 'disabled' }}>
                                @error('ano')
                                <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                            <div class="col-12 col-xxl-3 col-xl-3 col-lg-3 col-md-12 col-sm-12">
                                @if($canAdd)
                                    <button type="submit" class="btn btn-primary w-100">
                                        <i class="fa fa-upload me-1"></i> {{ __('Enviar') }}
                                    </button>
                                @else
                                    <button type="button" class="btn btn-secondary w-100" disabled>
                                        <i class="fa fa-lock me-1"></i> {{ __('Sem permissão') }}
                                    </button>
                                @endif
                            </div>
                        </div>
                    </form>
                </div>
            </div>

            {{-- CONSULTA --}}
            <div class="card border-0 shadow-sm mb-4">
                <div class="card-header bg-light py-2 text-center">
                    <h5 class="mb-0"><i class="fa fa-calendar me-2"></i>{{ __('Consultar Tábua') }}</h5>
                </div>
                <div class="card-body">
                    <form action="{{ route('admin.tabua.index') }}" method="GET" class="row g-3 align-items-end">
                        <div class="col-12 col-xxl-3 col-xl-3 col-lg-3 col-md-12 col-sm-12">
                            <label class="form-label fw-semibold">{{ __('Selecione a Data') }}</label>
                            <input type="date" name="data" id="data"
                                class="form-control"
                                value="{{ $dataSelecionada }}">

                        </div>
                        <div class="col-12 col-xxl-3 col-xl-3 col-lg-3 col-md-12 col-sm-12 d-flex justify-content-around">
                            <button type="submit" class="btn btn-secondary me-2">
                                <i class="fa fa-search me-1"></i> {{ __('Buscar') }}
                            </button>
                        </div>
                        <div class="col-12 col-xxl-3 col-xl-3 col-lg-3 col-md-12 col-sm-12">
                            <a href="{{ route('admin.tabua.index') }}" class="btn btn-outline-secondary">
                                <i class="fa fa-refresh me-1"></i> {{ __('Hoje') }}
                            </a>
                        </div>
                    </form>
                </div>
            </div>


            {{-- RESULTADO --}}
            <div class="row g-2 d-flex justify-content-center">
                {{-- DIA ANTERIOR --}}
                <div class="col-12 col-md-3">
                    @if($tabuaTresDias['anterior']['dados'] && count($tabuaTresDias['anterior']['dados']))
                    <div class="card shadow-sm border-2 border-info">
                        <div class="card-header bg-info text-white d-flex justify-content-between align-items-center py-2">
                            <strong>
                                <i class="fa fa-list me-2"></i>
                                {{ $tabuaTresDias['anterior']['dataFormatada'] }}
                            </strong>
                            <span class="badge bg-light text-dark">
                                {{ count($tabuaTresDias['anterior']['dados']) }} registro(s)
                            </span>
                        </div>
                        <div class="card-body p-0">
                            <div class="table-responsive">
                                <table class="table table-striped align-middle mb-0">
                                    <thead class="table-light">
                                        <tr>
                                            <th class="text-center">Hora</th>
                                            <th class="text-center">Altura (m)</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach($tabuaTresDias['anterior']['dados'] as $m)
                                        <tr>
                                            <td class="text-center fw-bold">{{ $m['hora'] }}</td>
                                            <td class="text-center">{{ number_format($m['altura'], 2, ',', '.') }} m</td>
                                        </tr>
                                        @endforeach
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    @else
                    <div class="alert alert-warning text-center">
                        <i class="fa fa-exclamation-triangle me-2"></i>
                        Nenhuma tábua encontrada para
                        <strong>{{ $tabuaTresDias['anterior']['dataFormatada'] }}</strong>.
                    </div>
                    @endif
                </div>

                {{-- DIA ATUAL --}}
                <div class="col-12 col-md-3">
                    @if($tabuaTresDias['atual']['dados'] && count($tabuaTresDias['atual']['dados']))
                    <div class="card shadow-sm border-2 border-success">
                        <div class="card-header bg-success text-white d-flex justify-content-between align-items-center py-2">
                            <strong>
                                <i class="fa fa-list me-2"></i>
                                {{ $tabuaTresDias['atual']['dataFormatada'] }}
                            </strong>
                            <span class="badge bg-light text-dark">
                                {{ count($tabuaTresDias['atual']['dados']) }} registro(s)
                            </span>
                        </div>
                        <div class="card-body p-0">
                            <div class="table-responsive">
                                <table class="table table-striped align-middle mb-0">
                                    <thead class="table-light">
                                        <tr>
                                            <th class="text-center">Hora</th>
                                            <th class="text-center">Altura (m)</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach($tabuaTresDias['atual']['dados'] as $m)
                                        <tr>
                                            <td class="text-center fw-bold">{{ $m['hora'] }}</td>
                                            <td class="text-center">{{ number_format($m['altura'], 2, ',', '.') }} m</td>
                                        </tr>
                                        @endforeach
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    @else
                    <div class="alert alert-danger text-center">
                        <i class="fa fa-exclamation-triangle me-2"></i>
                        Nenhuma tábua encontrada para
                        <strong>{{ $tabuaTresDias['atual']['dataFormatada'] }}</strong>.
                    </div>
                    @endif
                </div>

                {{-- DIA SEGUINTE --}}
                <div class="col-12 col-md-3">
                    @if($tabuaTresDias['seguinte']['dados'] && count($tabuaTresDias['seguinte']['dados']))
                    <div class="card shadow-sm border-2 border-primary">
                        <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center py-2">
                            <strong>
                                <i class="fa fa-list me-2"></i>
                                {{ $tabuaTresDias['seguinte']['dataFormatada'] }}
                            </strong>
                            <span class="badge bg-light text-dark">
                                {{ count($tabuaTresDias['seguinte']['dados']) }} registro(s)
                            </span>
                        </div>
                        <div class="card-body p-0">
                            <div class="table-responsive">
                                <table class="table table-striped align-middle mb-0">
                                    <thead class="table-light">
                                        <tr>
                                            <th class="text-center">Hora</th>
                                            <th class="text-center">Altura (m)</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach($tabuaTresDias['seguinte']['dados'] as $m)
                                        <tr>
                                            <td class="text-center fw-bold">{{ $m['hora'] }}</td>
                                            <td class="text-center">{{ number_format($m['altura'], 2, ',', '.') }} m</td>
                                        </tr>
                                        @endforeach
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    @else
                    <div class="alert alert-warning text-center">
                        <i class="fa fa-exclamation-triangle me-2"></i>
                        Nenhuma tábua encontrada para
                        <strong>{{ $tabuaTresDias['seguinte']['dataFormatada'] }}</strong>.
                    </div>
                    @endif
                </div>
            </div>

            {{-- ANOS DISPONÍVEIS --}}
            @if(!empty($anosDisponiveis))
            <div class="mx-auto col col-sm-12 col-md-12 col-lg-12 col-xl-6 col-xxl-6 alert alert-info mt-4 mb-0">
                <h6 class="fw-semibold mb-2">
                    <i class="fa fa-info-circle me-2"></i>Anos disponíveis:
                </h6>
                <div class="d-flex flex-wrap gap-2">
                    @foreach($anosDisponiveis as $ano)
                    <span class="badge bg-primary px-3 py-2">{{ $ano }}</span>
                    @endforeach
                </div>
            </div>
            @endif
        </div>
    </div>
</div>
@endsection

@push('styles')
<style>
    .card-header h4,
    .card-header h5 {
        margin: 0;
    }

    .card {
        border-radius: 10px;
    }

    .table th {
        border-top: none;
        font-weight: 600;
    }

    .btn {
        border-radius: 6px;
    }

    .alert {
        border-radius: 8px;
    }
</style>
@endpush
