<?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_Perlombaan_model $perlombaan_model
 */

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

    public function index()
    {
        $data['title'] = 'Halaman Perlombaan';
        $data['perlombaan'] = $this->perlombaan_model->get_data('tb_perlombaan')->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/perlombaan');
        $this->load->view('templates/footer');
    }

    public function tambah()
    {
        $data['title'] = 'Tambah Perlombaan Santri';
        $data['perlombaan'] = $this->perlombaan_model->get_data('tb_perlombaan')->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_perlombaan', $data);
        $this->load->view('templates/footer');
    }

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

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

            $this->perlombaan_model->insert_data($data, 'tb_perlombaan');
            $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('perlombaan');
        }
    }

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

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

            $data = array(
                'id_perlombaan' => $id_perlombaan,
                'agenda' => $this->input->post('agenda'),
                'perlombaan' => $this->input->post('perlombaan'),
                'tanggal' => $this->input->post('tanggal')
            );

            $this->perlombaan_model->update_data($data, 'tb_perlombaan');
            $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('perlombaan');
        }
    }

    public function _rules()
    {
        $this->form_validation->set_rules('agenda', 'Agenda', 'required', array(
            'required' => '%s harus diisi!!'
        ));
        $this->form_validation->set_rules('perlombaan', 'Perlombaan', 'required', array(
            'required' => '%s harus diisi!!'
        ));
        $this->form_validation->set_rules('tanggal', 'Tanggal', 'required', array(
            'required' => '%s harus diisi!!'
        ));
    }

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

        $this->perlombaan_model->delete($where, 'tb_perlombaan');
        $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('perlombaan');
    }
}
