<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

	class Reservation extends CI_Controller {

	   function __construct()
	   {
			parent::__construct();
			$this->load->library(array('encrypt', 'email'));
			$this->load->model("Mod_reservation","reservation");
	   }		

        public function index()
        {
			$session_id = $this->session->userdata('trn_user');
			$id = $this->encrypt->decode($session_id);
			$reservation = $this->reservation->getById($id);

			if(!$reservation)
			{
				$this->session->set_flashdata('message',"Please Booking Again!");
				redirect("Home");
				exit();
			}
			
			$data['reservation'] = $reservation;
			$RID           = $reservation->RID;
			$brand_id      = $reservation->brand_id;
			$service       = $reservation->services_id;
			$pickup_other  = $reservation->pick_up_other;
			$dropoff_other = $reservation->drop_off_other;
			$to_time 	   = $reservation->to_time;
			$to_date 	   = $reservation->to_date;
			$from_time 	   = $reservation->from_time;
			$from_date 	   = $reservation->from_date;
			$from_location = $reservation->pickup_location;
			$to_location   = $reservation->return_location;
			$from_name     = $reservation->pickupname;
			$to_name       = $reservation->dropOffname;
			$up_other      = $reservation->pick_up_other;
			$off_other     = $reservation->drop_off_other;
			$car_id        = $reservation->car_id;
			$car_name      = $reservation->carname;
			$service_name  = $reservation->servicename;
			$price         = $this->reservation->getprice($RID,$brand_id,$service);
			$peaktime      = $this->reservation->getpeaktime($service,$from_time,$to_time,$to_location,$from_location,$brand_id);
			
			$data['price'] = $price;
			$data['peaktime'] = $peaktime;

			if(!$price)
			{
				$total = "Please Contact Us <div>Phone: 095-232425</div> <div>Email: sales@channellimo.com</div>";
				$price_online = "";	
			}
			else
			{
				if($peaktime)
				{
					$total = $peaktime->amount+$price->price .'USD'.'PeakTime:'.$peaktime->amount."$";
					$price_online = $peaktime->amount+$price->price;
				}
				else
				{
					$total = $price->price .'USD';	
					$price_online = $price->price;
				}
			}

			if($from_location >0)
			{
				$pickup_name = $from_name;
			}
			else
			{
				$pickup_name = $reservation->pick_up_other;	
			}

			if($to_location >0)
			{
				$dropoff_name = $to_name;
			}
			else
			{
				$dropoff_name = $reservation->drop_off_other;	
			}

			$this->form_validation->set_rules('firstname', 'First Name', 'trim|required');
			$this->form_validation->set_rules('lastname', 'Last Name', 'trim|required');
			$this->form_validation->set_rules('phonenumber', 'Phone Number', 'required|numeric');
			$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
			$this->form_validation->set_rules('email_confirm', 'confirm email', 'required|matches[email]');
			$this->form_validation->set_rules('city', 'city', 'required');
			$this->form_validation->set_rules('agree', 'Confirm Box', 'required');

			if(isset($_POST['guest']) == 0)
			{
				$this->form_validation->set_rules('con_firstname', 'contact firstname', 'required');
				$this->form_validation->set_rules('con_lastname', 'contact lastname', 'required');
				$this->form_validation->set_rules('con_phonenumber', 'contact phonenumber', 'required');
				$this->form_validation->set_rules('con_city', 'contact city', 'required');
				$this->form_validation->set_rules('con_email', 'contact email', 'required|valid_email');
				$this->form_validation->set_rules('con_email_confirm','contact confirm email','required|matches[con_email]');	
			}

			if ($this->form_validation->run() == TRUE)
			{
				$post['trn_id'] = $id;
				$post['guest_firstname'] = trim($_POST['firstname']);
				$post['guest_lastname'] = trim($_POST['lastname']);
				$post['guest_phone'] = trim($_POST['phonenumber']);
				$post['guest_email'] = trim($_POST['email']);
				$post['guest_city'] = trim($_POST['city']);

				if (isset($_POST['guest']) == 0)
				{
					$con_firstname = $post['contact_firstname'] = trim($_POST['con_firstname']);
					$con_lastname = $post['contact_lastname'] = trim($_POST['con_lastname']);
					$con_phone = $post['contact_phone'] = trim($_POST['con_phonenumber']);
					$con_email = $post['contact_email'] = trim($_POST['con_email']);
					$con_city = $post['contact_city'] = trim($_POST['con_city']);		
				}
				else
				{
					$con_firstname = $post['contact_firstname'] = trim($_POST['firstname']);
					$con_lastname = $post['contact_lastname'] = trim($_POST['lastname']);
					$con_phone = $post['contact_phone'] = trim($_POST['phonenumber']);
					$con_email = $post['contact_email'] = trim($_POST['email']);
					$con_city = $post['contact_city'] = trim($_POST['city']);	
				}

				$post['createdate'] = date('y/m/d H:i:sa');	
				$this->reservation->customer($post);

				$reserve = array(
						'trn_id' =>$id,
						'brand_id' => $brand_id,
						'car_id' => $car_id,
						'services_id' => $service,
						'RID' => $RID,
						'pickup_location' => $from_location,
						'return_location' => $to_location,
						'pick_up_other' => $up_other,
						'drop_off_other' => $off_other,
						'from_date' => $from_date,
						'from_time' => $from_time,
						'to_date' => $to_date,
						'to_time' => $to_time,
						'createdate' => date('y/m/d H:i:sa'),
				);
				$this->reservation->reserve($reserve);

				$mydata = array(
						'id'=>$id,
						'first-name'	=>	trim($_POST['firstname']),
						'last-name'		=>	trim($_POST['lastname']),
						'phone-number'	=>	trim($_POST['phonenumber']),
						'email-address' =>	trim($_POST['email']),
						'city'			=>	trim($_POST['city']),
						'car_name'		=>	$car_name,
						'from_date'		=>	$from_date,
						'from_time'		=>	$from_time,
						'to_date'		=>	$to_date,
						'to_time'		=>	$to_time,
						'pickup_name'	=>	$pickup_name,
						'dropoff_name'	=>	$dropoff_name,
						'service_name'	=>	$service_name,
						'price'			=>	$total,
						'con_firstname' =>	$con_firstname,
						'con_lastname'  =>	$con_lastname,
						'con_phone'		=>	$con_phone,
						'con_email'		=>	$con_email,
						'con_city'		=>	$con_city,
				);
				
				$this->send($mydata);
				$this->session->unset_userdata('trn_user');
				if(is_numeric($price_online) and $price_online != "" and $price_online > 0)
				{
					$tem_id = $this->encrypt->encode($id);
					$price = $this->encrypt->encode($price_online);
					$newdata = array(
						'lastreffnumber' => $tem_id,
						'price'          => $price 
					);
			
					$this->session->set_userdata($newdata);
					redirect("payment/VPC_PHP_3P_DO");
					exit();
				}
				else
				{
					$tem_id = $this->encrypt->encode($id);
					$newdata = array(
						'tem_id'     => $tem_id
					);
					
					$this->session->set_userdata($newdata);
					redirect("reservation/complete");	
					exit();
				}
			}

			$this->load->view('header');
			$this->load->view('pages/reservation',$data);
			$this->load->view('footer');
        }		

		function complete()
		{
			$session_id = $this->session->userdata('tem_id');
			$id = $this->encrypt->decode($session_id);
			$reservation = $this->reservation->complete($id);
			if(!$reservation)
			{
				redirect("Home");
				exit();
			}
			
			$data['reservation'] = $reservation;
			$this->load->view('header');
			$this->load->view('pages/reserve_complete',$data);
			$this->load->view('footer');
		}

		private function send($mydata)
		{	
			$to_cc = array('sales@channellimo.com','admin@channellimo.com','accounts@channellimo.com');
			$to_bcc = array('admin@csdcambodia.com','accounts@csdcambodia.com','eungsovan@gmail.com','reasey@ptmcambodia.com');
			$subject = 'Your Channel Limousine reservation number';
			$from = 'sales@channellimo.com';
			
			$msg = "Thank you for booking with Channel Limousine.  Your booking is in process and our team will send you your invoice within the next hour after confirmation.<br/><br/>
					We wish you a pleasant stay.<br/><br/>
					This is Your Booking Detail!<br/><br/><br/>
					<table width=500 style='border:solid #BBD0F7 2px;'><tr style='background-color:#EEE;'>
					<td><font color=red>Booking Detail</font></td>
					</tr>
					<tr><td>Code <td>: ".$mydata['id']." \n</td></tr>
					<tr><td>Car Type <td>: ".$mydata['car_name']." \n</td></tr>
					<tr><td>Pick-Up Date&Time <td>: ".$mydata['from_date']." ".$mydata['from_time'].".
					<tr><td>Drop-Off Date&Time <td>: ".$mydata['to_date']." ".$mydata['to_time'].".
					<tr><td>Pick-Up Location <td>: ".$mydata['pickup_name']."
					<tr><td>Drop-Off Location <td>: ".$mydata['dropoff_name']."
					<tr><td>Service<td>: ".$mydata['service_name']."			
					<tr><td>Total Price<td><font color=red>: ".$mydata['price']."</font>
					</table><br/>
					<p>Channel Limousine team. New booking from Channel Limousine Online Booking, please follow up this mail!</p>
					<table width=500 style='border:solid #BBD0F7 2px;'><tr style='background-color:#EEE;'>
						<td><font color=red>Guest Information</font></td>
						<tr><td>First Name <td>: ".$mydata['first-name']." \n</td></tr>
						<tr><td>Last Name <td>: ".$mydata['last-name']."
						<tr><td>Phone Number <td>: ".$mydata['phone-number']."
						<tr><td>Email <td>: ".$mydata['email-address']."
						<tr><td>City <td>: ".$mydata['city']."
					</table><br/>

					<table width=500 style='border:solid #BBD0F7 2px;'><tr style='background-color:#EEE;'>
						<td><font color=red>Contact Information</font></td>
						<tr><td>First Name <td>: ".$mydata['con_firstname']." \n</td></tr>
						<tr><td>Last Name <td>: ".$mydata['con_lastname']."
						<tr><td>Phone Number <td>: ".$mydata['con_phone']."
						<tr><td>Email <td>: ".$mydata['con_email']."
						<tr><td>City <td>: ".$mydata['con_city']."
					</table><br/>";

					$config['charset'] = 'iso-8859-1';
					$config['mailtype'] = 'html';
					$this->email->initialize($config);
					$this->email->from($from);
					$this->email->to($mydata['email-address']);
					$this->email->cc($to_cc); 
					$this->email->bcc($to_bcc);
					$this->email->subject($subject);
					$this->email->message($msg);
					$this->email->send();

					//$this->email->print_debugger();

		}

}