<?php

class Roomcalendar extends CI_Controller {

    function __construct() {
        parent::__construct();
        $s_imeeting['activemenu'] = '006';
        $this->session->set_userdata('s_imeeting', $s_imeeting);

        $this->load->library('mydate');
        $this->load->model('script_m');
        $this->load->model('room_m');
    }

    //ปฏิทินการใช้ห้อง
    function index() {
        $this->load->library('mycalendar');
        $calendar = new $this->mycalendar();
        $calendar->page_url = "?";
        $calendar->year_be = true; // ไม่กำหนดก็ได้
        $calendar->language = "thai"; // ไม่กำหนดก็ได

        $this->db->where('status', '0');
        $this->db->or_where('status', '1');
        $row = $this->db->get('tbreserv')->result_array();
        $appointment = array();
        foreach ($row as $r) {
            $img = base_url() . "assets/images/msg0_16x16.png";   //รอการอนุมัติ
            if ($r['status'] == '1')
                $img = base_url() . "assets/images/msg1_16x16.png"; //อนุมัติแล้ว

            $event = $this->room_m->get_roomname($r['room_id'])."<br/>".htmlspecialchars($r['meet_name']) . "<br/>เวลา " . $r['start_time'] . " - " . $r['end_time'];
            $appointment[]['date'][$r['rdate']] = "<img src=\"$img\" title=\"$event\" onclick=\"showDetail(" . $r['id'] . ");\">";
        }

        //รายการจองเพิ่มเติม
        $sql = "SELECT tbreserv.meet_name, tbbooking.room_id, tbbooking.rdate, tbbooking.start_time, tbbooking.end_time, tbreserv.id, tbreserv.status";
        $sql.= " FROM tbbooking Inner Join tbreserv ON tbbooking.doc_id = tbreserv.id WHERE tbreserv.status='0' or tbreserv.status='1'";
        $result = $this->db->query($sql)->result_array();
        foreach ($result as $r) {
            $img = base_url() . "assets/images/msg0_16x16.png";   //รอการอนุมัติ
            if ($r['status'] == '1')
                $img = base_url() . "assets/images/msg1_16x16.png"; //อนุมัติแล้ว

            $event = $this->room_m->get_roomname($r['room_id'])."<br/>".htmlspecialchars($r['meet_name']) . "<br/>เวลา " . $r['start_time'] . " - " . $r['end_time'];
            $appointment[]['date'][$r['rdate']] = "<img src=\"$img\" title=\"$event\" onclick=\"showDetail(" . $r['id'] . ");\">";
        }

        $calendar->appointment = $appointment;
        $data['calendar'] = $calendar;

        $data['extraHeadContent'] = $this->script_m->bootstrap_tooltip();
        $data['extraHeadContent'].= $this->script_m->bootstrap_modal();

        $data['page_title'] = "ปฏิทินการใช้ห้อง";
        $data['content'] = 'roomcalendar/roomcalendar_v';
        $this->load->view('template_v', $data);
    }

    //get detail แบบ ajax
    function ajax_detail() {
        $id = $this->input->post('id');

        $sql = "SELECT tbreserv.id, tbreserv.meet_name, tbreserv.chairman, tbreserv.cdate, tbreserv.room_id, tbreserv.rdate, tbreserv.start_time,";
        $sql.= " tbreserv.end_time,tbreserv.amount_people,tbreserv.equip,tbreserv.contact_name,tbreserv.contact_tel,tbreserv.contact_email,";
        $sql.= " tbreserv.employee,tbreserv.meet_detail,tbreserv.status,tbreserv.status_detail,tbreserv.approve_id,tbreserv.approve_time,tbreserv.chairman,";
        $sql.= " tbroom.name as room_name,tbreserv.member_id,tbmember.name as member_name,tbmember.mobile,tbmember.tel,tbdepart.name as depart_name FROM tbroom";
        $sql.= " Inner Join tbreserv ON tbreserv.room_id = tbroom.id Inner Join tbmember ON tbreserv.member_id = tbmember.id ";
        $sql.= " Inner Join tbdepart ON tbmember.depart_id = tbdepart.id WHERE tbreserv.id =  '$id'";
        $row = $this->db->query($sql)->row_array();
        $row['status_text'] = $this->get_statusname($row['status']);


        $sql = "SELECT tbequip.name FROM tbbooking_equip Inner Join tbequip ON tbbooking_equip.equip_id = tbequip.id";
        $sql.= " WHERE tbbooking_equip.doc_id =  '" . $row['id'] . "'";
        $equip = $this->db->query($sql)->result_array();
        $data['equip'] = $equip;

        $sql = "SELECT tbroom.name as room_name, tbbooking.rdate,tbbooking.start_time, tbbooking.end_time FROM tbbooking";
        $sql.=" Inner Join tbroom ON tbbooking.room_id = tbroom.id WHERE tbbooking.doc_id =  '" . $row['id'] . "' AND tbbooking.wtype =  '0'";
        $sql.=" ORDER BY tbbooking.rdate ASC";
        $booking = $this->db->query($sql)->result_array();
        $data['booking'] = $booking;

        //ดึงรายละเอียดผู้อนุมัติ
        $this->db->where('id', $row['approve_id']);
        $approve = $this->db->get('tbmember')->row_array();
        $data['approve'] = $approve;

        $data['row'] = $row;
        $data['mydate'] = $this->mydate;
        $this->load->view('roomcalendar/detail_v', $data);
    }

    function get_statusname($status) {
        $st_name = 'รอการอนุมัติ';
        switch ($status) {
            case '1':
                $st_name = 'อนุมัติ';
                break;
            case '2':
                $st_name = 'ไม่อนุมัติ';
                break;
            case '3':
                $st_name = 'ยกเลิกจอง';
                break;
        }

        return $st_name;
    }

}

?>
