<?php

class App_model extends CI_Model {

    private $table = 'ruang_booking';

    public function getRequest() {
        $data = array(
            'npk' => $this->input->post('npk'),
            'nama' => $this->input->post('nama'),
            'email' => $this->input->post('email'),
            'extension' => $this->input->post('extension'),
            'divisi' => $this->input->post('divisi'),
            'department' => $this->input->post('departement'),
            'start' => date('Y-m-d H:i:s', strtotime($this->input->post('start'))),
            'end' => date('Y-m-d H:i:s', strtotime($this->input->post('end'))),
            'fungsi' => $this->input->post('fungsi')
        );
        return $data;
    }

    function ambil_data() {
        $this->db->order_by('id', 'DESC');
        $data = $this->db->get($this->table);
        return $data->result();
    }

    function ambil_satu_data() {
        $this->db->where('id', $this->uri->segment(3));
        $query = $this->db->get($this->table);
        return $query->result();
    }

    function tambah_data() {
        $data = $this->getRequest();
        $this->db->insert($this->table, $data);
        return true;
    }

    function edit_data() {
        $data = array(
            'npk' => $this->input->post('npk'),
            'nama' => $this->input->post('nama'),
            'email' => $this->input->post('email'),
            'extension' => $this->input->post('extension'),
            'divisi' => md5($this->input->post('divisi')),
            'departement' => $this->input->post('departement'),
            'start' => date('Y-m-d H:i', strtotime($this->input->post('start'))),
            'end' => date('Y-m-d H:i', strtotime($this->input->post('end'))),
            'fungsi' => $this->input->post('fungsi')
        );
        $this->db->where('id', $this->input->post('id'));
        $this->db->update($this->table, $data);
        //$this->db->insert($this->table, $data);
        return true;
    }

    function delete_data() {
        $this->db->where('id', $this->uri->segment(3));
        $this->db->delete($this->table);
    }

    function cek_waktu() {
        $start = date('Y-m-d H:i', strtotime($this->input->post('start')));
        $end = date('Y-m-d H:i', strtotime($this->input->post('end')));

        $kueri = $this->db->query("SELECT * FROM ruang_booking
                WHERE '$start' BETWEEN start AND end
                OR '$end' BETWEEN start AND end");
        if ($kueri->num_rows() == 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

}
?>
