@extends('layouts.user')

@section('title', 'Dashboard')

@section('content')

<!-- HERO -->
<div class="card-hero mb-4">
    <h2>Selamat Datang, {{ auth()->user()->name }} </h2>
    <p>
        Pantau riwayat diagnosa dan lakukan pemeriksaan kesehatan secara mandiri.
    </p>

    <a href="{{ url('/user/diagnosa') }}" class="btn btn-light mt-2">
        Mulai Diagnosa
    </a>
</div>

<!-- STATISTIK -->
<div class="row mb-4">

    <div class="col-md-4">
        <div class="card p-3">
            <h6>Total Diagnosa</h6>
            <h3>{{ $totalDiagnosa }}</h3>
        </div>
    </div>

    <div class="col-md-4">
        <div class="card p-3">
            <h6>Diagnosa Terakhir</h6>
            <h5>
                {{ $lastDiagnosa->penyakit->nama ?? '-' }}
            </h5>
        </div>
    </div>
    

    <div class="col-md-4">
        <div class="card p-3">
            <h6>Nilai CF</h6>
            <h5>
                {{ isset($lastDiagnosa) ? number_format($lastDiagnosa->cf_hasil,2) : '-' }}
            </h5>
        </div>
    </div>

</div>

<div class="d-flex justify-content-between align-items-center mb-3">

    <h5 class="mb-0">Riwayat Diagnosa</h5>

    <form method="GET">
        <select name="perPage" onchange="this.form.submit()" class="form-select form-select-sm">
            <option value="5" {{ $perPage == 5 ? 'selected' : '' }}>5</option>
            <option value="10" {{ $perPage == 10 ? 'selected' : '' }}>10</option>
            <option value="25" {{ $perPage == 25 ? 'selected' : '' }}>25</option>
            <option value="50" {{ $perPage == 50 ? 'selected' : '' }}>50</option>
        </select>
    </form>

</div>

<!-- RIWAYAT TERAKHIR -->
<div class="card p-3">

    <div class="table-responsive">
        <table class="table table-hover align-middle">

            <thead class="table-light">
                <tr>
                    <th>No</th>
                    <th>Tanggal</th>
                    <th>Penyakit</th>
                    <th>CF</th>
                    
                </tr>
            </thead>

            <tbody>
                @forelse($riwayat as $r)
                <tr>
                    <td>{{ $loop->iteration + ($riwayat->currentPage() - 1) * $riwayat->perPage() }}</td>
                    <td>{{ $r->created_at->format('d-m-Y') }}</td>
                    <td>{{ $r->penyakit->nama ?? '-' }}</td>
                    <td>{{ number_format($r->cf_hasil, 2) }}</td>
 
                </tr>
                @empty
                <tr>
                    <td colspan="5" class="text-center text-muted">
                        Belum ada riwayat diagnosa
                    </td>
                </tr>
                @endforelse
            </tbody>

        </table>
    </div>

    <!-- PAGINATION -->
    <div class="mt-3">
        {{ $riwayat->links() }}
    </div>

</div>
<a href="{{ url('/user/riwayat') }}" class="btn btn-primary btn-sm mt-2">
    Lihat Semua Riwayat
</a>

@endsection