<?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_Kegiatan_santri_model $kegiatan_santri_model
 */

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

    public function index()
    {
        $data['title'] = 'Kegiatan Santri';
        $data['kegiatan'] = $this->kegiatan_santri_model->get_data('tb_kegiatan')->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/kegiatan_santri');
        $this->load->view('templates/footer');
    }

    public function tambah()
    {
        $data['title'] = 'Tambah Kegiatan Santri';
        $data['kegiatan'] = $this->kegiatan_santri_model->get_data('tb_kegiatan')->result();
        $data['admin'] = $this->db->get_where('admin', ['email' => $this->session->userdata('email')])->row_array();

        // $data['id_santri'] = $this->db->select_max('id_santri')->get('tb_kegiatan')->row()->id_santri + 1;

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

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

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

            $this->kegiatan_santri_model->insert_data($data, 'tb_kegiatan');
            $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('kegiatansantri');
        }
    }

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

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

            $data = array(
                'id_kegiatan' => $id_kegiatan,
                'hari' => $this->input->post('hari'),
                'nama_kegiatan' => $this->input->post('nama_kegiatan'),
                'waktu' => $this->input->post('waktu')
            );

            $this->kegiatan_santri_model->update_data($data, 'tb_kegiatan');
            $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('kegiatansantri');
        }
    }

    public function _rules()
    {
        $this->form_validation->set_rules('hari', 'Hari', 'required', array(
            'required' => '%s harus diisi!!'
        ));
        $this->form_validation->set_rules('nama_kegiatan', 'Nama Kegiatan', 'required', array(
            'required' => '%s harus diisi!!'
        ));
        $this->form_validation->set_rules('waktu', 'Waktu', 'required', array(
            'required' => '%s harus diisi!!'
        ));
    }

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

        $this->kegiatan_santri_model->delete($where, 'tb_kegiatan');
        $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('kegiatansantri');
    }
}
