<?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_Sejarah_model $sejarah_model
 */

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

    public function index()
    {
        $data['title'] = 'Sejarah';
        $data['sejarah'] = $this->sejarah_model->get_data('tb_sejarah')->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/sejarah');
        $this->load->view('templates/footer');
    }

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

        if ($this->form_validation->run() == TRUE) {
            $data = array(
                'sejarah' => $this->input->post('sejarah'),
            );

            $this->sejarah_model->insert_data($data, 'tb_sejarah');
            $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('sejarah');
        }
    }

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

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

            $data = array(
                'id_sejarah' => $id_sejarah,
                'sejarah' => $this->input->post('sejarah'),
            );

            $this->sejarah_model->update_data($data, 'tb_sejarah');
            $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('sejarah');
        }
    }

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

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

        $this->sejarah_model->delete($where, 'tb_sejarah');
        $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('sejarah');
    }
}
