<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use App\Models\RuangBacaModel;

class RuangBaca extends BaseController
{
    protected $ruangBaca;

    public function __construct()
    {
        $this->ruangBaca = new RuangBacaModel();
    }

    public function index()
    {
        $data['ruang_baca'] = $this->ruangBaca->findAll();

        return view('ruang_baca/index', $data);
    }

    public function create()
    {
        return view('ruang_baca/create');
    }

    public function store()
    {
        $file = $this->request->getFile('file_pdf');

        $namaFile = '';

        if ($file && $file->isValid() && !$file->hasMoved()) {

            $namaFile = $file->getRandomName();

            $file->move(FCPATH . 'uploads/ruang_baca', $namaFile);
        }

        $this->ruangBaca->save([
            'judul'    => $this->request->getPost('judul'),
            'penulis'  => $this->request->getPost('penulis'),
            'kategori' => $this->request->getPost('kategori'),
            'jenis'    => $this->request->getPost('jenis'),
            'file_pdf' => $namaFile,
        ]);

        return redirect()->to('/ruang_baca')
            ->with('success', 'Data Ruang Baca berhasil ditambahkan.');
    }

    public function edit($id)
    {
        $ruangBaca = $this->ruangBaca->find($id);

        if (!$ruangBaca) {
            throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
        }

        return view('ruang_baca/edit', [
            'ruang_baca' => $ruangBaca
        ]);
    }

    public function update($id)
    {
        $ruangBaca = $this->ruangBaca->find($id);

        if (!$ruangBaca) {
            throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
        }

        $data = [
            'judul'    => $this->request->getPost('judul'),
            'penulis'  => $this->request->getPost('penulis'),
            'kategori' => $this->request->getPost('kategori'),
            'jenis'    => $this->request->getPost('jenis'),
        ];

        $file = $this->request->getFile('file_pdf');

        if ($file && $file->isValid() && !$file->hasMoved()) {

            // Hapus file lama
            if (!empty($ruangBaca['file_pdf']) && file_exists(FCPATH . 'uploads/ruang_baca/' . $ruangBaca['file_pdf'])) {
                unlink(FCPATH . 'uploads/ruang_baca/' . $ruangBaca['file_pdf']);
            }

            // Upload file baru
            $namaFile = $file->getRandomName();
            $file->move(FCPATH . 'uploads/ruang_baca', $namaFile);

            $data['file_pdf'] = $namaFile;
        }

        $this->ruangBaca->update($id, $data);

        return redirect()->to('/ruang_baca')
            ->with('success', 'Data Ruang Baca berhasil diubah.');
    }

    public function delete($id)
    {
        $ruangBaca = $this->ruangBaca->find($id);

        if ($ruangBaca) {

            if (!empty($ruangBaca['file_pdf']) && file_exists(FCPATH . 'uploads/ruang_baca/' . $ruangBaca['file_pdf'])) {
                unlink(FCPATH . 'uploads/ruang_baca/' . $ruangBaca['file_pdf']);
            }

            $this->ruangBaca->delete($id);
        }

        return redirect()->to('/ruang_baca')
            ->with('success', 'Data Ruang Baca berhasil dihapus.');
    }

    public function download($id)
    {
        $ruangBaca = $this->ruangBaca->find($id);

        if (!$ruangBaca || empty($ruangBaca['file_pdf'])) {
            throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
        }

        $path = FCPATH . 'uploads/ruang_baca/' . $ruangBaca['file_pdf'];

        if (!file_exists($path)) {
            throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
        }

        return $this->response->download($path, null);
    }
}
