<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Services\DummyDataService;
use Illuminate\Http\Request;

class BookController extends Controller
{
    public function index(Request $request)
    {
        $filters = $request->only(['search']);
        $semuaBuku = DummyDataService::getKatalogBuku($filters);

        // Memisahkan buku menjadi dua koleksi: online dan offline
        [$bukuOnline, $bukuOffline] = $semuaBuku->partition(function ($buku) {
            $tipe = $buku['tipe_akses'];
            return $tipe === 'online' || (is_array($tipe) && in_array('online', $tipe));
        });

        return view('admin.buku.index', [
            'pageTitle' => 'Manajemen Buku',
            'bukuOnline' => $bukuOnline,
            'bukuOffline' => $bukuOffline,
            'input' => $filters
        ]);
    }

    /**
     * Menampilkan halaman form untuk menambah buku baru.
     */
    public function create()
    {
        return view('admin.buku.create', [
            'pageTitle' => 'Tambah Buku Baru'
        ]);
    }

    public function edit($id)
{
    $buku = DummyDataService::getKatalogBuku()->firstWhere('id', (int)$id);
    abort_if(!$buku, 404);

    return view('admin.buku.edit', [
        'pageTitle' => 'Edit Buku: ' . $buku['judul'],
        'buku' => $buku
    ]);
}
}