@extends('layouts.app')

@section('title', 'Data Hasil Uji ISPA')

@section('content')
<div class="container">
    <h1 class="mb-3">Data Hasil Uji</h1>

    <!-- Card yang menampung Search, Export, dan Table -->
    <div class="card shadow-sm">
        <div class="card-body py-3">
            <!-- Baris Search dan Export dengan Flexbox -->
            <div class="d-flex mb-3">
                <!-- Form Search, fleksibel agar mengisi ruang -->
                <form action="{{ route('admin.prediksi.index') }}" method="GET" class="flex-grow-1 me-2">
                    <div class="input-group">
                        <input type="text" name="search" class="form-control" placeholder="Cari hasil prediksi..." value="{{ request('search') }}">
                        <button class="btn btn-primary" type="submit">Cari</button>
                    </div>
                </form>

                <!-- Tombol Export Excel -->
                <a href="{{ route('admin.prediksi.export') }}" class="btn btn-success">Export Excel</a>
            </div>

            <!-- Data Table -->
            <div class="table-responsive">
                <table class="table table-striped mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>Kecamatan</th>
                            <th>Data Tahun</th>
                            <th>PHBS</th>
                            <th>Imunisasi</th>
                            <th>Merokok</th>
                            <th>Jumlah Kasus</th>
                            <th>Latitude</th>
                            <th>Longitude</th>
                            <th>Prediksi Tahun</th>
                            <th>Status Prediksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($dataPrediksi as $data)
                        <tr>
                        <td>{{ $data->kecamatan }}</td>
                        <td>{{ $data->data_tahun }}</td>
                        <td>{{ $data->phbs }}</td>
                        <td>{{ $data->imunisasi ?? $data->rata_rata_imunisasi }}</td>
                        <td>{{ $data->merokok ?? $data->perilaku_merokok }}</td>
                        <td>{{ $data->jumlah_kasus ?? $data->data_kasus }}</td>
                        <td>{{ $data->latitude }}</td>
                        <td>{{ $data->longitude }}</td>
                        <td>
                            @if(isset($data->data_tahun))
                                @if(in_array((int)$data->data_tahun, [2023, 2024]))
                                    2025
                                @else
                                    {{ (int)$data->data_tahun + 1 }}
                                @endif
                            @else
                                -
                            @endif
                        </td>
                        <td>
                            <span class="badge {{ $data->status_prediksi == 'Naik' ? 'bg-danger' : 'bg-success' }}">
                                {{ $data->status_prediksi }}
                            </span>
                        </td>
                    </tr>

                        @empty
                        <tr>
                            <td colspan="11" class="text-center text-muted">Data tidak ditemukan</td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Pagination -->
    <div class="mt-4 d-flex justify-content-center">
        {{ $dataPrediksi->links('pagination::bootstrap-5') }}
    </div>
</div>
@endsection
