<?php

class Room extends MY_Controller {

    public $page_title = 'ข้อมูลห้องประชุม';

    function __construct() {
        parent::__construct();
        $this->load->model('db_m');
        $this->load->model('script_m');
        $this->load->model('room_m');

        $s_imeeting['activemenu'] = '002';
        $this->session->set_userdata('s_imeeting', $s_imeeting);
    }

    function index() {
        $data['page_title'] = $this->page_title;
        $data['content'] = 'room/room_v';
        $this->load->view('template_v', $data);
    }

    function news() {
        $data['extraHeadContent'] = $this->script_m->formatCurrency();
        $data['extraHeadContent'].= $this->script_m->mytab();
        $data['extraHeadContent'].= $this->script_m->lightbox();

        $data['page_title'] = $this->page_title . " (เพิ่มข้อมูล)";
        $data['content'] = 'room/room_frm_v';
        $this->load->view('template_v', $data);
    }

    function edit($id, $success = '0') {
        $this->db->where('id', $id);
        $row = $this->db->get('tbroom')->row_array();
        $data['row'] = $row;
        if ($success == '1') {
            $data['msg_text'] = 'บันทึกข้อมูเสร็จเรียบร้อย';
        }

        $data['extraHeadContent'] = $this->script_m->formatCurrency();
        $data['extraHeadContent'].= $this->script_m->mytab();
        $data['extraHeadContent'].= $this->script_m->lightbox();

        $data['page_title'] = $this->page_title;
        $data['content'] = 'room/room_frm_v';
        $this->load->view('template_v', $data);
    }

    function ajax_validate() {
        $id = trim($_POST['id']);
        $code = trim($_POST['name']);

        $data['msg'] = '0';
        if ($this->db_m->validate('tbroom', $id, 'name', $code)) {
            $data['msg'] = '1';
            $data['msg_text'] = 'ชื่อห้องประชุมนี้มีการบันทึกข้อมูลแล้ว ไม่สามารถสร้างซ้ำชื่อเดิมได้';
        }
        echo json_encode($data);
    }

    function save() {
        $id = $this->input->post('id');

        $data = array(
            'name' => $this->input->post('name'),
            'room_size' => $this->input->post('room_size'),
            'sit_amount' => str_replace(",", "", $this->input->post('sit_amount')),
            'description' => $this->input->post('description'),
            'pre_hours' => str_replace(",", "", $this->input->post('pre_hours')),
            'status' => $this->input->post('status'),
            'ma_email'=>$this->input->post('ma_email'),
            'ma_name'=>$this->input->post('ma_name')
        );

        if ($id == '') {
            $this->db->insert('tbroom', $data);
            $id = $this->db->insert_id();
        } else {
            $this->db->where('id', $id);
            $this->db->update('tbroom', $data);
        }
        redirect("room/edit/$id/1");
    }

    function ajax_get_grid($offset = 0) {
        $txtsearch = $this->input->post('txtsearch');

        $array = $this->room_m->get_all($txtsearch, $offset);
        $data['query'] = $array['query'];
        $data['pagination'] = $array['pagination'];
        $this->load->view('room/room_grid_v', $data);
    }

    function ajax_delete() {
        $this->db->where('id', $this->input->post('id'));
        $this->db->delete('tbroom');
        echo true;
    }

    function upload() {
        $this->load->library('myfile');
        $id = $this->input->post('id');

        for ($i = 0; $i < count($_FILES["fileUpload"]["name"]); $i++) {
            if ($_FILES["fileUpload"]["name"][$i] != "") {
                // rename
                $file_name = basename($_FILES['fileUpload']['name'][$i]);
                $ext_file = explode(".", $file_name);
                $ext_file = $ext_file[count($ext_file) - 1];
                $file_name = $id . "_" . $i . "." . $ext_file;

                move_uploaded_file($_FILES["fileUpload"]["tmp_name"][$i], $this->myfile->GetPhysicalFromURL() . "assets/upload/" . $file_name);
                $j = $i + 1;
                $data['picture' . $j] = $file_name;
                $this->db->where('id', $id);
                $this->db->update('tbroom', $data);
            }
        }

        redirect("room/edit/$id/1");
    }

    function ajax_roombusy_set_session() {
        $this->load->library('mydate');
        $s_roombusy = array(
            'rdate' => $this->mydate->dateToMySql($this->input->post('rdate')),
            'start_time' => $this->input->post('start_time'),
            'end_time' => $this->input->post('end_time')
        );
        $this->session->set_userdata('s_roombusy',$s_roombusy);
        echo true;
    }

    function roombusy_dlg() {
        $s_roombusy = $this->session->userdata('s_roombusy');
        
        $rdate = $s_roombusy['rdate'];
        $start_time = $s_roombusy['start_time'];
        $end_time = $s_roombusy['end_time'];

        $result = $this->room_m->get_all_roombusy($rdate, $start_time, $end_time);
        $data['query'] = $result;
        $data['content'] = 'room/roombusy_dlg_v';
        $this->load->view('template_modal_window_v', $data);
    }
    
    function ajax_get_sit(){
        $room_id = $this->input->post('room_id');
        $this->db->where('id',$room_id);
        $row = $this->db->get('tbroom')->row_array();
        
        $sit_amount = 0;
        if(isset($row['id'])) $sit_amount = $row['sit_amount'];
        
        $data['sit_amount'] = $sit_amount;
        echo json_encode($data);
    }

}

?>
