@extends('layouts.admin')

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

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

            <!-- BUTTON -->
            <a href="{{ route('penyakit.create') }}"
               class="btn btn-primary">
               + Tambah Penyakit
            </a>

            <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 penyakit..."
                           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>

        <!-- ALERT -->
        @if(session('success'))
            <div class="alert alert-success">
                {{ session('success') }}
            </div>
        @endif

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

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

                <thead class="table-light">
                    <tr>
                        <th width="70">No</th>
                        <th width="120">Kode</th>
                        <th>Nama Penyakit</th>
                        <th width="180">Aksi</th>
                    </tr>
                </thead>

                <tbody>

                    @forelse($penyakits as $p)

                    <tr>

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

                        <td>{{ $p->kode }}</td>

                        <td>{{ $p->nama }}</td>

                        <td>

                            <a href="{{ route('penyakit.edit', $p->id) }}"
                               class="btn btn-warning btn-sm">
                               Edit
                            </a>

                            <form action="{{ route('penyakit.destroy', $p->id) }}"
                                  method="POST"
                                  style="display:inline;">

                                @csrf
                                @method('DELETE')

                                <button class="btn btn-danger btn-sm"
                                        onclick="return confirm('Yakin ingin menghapus data ini?')">
                                    Hapus
                                </button>

                            </form>

                        </td>

                    </tr>

                    @empty

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

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

                        </td>
                    </tr>

                    @endforelse

                </tbody>

            </table>

        </div>

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

    </div>
</div>

@endsection