<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use App\Models\AnggotaModel;

class Anggota extends BaseController
{
    protected $anggota;

    public function __construct()
    {
        $this->anggota = new AnggotaModel();
    }

    public function index()
    {
        return view('anggota/index', [
            'anggota' => $this->anggota->findAll()
        ]);
    }

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

    public function store()
    {
        $namaFoto = '';

        $foto = $this->request->getFile('foto');

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

            $namaFoto = $foto->getRandomName();

            $foto->move(FCPATH . 'uploads/anggota', $namaFoto);
        }

        $this->anggota->save([
            'foto'   => $namaFoto,
            'nama'   => $this->request->getPost('nama'),
            'nisn'   => $this->request->getPost('nisn'),
            'jenis'  => $this->request->getPost('jenis'),
            'kelas'  => $this->request->getPost('kelas'),
            'alamat' => $this->request->getPost('alamat')
        ]);

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

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

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

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

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

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

        $data = [
            'nama'   => $this->request->getPost('nama'),
            'nisn'   => $this->request->getPost('nisn'),
            'jenis'  => $this->request->getPost('jenis'),
            'kelas'  => $this->request->getPost('kelas'),
            'alamat' => $this->request->getPost('alamat')
        ];

        $foto = $this->request->getFile('foto');

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

            // Hapus foto lama
            if (!empty($anggota['foto']) && file_exists(FCPATH . 'uploads/anggota/' . $anggota['foto'])) {
                unlink(FCPATH . 'uploads/anggota/' . $anggota['foto']);
            }

            // Upload foto baru
            $namaFoto = $foto->getRandomName();
            $foto->move(FCPATH . 'uploads/anggota', $namaFoto);

            $data['foto'] = $namaFoto;
        }

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

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

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

        if ($anggota) {

            if (!empty($anggota['foto']) && file_exists(FCPATH . 'uploads/anggota/' . $anggota['foto'])) {
                unlink(FCPATH . 'uploads/anggota/' . $anggota['foto']);
            }

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

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