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

/**
 * @property CI_Db $db
 * @property CI_Session $session
 * @property CI_Form_validation $form_validation
 * @property CI_Input $input
 * @property CI_Pelanggaran_model $pelanggaran_model
 */

class Pelanggaran extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('pelanggaran_model');
        is_logged_in();
    }

    public function index()
    {
        $data['title'] = 'Kategori Pelanggaran';
        $data['pelanggaran'] = $this->pelanggaran_model->get_data('tb_pelanggaran')->result();
        $data['admin'] = $this->db->get_where('admin', ['email' => $this->session->userdata('email')])->row_array();

        $this->load->view('templates/header', $data);
        $this->load->view('templates/sidebar', $data);
        $this->load->view('admin/pelanggaran');
        $this->load->view('templates/footer');
    }

    public function tambah()
    {
        $data['title'] = 'Tambah Kategori Pelanggaran';
        $data['pelanggaran'] = $this->pelanggaran_model->get_data('tb_pelanggaran')->result();
        $data['admin'] = $this->db->get_where('admin', ['email' => $this->session->userdata('email')])->row_array();

        $this->load->view('templates/header', $data);
        $this->load->view('templates/sidebar', $data);
        $this->load->view('admin/tambah_kategori_pelanggaran', $data);
        $this->load->view('templates/footer');
    }

    public function tambah_aksi()
    {
        $this->_rules();

        if ($this->form_validation->run() == FALSE) {
            $this->tambah();
        } else {
            $data = array(
                'pelanggaran' => $this->input->post('pelanggaran'),
                'sanksi' => $this->input->post('sanksi')
            );

            $this->pelanggaran_model->insert_data($data, 'tb_pelanggaran');
            $this->session->set_flashdata('pesan', '<div class="alert alert-success alert-dismissible fade show" role="alert">
            Data berhasil ditambahkan! <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">&times;</span>
            </button></div>');
            redirect('pelanggaran');
        }
    }

    public function edit($id_pelanggaran)
    {
        $this->_rules();

        if ($this->form_validation->run() == FALSE) {
            $this->index();
        } else {

            $data = array(
                'id_pelanggaran' => $id_pelanggaran,
                'pelanggaran' => $this->input->post('pelanggaran'),
                'sanksi' => $this->input->post('sanksi')
            );

            $this->pelanggaran_model->update_data($data, 'tb_pelanggaran');
            $this->session->set_flashdata('pesan', '<div class="alert alert-success alert-dismissible fade show" role="alert">
                Data berhasil diubah! <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">&times;</span>
                </button></div>');
            redirect('pelanggaran');
        }
    }

    public function _rules()
    {
        $this->form_validation->set_rules('pelanggaran', 'Pelanggaran', 'required', array(
            'required' => '%s harus diisi!!'
        ));
        $this->form_validation->set_rules('sanksi', 'Sanksi', 'required', array(
            'required' => '%s harus diisi!!'
        ));
    }

    public function delete($id)
    {
        $where = array('id_pelanggaran' => $id);

        $this->pelanggaran_model->delete($where, 'tb_pelanggaran');
        $this->session->set_flashdata('pesan', '<div class="alert alert-danger alert-dismissible fade show" role="alert">
                Data berhasil dihapus! <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">&times;</span>
                </button></div>');
        redirect('pelanggaran');
    }
}
