@extends('layouts.admin')

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

@section('content')

<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>
                <h5 class="mb-0 fw-bold">Tabel Data Edukasi</h5>
                <small class="text-muted">
                    Kelola informasi edukasi kesehatan
                </small>
            </div>

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

                <!-- SEARCH -->
                <form method="GET"
                      action="{{ route('edukasi.index') }}"
                      class="d-flex">

                    <input type="text"
                           name="search"
                           class="form-control form-control-sm"
                           placeholder="Cari edukasi..."
                           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>

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

            </div>

        </div>

        @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="5%">No</th>
                        <th>Judul</th>
                        <th width="25%">Aksi</th>
                    </tr>
                </thead>

                <tbody>

                    @forelse($edukasis as $e)

                    <tr>

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

                        <td class="fw-semibold">
                            {{ $e->judul }}
                        </td>

                        <td>

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

                                <a href="{{ route('edukasi.show', $e->id) }}"
                                   class="btn btn-info btn-sm text-white">
                                    Detail
                                </a>

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

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

                                    @csrf
                                    @method('DELETE')

                                    <button class="btn btn-danger btn-sm">
                                        Hapus
                                    </button>

                                </form>

                            </div>

                        </td>

                    </tr>

                    @empty

                    <!-- PESAN JIKA KOSONG -->
                    <tr>

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

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

                                @if(request('search'))
                                    Edukasi tidak ditemukan.
                                @else
                                    Belum ada data edukasi.
                                @endif

                            </div>

                        </td>

                    </tr>

                    @endforelse

                </tbody>

            </table>

        </div>

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

    </div>

</div>

@endsection