@extends('layouts.admin')

@section('title', 'Data Pasien')

@section('content')

<div class="card shadow-sm">
    <div class="card-body">

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

            <div>
                <h4 class="fw-bold mb-1">Tabel Data Pasien</h4>
            </div>

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

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

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

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

                </form>

                <!-- PER PAGE -->
                <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 table-hover align-middle">

                <thead class="table-light">
                    <tr>
                        <th>No</th>
                        <th>Nama</th>
                        <th>Jenis Kelamin</th>
                        <th>Tanggal Lahir</th>
                        <th>Alamat</th>
                        <th>No Telp</th>
                        <th>Email</th>
                    </tr>
                </thead>

                <tbody>

                    @forelse($pasiens as $p)

                    <tr>

                        <td>{{ $loop->iteration + ($pasiens->currentPage() - 1) * $pasiens->perPage() }}</td>
                        <td>{{ $p->nama }}</td>
                        <td>{{ $p->jenis_kelamin }}</td>
                        <td>{{ \Carbon\Carbon::parse($p->tanggal_lahir)->format('d-m-Y') }}</td>
                        <td>{{ $p->alamat }}</td>
                        <td>{{ $p->no_telp }}</td>
                        <td>{{ $p->user->email ?? '-' }}</td>

                    </tr>

                    @empty

                    <tr>
                        <td colspan="5" class="text-center text-muted">

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

                        </td>
                    </tr>

                    @endforelse

                </tbody>

            </table>

        </div>

        <!-- PAGINATION -->
        <div class="mt-3">
            {{ $pasiens->links('pagination::bootstrap-5') }}
        </div>

    </div>
</div>

@endsection