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

class Master_pendidikan extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('session');
        if (!$this->session->userdata('logged_in')) redirect('auth');
        $this->load->model('Master_pendidikan_model');
        $this->load->model('User_model');
        $this->load->library('form_validation');
        $this->_check_admin();
    }

    public function index() {
        $data['title'] = 'Master Pendidikan';
        $data['list']  = $this->Master_pendidikan_model->get_all();
        $this->load->view('templates/header', $data);
        $this->load->view('templates/sidebar', $data);
        $this->load->view('master_pendidikan/index', $data);
        $this->load->view('templates/footer');
    }

    public function tambah() {
        $this->form_validation->set_rules('nama', 'Nama Pendidikan', 'required|trim|max_length[100]');

        if ($this->form_validation->run() == FALSE) {
            $this->session->set_flashdata('error', validation_errors());
        } else {
            $this->Master_pendidikan_model->insert([
                'nama'   => $this->input->post('nama'),
                'urutan' => (int)$this->input->post('urutan') ?: 0,
                'status' => 1,
            ]);
            $this->User_model->log_activity($this->session->userdata('user_id'), 'create', 'Tambah master pendidikan: '.$this->input->post('nama'));
            $this->session->set_flashdata('success', 'Data pendidikan berhasil ditambahkan');
        }
        redirect('master-pendidikan');
    }

    public function edit($id) {
        $row = $this->Master_pendidikan_model->get_by_id($id);
        if (!$row) {
            $this->session->set_flashdata('error', 'Data tidak ditemukan');
            redirect('master-pendidikan');
        }
        $this->form_validation->set_rules('nama', 'Nama Pendidikan', 'required|trim|max_length[100]');

        if ($this->form_validation->run() == FALSE) {
            $this->session->set_flashdata('error', validation_errors());
        } else {
            $this->Master_pendidikan_model->update($id, [
                'nama'   => $this->input->post('nama'),
                'urutan' => (int)$this->input->post('urutan') ?: 0,
                'status' => (int)$this->input->post('status'),
            ]);
            $this->User_model->log_activity($this->session->userdata('user_id'), 'update', 'Edit master pendidikan: '.$row->nama);
            $this->session->set_flashdata('success', 'Data pendidikan berhasil diupdate');
        }
        redirect('master-pendidikan');
    }

    public function hapus($id) {
        $row = $this->Master_pendidikan_model->get_by_id($id);
        if ($row) {
            $this->Master_pendidikan_model->delete($id);
            $this->User_model->log_activity($this->session->userdata('user_id'), 'delete', 'Hapus master pendidikan: '.$row->nama);
            $this->session->set_flashdata('success', 'Data pendidikan berhasil dihapus');
        }
        redirect('master-pendidikan');
    }

    public function toggle_status($id) {
        $this->Master_pendidikan_model->toggle_status($id);
        $this->session->set_flashdata('success', 'Status berhasil diubah');
        redirect('master-pendidikan');
    }

    public function ajax_get($id) {
        $row = $this->Master_pendidikan_model->get_by_id($id);
        echo json_encode($row);
    }

    private function _check_admin() {
        $role = $this->session->userdata('role');
        if (!in_array($role, ['admin', 'rt', 'ketua_rt'])) {
            $this->session->set_flashdata('error', 'Akses ditolak');
            redirect('dashboard');
        }
    }
}
