<?php

class Mycarquery extends CI_Controller {

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

        $s_imeeting['activemenu'] = '008';
        $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'] = "รายการจองใช้รถ (ส่วนตัว)";
        $data['content'] = 'profile/mycarquery_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->carquery_m->get_all($start_date, $end_date, $status, $fieldsearch, $txtsearch, $offset, 20, '/mycarquery/ajax_get_grid', $member_id);
        $data['query'] = $array['query'];
        $data['pagination'] = $array['pagination'];

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

    function show($id = 0, $success = 0) {
        $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;

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

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

        redirect("mycarquery/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 preview($id) {
        $this->load->library('mpdf/mpdf');
        $this->load->model('report_m');

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

        //ดึงรายละเอียดผู้อนุมัติ
        $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['name'] . '</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><b>ทะเบียน</b> ' . $row['car_code'] . "&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><b>ทะเบียน</b> ' . $r['car_code'] . "&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>' . nl2br($row['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();
    }

}

?>
