<?php

namespace App\Controllers\Admin;

use App\Controllers\BaseController;
use App\Models\StrukturModel;

class Struktur extends BaseController
{
    protected $strukturModel;
    protected $helpers = ['url', 'form'];

    public function __construct()
    {
        $this->strukturModel = new StrukturModel();
    }

    public function index()
    {
        $data = [
            'title'    => 'Manajemen Struktur Organisasi',
            'struktur' => $this->strukturModel->orderBy('level', 'ASC')->orderBy('urutan', 'ASC')->findAll()
        ];
        return view('admin/struktur/index', $data);
    }

    public function tambah()
    {
        return view('admin/struktur/tambah', ['title' => 'Tambah Personil Baru']);
    }

    public function simpan()
    {
        $fileFoto = $this->request->getFile('foto');
        if ($fileFoto->getError() == 4) {
            $namaFoto = 'default.jpg';
        } else {
            $namaFoto = $fileFoto->getRandomName();
            $fileFoto->move('assets/img/team/', $namaFoto);
        }

        $this->strukturModel->save([
            'nama'      => $this->request->getPost('nama'),
            'jabatan'   => $this->request->getPost('jabatan'),
            'level'     => $this->request->getPost('level'),
            'parent_id' => $this->request->getPost('parent_id'), // Tambahkan ini
            'urutan'    => $this->request->getPost('urutan'),
            'foto'      => $namaFoto
        ]);

        return redirect()->to(base_url('admin/struktur'))->with('success', 'Data personil berhasil ditambahkan.');
    }

    public function edit($id)
    {
        $data = [
            'title'    => 'Edit Data Personil',
            'personil' => $this->strukturModel->find($id)
        ];
        return view('admin/struktur/edit', $data);
    }

    public function update()
    {
        $id = $this->request->getPost('id_struktur');
        $fileFoto = $this->request->getFile('foto');
        $fotoLama = $this->request->getPost('fotoLama');

        if ($fileFoto->getError() == 4) {
            $namaFoto = $fotoLama;
        } else {
            $namaFoto = $fileFoto->getRandomName();
            $fileFoto->move('assets/img/team/', $namaFoto);
            if ($fotoLama != 'default.jpg' && file_exists('assets/img/team/' . $fotoLama)) {
                @unlink('assets/img/team/' . $fotoLama);
            }
        }

        $this->strukturModel->update($id, [
            'nama'      => $this->request->getPost('nama'),
            'jabatan'   => $this->request->getPost('jabatan'),
            'level'     => $this->request->getPost('level'),
            'parent_id' => $this->request->getPost('parent_id'), // Tambahkan ini
            'urutan'    => $this->request->getPost('urutan'),
            'foto'      => $namaFoto
        ]);

        return redirect()->to(base_url('admin/struktur'))->with('success', 'Data berhasil diperbarui.');
    }

    public function hapus($id)
    {
        $personil = $this->strukturModel->find($id);
        if ($personil['foto'] != 'default.jpg' && file_exists('assets/img/team/' . $personil['foto'])) {
            @unlink('assets/img/team/' . $personil['foto']);
        }
        $this->strukturModel->delete($id);
        return redirect()->to(base_url('admin/struktur'))->with('success', 'Data berhasil dihapus.');
    }
}