@extends('layouts.admin')

@section('title', 'Riwayat')

@section('content')

<!-- CARD -->
<div class="card shadow-sm border-0 rounded-4">

    <div class="card-body">

        <!-- HEADER -->
        <div class="d-flex justify-content-between align-items-center mb-4 flex-wrap gap-2">

            <div>
                <h4 class="fw-bold mb-1">Tabel Riwayat Diagnosa</h4>

            </div>

            <div class="d-flex gap-2 flex-wrap">

                <!-- SEARCH -->
                <form method="GET" class="d-flex">

                    <input type="text"
                           name="search"
                           class="form-control form-control-sm"
                           placeholder="Cari pasien / penyakit..."
                           value="{{ request('search') }}">

                    <button class="btn btn-primary btn-sm ms-2">
                        Cari
                    </button>

                </form>

                <!-- SELECT DATA -->
                <form method="GET">

                    <input type="hidden"
                           name="search"
                           value="{{ request('search') }}">

                    <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>

        </div>

        <!-- TABLE -->
        <div class="table-responsive">

            <table class="table table-bordered align-middle table-hover">

                <thead class="table-light">
                    <tr>
                        <th width="5%">No</th>
                        <th>Nama</th>
                        <th>Penyakit</th>
                        <th>CF (%)</th>
                        <th>Tanggal</th>
                        <th width="20%">Aksi</th>
                    </tr>
                </thead>

                <tbody>

                    @forelse($data as $d)

                    <tr>

                        <td>
                            {{ $loop->iteration + ($data->currentPage() - 1) * $data->perPage() }}
                        </td>

                        <td class="fw-semibold">
                            {{ $d->pasien->nama ?? '-' }}
                        </td>

                        <td>
                            {{ $d->penyakit->nama }}
                        </td>

                        <td>
                            <span class="badge bg-success px-3 py-2">
                                {{ number_format($d->cf_hasil * 100, 2) }}%
                            </span>
                        </td>

                        <td>
                            {{ $d->created_at->format('d-m-Y') }}
                        </td>

                        <td>

                            <div class="d-flex gap-2 flex-wrap">

                                <a href="{{ route('diagnosa.hasil', $d->id) }}"
                                   class="btn btn-info btn-sm text-white">
                                    Preview
                                </a>

                                <a href="{{ route('diagnosa.pdf', $d->id) }}"
                                   class="btn btn-success btn-sm">
                                    Download PDF
                                </a>

                            </div>

                        </td>

                    </tr>

                    @empty

                    <tr>
                        <td colspan="6" class="text-center py-4">

                            <div class="alert alert-info mb-0">

                                @if(request('search'))
                                    Data tidak ditemukan
                                @else
                                    Belum ada data riwayat
                                @endif

                            </div>

                        </td>
                    </tr>

                    @endforelse

                </tbody>

            </table>

        </div>

    <!-- PAGINATION -->
    <div>
        {{ $data->links('pagination::bootstrap-5') }}
    </div>

</div>

    </div>

</div>

@endsection