<?php

class Roomquery extends CI_Controller {

    var $page_title = "รายการจองห้องประชุม";

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

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

    function index() {
        $data['extraHeadContent'] = $this->script_m->calendar();
        $data['extraHeadContent'].= $this->script_m->textformat();

        $first_date = strftime('%Y') . '-' . strftime('%m') . '-01';
        $data['start_date'] = $this->mydate->dateToText($first_date);
        $data['end_date'] = $this->mydate->lastDate();

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

    function ajax_get_grid($offset = 0) {
        $start_date = $this->mydate->dateToMySql($this->input->post('start_date'));
        $end_date = $this->mydate->dateToMySql($this->input->post('end_date'));
        $status = $this->input->post('status');
        $fieldsearch = $this->input->post('fieldsearch');
        $txtsearch = $this->input->post('txtsearch');

        $array = $this->roomquery_m->get_all($start_date, $end_date, $status, $fieldsearch, $txtsearch, $offset);
        $data['query'] = $array['query'];
        $data['pagination'] = $array['pagination'];

        $data['mydate'] = $this->mydate;
        $this->load->view('roomquery/roomquery_grid_v', $data);
    }

    function show($id = 0, $success = 0) {
        $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_time,tbreserv.approve_id,tbreserv.chairman,";
        $sql.= " tbroom.name as room_name,tbreserv.member_id,tbmember.name as member_name,tbmember.mobile,tbmember.tel,";
        $sql.= " tbmember.depart_id, tbdepart.name as depart_name,tbreserv.file_tmpname,tbreserv.file_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;

        if ($success == '1') {
            $data['msg_text'] = 'บันทึกผลการอนุมัติเรียบร้อย';
        }

        $row['sendemail'] = '0';
        $cnf = $this->db->get('tbconfig')->row_array();
        if (isset($cnf['id'])) {
            if ($cnf['sendemail'] == '1') {
                $row['sendemail'] = '1';
            }
        }

        //ตรวจสอบว่าเป็นผู้อนุมัติหรือไม่ ถ้าใช่ก้มีสิทธิ์แก้ไขการอนุมัติ
        $s_login = $this->session->userdata('s_login');
        $this->db->where('member_id', $s_login['login_id']);
        $this->db->where('depart_id', $row['depart_id']);
        $chk_approve = $this->db->get('tbmembertodepart')->num_rows();
        if ($s_login['login_type'] == '1') {
            $chk_approve = '1';
        }
        $data['chk_approve'] = $chk_approve;




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

        $data['row'] = $row;
        $data['mydate'] = $this->mydate;
        $data['page_title'] = "รายการจองห้องประชุม";
        $data['content'] = 'roomquery/roomquery_frm_v';
        $this->load->view('template_v', $data);
    }

    function save() {
        $this->load->model('email_m');

        $s_login = $this->session->userdata('s_login');
        $id = $this->input->post('id');

        $data = array(
            'status' => $this->input->post('status'),
            'status_detail' => $this->input->post('status_detail'),
            'approve_time' => date("Y-m-d H:i:s"),
            'approve_id' => $s_login['login_id']
        );
        $this->db->where('id', $id);
        $this->db->update('tbreserv', $data);

        //ส่งอีเมลล์แจ้งผลการอนุมัติ
        $this->db->where('id', $this->input->post('member_id'));
        $member = $this->db->get('tbmember')->row_array();

        if ($member['email'] != '') {

            $this->db->where('id', $id);
            $res = $this->db->get('tbreserv')->row_array();

            $subject = 'แก้ไขผลการอนุมัติจองห้องประชุม';
            $message = '<b>วาระการประชุม</b> ' . $res['meet_name'] . "<br/>";
            $message.= '<b>วันที่จอง</b> ' . $this->mydate->dateToText($res['rdate']) . " <b>เวลา</b> " . $res['start_time'] . " <b>ถึง</b> " . $res['end_time'];
            $message.= " <b>ห้องประชุม</b> " . $this->room_m->get_roomname($res['room_id']) . "<br/>";

            $this->db->where('doc_id', $id);
            $this->db->where('wtype', '0');
            $booking = $this->db->get('tbbooking')->result_array();
            foreach ($booking as $b) {
                $message.= "วันที่ </b>" . $this->mydate->dateToText($b['rdate']) . " <b>เวลา</b> " . $b['start_time'] . " <b>ถึง</b> " . $b['end_time'];
                $message.= " <b>ห้องประชุม</b> " . $this->room_m->get_roomname($b['room_id']) . "<br/>";
            }

            $message.= "<b>ผู้จอง</b> " . $member['name'] . " <b>ติดต่อ</b> " . $member['mobile'] . ", " . $member['tel'] . "<br/>";
            $message.= "<b>ผลการอนุมัติ </b>=> " . $this->get_statusname($this->input->post('status')) . " <=";
            $message.= "<br/><b>เหตุผล</b> " . nl2br($this->input->post('status_detail'));
            $message.= "<br/><b>วันที่อนุมัติ</b> " . date("Y-m-d H:i");
            $this->email_m->sendmail($member['email'], 'ระบบ iMeeting', $member['email'], $subject, $message, $member['id']);
        }
        redirect("roomquery/show/$id/1");
    }

    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;
    }

    function roombusy() {
        $data['timerange'] = $this->db->get('tbtimerange')->result_array();

        $data['extraHeadContent'] = $this->script_m->calendar();
        $data['extraHeadContent'].= $this->script_m->textformat();

        $data['rdate'] = $this->mydate->dateToText(date("Y-m-d"));
        $data['page_title'] = 'สอบถามห้องประชุมว่าง';
        $data['content'] = 'roomquery/roombusy_v';
        $this->load->view('template_v', $data);
    }

    function ajax_roombusy() {
        $rdate = $this->mydate->dateToMySql($this->input->post('rdate'));
        $start_time = $this->input->post('start_time');
        $end_time = $this->input->post('end_time');

        $result = $this->room_m->get_all_roombusy($rdate, $start_time, $end_time);
        $data['query'] = $result;
        $this->load->view('roomquery/roombusy_grid_v', $data);
    }

    function room() {
        $data['page_title'] = "รายละเอียดห้องประชุม";
        $data['content'] = 'roomquery/room_v';
        $this->load->view('template_v', $data);
    }

    function ajax_room_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('roomquery/room_grid_v', $data);
    }

    function room_detail($id) {
        $this->db->where('id', $id);
        $row = $this->db->get('tbroom')->row_array();
        $data['row'] = $row;

        $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'] = 'roomquery/room_detail_v';
        $this->load->view('template_v', $data);
    }

}

?>
