<?php

namespace App\Controllers\admin;

use App\Models\UserModel;
use App\Models\GalleryModel;
use App\Controllers\BaseController;


class GalleryController extends BaseController
{
    public function index()
    {
        $galleryModel = new GalleryModel();
        $userModel = new UserModel();

        // Ambil semua data gallery
        $galleryData = $galleryModel->findAll();

        // Ambil semua data user
        $userData = $userModel->findAll();

        // Tampilkan view dengan data gallery dan user
        return view('admin/gallery/index', [
            'galleryData' => $galleryData,
            'userData' => $userData,
            'aktif' => 'gallery'
        ]);
    }
    public function create()
    {

        $userModel = new UserModel();
        $aktif = 'gallery';


        // Ambil semua data mata pelajaran
        $userData = $userModel->findAll();
        return view('admin/gallery/add', [
            'aktif' => $aktif,
            'userData' => $userData
        ]);
    }
    public function store()
    {

        //validasi 
        $validationRules = [
            'gambar' => 'uploaded[gambar]|max_size[gambar,1024]|is_image[gambar]',
            'judul' => 'required',
            'keterangan' => 'required',

        ];

        if (!$this->validate($validationRules)) {
            session()->setFlashdata('error', 'Data tidak valid');
            return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
        }
        $galleryModel = new GalleryModel();
        // Unggah gambar
        $gambar = $this->request->getFile('gambar');
        $newGambarName = $gambar->getRandomName();
        $gambar->move(FCPATH . 'foto_gallery', $newGambarName);
        $data = [
            'id_admin' => session()->get('id_admin'),
            'judul' => $this->request->getPost('judul'),
            'keterangan' => $this->request->getPost('keterangan'),
            'gambar' => $newGambarName,
        ];
        $galleryModel->insert($data);

        //tampil pesan 
        session()->setFlashdata('success', 'Berhasil tambah data gallery');
        return redirect()->to(base_url('gallery'));
    }

    public function edit($id)
    {
        if (empty($id) || !is_numeric($id)) {
            session()->setFlashdata('error', 'Data tidak valid');
            return redirect()->to(base_url('gallery'))->with('error', 'ID tidak valid.');
        }
        $galleryModel = new GalleryModel();
        $userModel = new UserModel();

        // Ambil data gallery berdasarkan ID
        $gallery = $galleryModel->find($id);

        if (!$gallery) {
            session()->setFlashdata('error', 'Data tidak ditemukan');
            return redirect()->to(base_url('gallery'))->with('error', 'Gallery tidak ditemukan.');
        }

        // Ambil semua data mata pelajaran
        $userData = $userModel->findAll();
        $aktif = 'gallery';
        return view('admin/gallery/edit', [
            'aktif' => $aktif,
            'gallery' => $gallery,
            'userData' => $userData
        ]);
    }
    public function update($id)
    {

        $validationRules = [
            'gambar' => 'uploaded[gambar]|max_size[gambar,1024]|is_image[gambar]',
            'judul' => 'required',
            'keterangan' => 'required',

        ];

        if (!$this->validate($validationRules)) {
            session()->setFlashdata('error', 'Data tidak valid');
            return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
        }
        $galleryModel = new GalleryModel();
        $gallery = $galleryModel->find($id);

        $gambar = $this->request->getFile('gambar');
        if ($gambar->isValid() && !$gambar->hasMoved()) {
            $newGambarName = $gambar->getRandomName();
            $gambar->move(FCPATH . 'foto_gallery', $newGambarName);
            unlink(FCPATH . 'foto_gallery/' . $gallery->gambar); // Hapus gambar lama
        } else {
            $newGambarName = $gallery->gambar;
        }
        $data = [
            'id_admin' => session()->get('id_admin'),
            'judul' => $this->request->getPost('judul'),
            'keterangan' => $this->request->getPost('keterangan'),
            'gambar' => $newGambarName,
        ];
        unset($data['_method']);
        $galleryModel->update($id, $data);

        //tampil pesan 
        session()->setFlashdata('success', 'Berhasil edit data gallery');
        return redirect()->to(base_url('gallery'));
    }
    public function destroy($id)
    {
        // Periksa apakah ID tidak valid (kosong atau tidak numeric)
        if (empty($id) || !is_numeric($id)) {
            session()->setFlashdata('error', 'Data tidak valid');
            return redirect()->to(base_url('gallery'))->with('error', 'ID tidak valid.');
        }

        $galleryModel = new GalleryModel();
        $gallery = $galleryModel->find($id);

        // Periksa apakah gallery dengan ID tersebut tidak ditemukan
        if (!$gallery) {
            session()->setFlashdata('error', 'Data tidak ditemukan');
            return redirect()->to(base_url('gallery'))->with('error', 'Gallery tidak ditemukan.');
        }
        // Hapus gambar terkait
        $gambarPath = FCPATH . 'foto_gallery/' . $gallery->gambar;
        if (file_exists($gambarPath)) {
            unlink($gambarPath);
        }
        $galleryModel->delete($id);
        return redirect()->to(base_url('gallery'))->with('success', 'Gallery berhasil dihapus.');
    }
}