<?php

class Myroomquery extends MY_Controller {

    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'] = '008';
        $this->session->set_userdata('s_imeeting', $s_imeeting);
    }

    function index($success = '0') {
        $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'] = "รายการจองห้องประชุม (ส่วนตัว)";
        $data['content'] = 'profile/myroomquery_v';
        $this->load->view('template_v', $data);
    }

    function ajax_get_grid($offset = 0) {
        $s_login = $this->session->userdata('s_login');
        $member_id = $s_login['login_id'];

        $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, 20, '/myroomquery/ajax_get_grid', $member_id);
        $data['query'] = $array['query'];
        $data['pagination'] = $array['pagination'];

        $data['mydate'] = $this->mydate;
        $this->load->view('profile/myroomquery_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';
            }
        }

        //ดึงรายละเอียดผู้อนุมัติ
        $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'] = 'profile/myroomquery_frm_v';
        $this->load->view('template_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;
    }

    function cancel() {
        $id = $this->input->post('id');
        $data['status'] = '3';
        $this->db->where('id', $id);
        $this->db->update('tbreserv', $data);

        redirect("myroomquery/show/$id/1");
    }

    function preview($id) {
        $this->load->library('mpdf/mpdf');
        $this->load->model('report_m');

        $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 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();

        $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();

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

        $tbl= '<div class="caption"><b>ใบจองขอใช้ห้องประชุม</b></div>';
        $tbl.='<table>';
        $tbl.='<tbody>';
        $tbl.='<tr>';
        $tbl.='<td width="150">วาระการประชุม :</td>';
        $tbl.='<td>' . $row['meet_name'] . '</td>';
        $tbl.="</tr>";
        $tbl.="<tr>";
        $tbl.='<td class="td1">ประธานการประชุม :</td>';
        $tbl.='<td>' . $row['chairman'] . '</td>';
        $tbl.='</tr>';
        $tbl.='<tr>';
        $tbl.='<td class="td1">จำนวนผู้เข้าประชุม :</td>';
        $tbl.='<td>' . $row['amount_people'] . ' คน</td>';
        $tbl.='</tr>';
        $tbl.='<tr>';
        $tbl.='<td class="td1" valign="top">รายชื่อผู้เข้าประชุม :</td>';
        $tbl.='<td>' . nl2br($row['employee']) . '</td>';
        $tbl.='</tr>';
        $tbl.='<tr>';
        $tbl.='<td class="td1" valign="top">ห้องประชุม :</td>';
        $tbl.='<td>' . $row['room_name'] . "&nbsp;&nbsp;<b>วันที่</b> " . $this->mydate->dateToText($row['rdate']) . '&nbsp;&nbsp;<b>จากเวลา :</b>' . $row['start_time'] . '&nbsp;-&nbsp;' . $row['end_time'];
        foreach ($booking as $r) {
            $tbl.='<p>' . $r['room_name'] . "&nbsp;&nbsp;<b>วันที่</b> " . $this->mydate->dateToText($r['rdate']) . "&nbsp;&nbsp;<b>จากเวลา :</b>" . $r['start_time'] . "&nbsp;-&nbsp;" . $r['end_time']."</p>";
        }
        $tbl.='</td>';
        $tbl.='</tr>';
        $tbl.='<tr>';
        $tbl.='<td class="td1" valign="top">อุปกรณ์ที่ต้องการ :</td>';
        $tbl.='<td>';
        foreach ($equip as $r) {
            $tbl.=$r['name'] . ", ";
        }
        $tbl.='</td>';
        $tbl.='</tr>';
        $tbl.='<tr>';
        $tbl.='<td class="td1" valign="top">อุปกรณ์เพิ่มเติม :</td>';
        $tbl.='<td>' . nl2br($row['equip']) . '</td>';
        $tbl.='</tr>';
        $tbl.='<tr>';
        $tbl.='<td class="td1" valign="top">หมายเหตุ :</td>';
        $tbl.='<td>' . nl2br($row['meet_detail']) . '</td>';
        $tbl.='</tr>';
        $tbl.='<tr>';
        $tbl.='<td class="td1">ผู้ติดต่อ :</td>';
        $tbl.='<td>' . $row['member_name'] . '</td>';
        $tbl.='</tr>';
        $tbl.='<tr>';
        $tbl.='<td class="td1">แผนก :</td>';
        $tbl.='<td>' . $row['depart_name'] . '</td>';
        $tbl.='</tr>';
        $tbl.='<tr>';
        $tbl.='<td class="td1">โทรศัพท์ :</td>';
        $tbl.='<td>' . $row['mobile'] . "," . $row['tel'] . '</td>';
        $tbl.='</tr>';
        $tbl.='<tr>';
        $tbl.='<td class="td1">ผู้อนุมัติ :</td>';
        $tbl.='<td>' . @$approve['name'] . "&nbsp;&nbsp;&nbsp;<b>โทรศัพท์</b> " . @$approve['mobile'] . "," . @$approve['tel'] . '</td>';
        $tbl.='</tr>';
        $tbl.='<tr>';
        $tbl.='<td class="td1">วันที่อนุมัติ :</td>';
        $tbl.='<td>' . $row['approve_time'] . '</td>';
        $tbl.='</tr>';
        $tbl.='<tr>';
        $tbl.='<td class="td1">ผลการอนุมัติ :</td>';
        $tbl.='<td>' . $row['status_text'] . '</td>';
        $tbl.='</tr>';
        $tbl.='<tr>';
        $tbl.='<td class="td1">หมายเหตุการอนุมัติ :</td>';
        $tbl.='<td>' . $row['status_detail'] . '</td>';
        $tbl.='</tr>';
        $tbl.='</tbody>';
        $tbl.='</table>';

        $this->mpdf = new mPDF('th');
        $this->mpdf->WriteHTML($this->report_m->get_css_pdf(), 1);
        $this->mpdf->WriteHTML($tbl);
        $this->mpdf->Output();
    }

}
?>
