@extends('layouts.admin')

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

@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 TAMBAH -->
            <a href="{{ route('gejala.create') }}" class="btn btn-primary">
                + Tambah Gejala
            </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 gejala..."
                           value="{{ request('search') }}">

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

                </form>

                <!-- PER PAGE -->
                <form method="GET">

                    <!-- AGAR SEARCH TETAP ADA -->
                    <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</th>
                        <th width="180">Aksi</th>
                    </tr>
                </thead>

                <tbody>

                    @forelse($gejalas as $g)

                    <tr>

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

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

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

                        <td>

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

                            <form action="{{ route('gejala.destroy', $g->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 gejala tidak ditemukan
                            @else
                                Belum ada data gejala
                            @endif

                        </td>
                    </tr>

                    @endforelse

                </tbody>

            </table>

        </div>

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

    </div>
</div>

@endsection