<?php

namespace App\Http\Controllers;

use App\Models\Buku;
use Illuminate\Http\Request;
use Barryvdh\DomPDF\Facade\Pdf;

class BukuController extends Controller
{
    public function index()
    {
        $bukus = Buku::orderBy('judul')->paginate(10);

        return view('buku.index', compact('bukus'));
    }

    public function show(Buku $buku)
{
    return view('buku.show', compact('buku'));
}


    public function create()
    {
        return view('buku.create');
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'judul'        => 'required|string|max:255',
            'pengarang'    => 'required|string|max:255',
            'penerbit'     => 'required|string|max:255',
            'tahun_terbit' => 'required|digits:4',
            'stok'         => 'required|integer|min:0',
        ]);

        Buku::create($validated);

        return redirect()
            ->route('admin.buku.index')
            ->with('success', 'Data buku berhasil ditambahkan');
    }

    public function edit(Buku $buku)
    {
        return view('buku.edit', compact('buku'));
    }

    public function update(Request $request, Buku $buku)
    {
        $validated = $request->validate([
            'judul'        => 'required|string|max:255',
            'pengarang'    => 'required|string|max:255',
            'penerbit'     => 'required|string|max:255',
            'tahun_terbit' => 'required|digits:4',
            'stok'         => 'required|integer|min:0',
        ]);

        $buku->update($validated);

        return redirect()
            ->route('admin.buku.index')
            ->with('success', 'Data buku berhasil diperbarui');
    }

    public function destroy(Buku $buku)
    {
        $buku->delete();

        return redirect()
            ->route('admin.buku.index')
            ->with('success', 'Data buku berhasil dihapus');
    }

    public function cetakPDF()
    {
        $bukus = Buku::orderBy('judul')->get();

        $pdf = Pdf::loadView('buku.laporan', compact('bukus'))
                  ->setPaper('a4', 'portrait');

        return $pdf->download('laporan_buku.pdf');
    }
}
