<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Divisi extends Auth_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Divisi_model');
    }

    public function index()
    {
        $data = [
            'title'        => 'Master Divisi | ' . app_name(),
            'page_title'   => 'Master Divisi',
            'page_subtitle'=> 'Data divisi / departemen perusahaan',
            'list_divisi'  => $this->Divisi_model->get_all(),
        ];
        $this->render('master/divisi/list', $data);
    }

    public function tambah()
    {
        $this->_form_rules();

        if ($this->form_validation->run() === FALSE) {
            $this->_form_view();
            return;
        }

        $kode = strtoupper(trim($this->input->post('kode_divisi', TRUE)));
        if ($this->Divisi_model->kode_exists($kode)) {
            $this->session->set_flashdata('error', ['title' => 'Gagal', 'text' => 'Kode divisi sudah digunakan.']);
            $this->_form_view();
            return;
        }

        $this->Divisi_model->insert([
            'kode_divisi' => $kode,
            'nama_divisi' => trim($this->input->post('nama_divisi', TRUE)),
            'keterangan'  => trim($this->input->post('keterangan', TRUE)),
            'is_active'   => 1,
        ]);

        audit_log('TAMBAH', 'DIVISI', 'Tambah divisi: ' . $kode);
        $this->session->set_flashdata('success', ['title' => 'Berhasil', 'text' => 'Divisi berhasil ditambahkan.']);
        redirect('master/divisi');
    }

    public function edit($id = NULL)
    {
        $row = $this->_get_or_404($id);

        $this->_form_rules();
        if ($this->form_validation->run() === FALSE) {
            $this->_form_view($row);
            return;
        }

        $kode = strtoupper(trim($this->input->post('kode_divisi', TRUE)));
        if ($this->Divisi_model->kode_exists($kode, $id)) {
            $this->session->set_flashdata('error', ['title' => 'Gagal', 'text' => 'Kode divisi sudah digunakan.']);
            $this->_form_view($row);
            return;
        }

        $this->Divisi_model->update($id, [
            'kode_divisi' => $kode,
            'nama_divisi' => trim($this->input->post('nama_divisi', TRUE)),
            'keterangan'  => trim($this->input->post('keterangan', TRUE)),
        ]);

        audit_log('EDIT', 'DIVISI', 'Edit divisi ID: ' . $id);
        $this->session->set_flashdata('success', ['title' => 'Berhasil', 'text' => 'Divisi berhasil diperbarui.']);
        redirect('master/divisi');
    }

    public function toggle($id = NULL)
    {
        $this->_get_or_404($id);
        $this->Divisi_model->toggle_active($id);
        audit_log('TOGGLE', 'DIVISI', 'Toggle aktif divisi ID: ' . $id);
        $this->session->set_flashdata('success', ['title' => 'Berhasil', 'text' => 'Status divisi diperbarui.']);
        redirect('master/divisi');
    }

    // -------------------------------------------------------
    // Helpers private
    // -------------------------------------------------------

    private function _form_rules()
    {
        $this->form_validation->set_rules('kode_divisi', 'Kode Divisi', 'required|trim|max_length[10]|alpha_numeric');
        $this->form_validation->set_rules('nama_divisi', 'Nama Divisi', 'required|trim|max_length[100]');
    }

    private function _form_view($row = NULL)
    {
        $data = [
            'title'        => ($row ? 'Edit' : 'Tambah') . ' Divisi | ' . app_name(),
            'page_title'   => ($row ? 'Edit' : 'Tambah') . ' Divisi',
            'page_subtitle'=> 'Master Data Divisi',
            'row'          => $row,
        ];
        $this->render('master/divisi/form', $data);
    }

    private function _get_or_404($id)
    {
        $row = $this->Divisi_model->get_by_id($id);
        if (!$row) show_404();
        return $row;
    }
}
