<?php

class Approve_car extends MY_Controller {
    var $page_title = 'รายการจองใช้รถ รออนุมัติ';
    
    function __construct() {
        parent::__construct();
        $this->load->library('mydate');
        $this->load->model('car_booking_m');

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

    function index() {
        $data['query'] = $this->car_booking_m->getApproveByManager();
        $data['mydate'] = $this->mydate;

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

    function edit($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,";
        $sql.= " tbcar.code as car_code,tbcarreserv.member_id,tbmember.name as member_name,tbmember.mobile,tbmember.tel,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();
        
        $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';
            }
        }
        
        $data['row'] = $row;
        $data['mydate'] = $this->mydate;
        $data['page_title'] = $this->page_title;
        $data['content'] = 'approvecar/approve_car_frm_v';
        $this->load->view('template_v', $data);
    }

    function save() {
        $this->load->model('car_m');
        $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('tbcarreserv', $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('tbcarreserv')->row_array();

            $subject = 'แจ้งผลการอนุมัติจองใช้รถ';
            $message = '<b>จองใช้รถเพื่อ</b> ' . $res['name'] . "<br/>";
            $message.= '<b>วันที่จอง</b> ' . $this->mydate->dateToText($res['rdate']) . " <b>เวลา</b> " . $res['start_time'] . " <b>ถึง</b> " . $res['end_time'];
            $message.= " <b>ทะเบียนรถ</b> " . $this->car_m->get_carcode($res['car_id']) . "<br/>";

            $this->db->where('doc_id', $id);
            $this->db->where('wtype', '0');
            $booking = $this->db->get('tbbooking_car')->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->car_m->get_carcode($b['car_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("approve_car/edit/$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;
    }

}

?>
