<?php

class Carcalendar extends CI_Controller {

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

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

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

        //สำหรับการจอง 1 วัน
        /*
        $this->db->where('status', '0');
        $this->db->or_where('status', '1');
        $row = $this->db->get('tbcarreserv')->result_array();
         */
        $sql = "SELECT tbcarreserv.id, tbcarreserv.car_id, tbcarreserv.name, tbcarreserv.start_time,";
        $sql.= " tbcarreserv.end_time, tbcarreserv.rdate, tbcarreserv.status FROM tbcarreserv Inner Join tbcar ON tbcarreserv.car_id = tbcar.id";
        $sql.= " WHERE tbcarreserv.status =  '0' or tbcarreserv.status = '1'";
        $row = $this->db->query($sql)->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->car_m->get_carcode($r['car_id'])."<br/>".htmlspecialchars($r['name']) . "<br/>เวลา " . $r['start_time'] . " - " . $r['end_time'];
            $appointment[]['date'][$r['rdate']] = "<img src=\"$img\" title=\"$event\" onclick=\"showDetail(" . $r['id'] . ");\">";
        }

        //สำหรับการจองที่เพิ่มวันจอง
        $sql = "SELECT tbcarreserv.name, tbbooking_car.car_id, tbbooking_car.rdate, tbbooking_car.start_time, tbbooking_car.end_time, ";
        $sql.= " tbcarreserv.id, tbcarreserv.status FROM tbbooking_car Inner Join tbcarreserv ON tbbooking_car.doc_id = tbcarreserv.id ";
        $sql.= " Inner Join tbcar ON tbcar.id = tbbooking_car.car_id WHERE tbcarreserv.status='0' or tbcarreserv.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->car_m->get_carcode($r['car_id'])."<br/>".htmlspecialchars($r['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'] = 'carcalendar/carcalendar_v';
        $this->load->view('template_v', $data);
    }

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

        $sql = "SELECT tbcarreserv.id, tbcarreserv.name, tbcarreserv.cdate, tbcarreserv.car_id, tbcarreserv.rdate, tbcarreserv.start_time,";
        $sql.= " tbcarreserv.end_time,tbcarreserv.amount_people,tbcarreserv.employee,";
        $sql.= " tbcarreserv.detail,tbcarreserv.status,tbcarreserv.status_detail,tbcarreserv.approve_time,tbcarreserv.approve_id,";
        $sql.= " tbcar.code as car_code,tbcarreserv.member_id,tbmember.name as member_name,tbmember.mobile,tbmember.tel,";
        $sql.= " tbmember.depart_id,tbdepart.name as depart_name FROM tbcar";
        $sql.= " Inner Join tbcarreserv ON tbcarreserv.car_id = tbcar.id Inner Join tbmember ON tbcarreserv.member_id = tbmember.id ";
        $sql.= " Inner Join tbdepart ON tbmember.depart_id = tbdepart.id WHERE tbcarreserv.id =  '$id'";
        $row = $this->db->query($sql)->row_array();
        $row['status_text'] = $this->get_statusname($row['status']);


        $sql = "SELECT tbcar.code, tbbooking_car.rdate,tbbooking_car.start_time, tbbooking_car.end_time FROM tbbooking_car";
        $sql.=" Inner Join tbcar ON tbbooking_car.car_id = tbcar.id WHERE tbbooking_car.doc_id =  '" . $row['id'] . "'";
        $sql.=" ORDER BY tbbooking_car.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('carcalendar/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;
    }

}

?>
