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

class Jabatan extends Auth_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model(['Jabatan_model', 'Divisi_model']);
    }

    public function index()
    {
        $data = [
            'title'        => 'Master Jabatan | ' . app_name(),
            'page_title'   => 'Master Jabatan',
            'page_subtitle'=> 'Data jabatan / posisi pegawai',
            'list_jabatan' => $this->Jabatan_model->get_all(),
        ];
        $this->render('master/jabatan/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_jabatan', TRUE)));
        if ($this->Jabatan_model->kode_exists($kode)) {
            $this->session->set_flashdata('error', ['title' => 'Gagal', 'text' => 'Kode jabatan sudah digunakan.']);
            $this->_form_view();
            return;
        }

        $this->Jabatan_model->insert([
            'id_divisi'    => (int) $this->input->post('id_divisi', TRUE),
            'kode_jabatan' => $kode,
            'nama_jabatan' => trim($this->input->post('nama_jabatan', TRUE)),
            'level_jabatan'=> (int) $this->input->post('level_jabatan', TRUE),
            'is_active'    => 1,
        ]);

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

    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_jabatan', TRUE)));
        if ($this->Jabatan_model->kode_exists($kode, $id)) {
            $this->session->set_flashdata('error', ['title' => 'Gagal', 'text' => 'Kode jabatan sudah digunakan.']);
            $this->_form_view($row);
            return;
        }

        $this->Jabatan_model->update($id, [
            'id_divisi'    => (int) $this->input->post('id_divisi', TRUE),
            'kode_jabatan' => $kode,
            'nama_jabatan' => trim($this->input->post('nama_jabatan', TRUE)),
            'level_jabatan'=> (int) $this->input->post('level_jabatan', TRUE),
        ]);

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

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

    // AJAX: jabatan by divisi (untuk form pegawai)
    public function ajax_by_divisi($id_divisi = 0)
    {
        $id_divisi = (int) $id_divisi;
        $list = [];
        if ($id_divisi > 0) {
            $list = $this->Jabatan_model->get_by_divisi($id_divisi);
        }

        return $this->output
            ->set_content_type('application/json')
            ->set_output(json_encode($list));
    }

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

    private function _form_rules()
    {
        $this->form_validation->set_rules('id_divisi',    'Divisi',        'required|is_natural_no_zero');
        $this->form_validation->set_rules('kode_jabatan', 'Kode Jabatan',  'required|trim|max_length[10]|alpha_numeric');
        $this->form_validation->set_rules('nama_jabatan', 'Nama Jabatan',  'required|trim|max_length[100]');
        $this->form_validation->set_rules('level_jabatan','Level Jabatan', 'required|in_list[1,2,3]');
    }

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

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