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

	class Home extends Site_Controller {
		
	   function __construct()
	   {
			parent::__construct();
			$this->load->library(array('encrypt'));
			$this->load->helper(array('string'));
			$this->load->model(array("Mod_car","Mod_checkout"));
	   }		

        public function index()
        {
			$data["car"] = $this->Mod_car->getcar();
			$data["car"] = $data["car"]->result_array();
			
			$this->form_validation->set_message('check_default', 'You need to select something other than the default');
			$this->form_validation->set_rules('car_select', 'Car', 'callback_check_default');
			$this->form_validation->set_rules('services_for', 'Services', 'callback_check_default');
			$this->form_validation->set_rules('pick_up_date', 'Pick-up-date', 'required');

			if($this->input->post('services_for',TRUE))
			{

				$service_for = $this->input->post('services_for',TRUE);
				
				if($service_for == 1)
				{
					$this->form_validation->set_rules('pick_up_location', 'Pick-up-location', 'callback_check_default|required');
					$this->form_validation->set_rules('drop_off_location', 'Drop-off-location', 'callback_check_default|required');

					if($this->input->post('pick_up_location',TRUE)=="other")
					{
						$this->form_validation->set_rules('pick_up_other', 'Pick-up-other', 'required|max_length[100]');
					}

					if($this->input->post('drop_off_location',TRUE)=="other")
					{
						$this->form_validation->set_rules('drop_off_other', 'Drop-off-other', 'required|max_length[100]');
					}

					if($this->input->post('pick_up_location',TRUE)=="hotel")
					{
						$this->form_validation->set_rules('pick_up_hotel', 'Pick-up-hotel', 'required');
					}
				
					if($this->input->post('drop_off_location',TRUE)=="hotel")
					{
						$this->form_validation->set_rules('drop_off_hotel', 'Drop-off-hotel', 'required');
					}//exit-service 1
				}
				elseif($service_for == 2 or $service_for == 3)
				{
					if($this->input->post('pick_up_location',TRUE)=="hotel")
					{
						$this->form_validation->set_rules('pick_up_hotel', 'Pick-up-hotel', 'required');
					}
					elseif($this->input->post('pick_up_location',TRUE)=="other" || $this->input->post('pick_up_location',TRUE)=="")
					{
						$this->form_validation->set_rules('visit_place', 'Place to visit and drop off','required');
					}
					else
					{
						$this->form_validation->set_rules('pick_up_location', 'Pick-up-location', 'callback_check_default|required'); 
					}
				}//exit-service 2 and 3
				else
				{
					$this->session->set_flashdata('message',"Please select our service!");
					redirect("Home");
					exit();
				}
			}
			//Start Services

			if ($this->form_validation->run() == TRUE)
			{

				$trn_id = random_string('nozero');
				$service = $this->input->post('services_for',TRUE);
				$from = $this->input->post('pick_up_location',TRUE);
				$to = $this->input->post('drop_off_location',TRUE);
				$brand_id = $this->input->post('car_select',TRUE);
				$car_id = $this->Mod_checkout->getcar($brand_id);
				$from_date = date("y/m/d", strtotime($this->input->post('pick_up_date',TRUE)));
				$from_time = date("H:i", strtotime($this->input->post('pick_up_time',TRUE)));
				$to_date = date("y/m/d", strtotime($this->input->post('pick_up_date',TRUE)));	

				if($service == "1")
				{					
					if($from > 0)
					{
						$pickup_from = $from;
					}
					elseif($from == "hotel")
					{
						$pick_hotel = $this->input->post('pick_up_hotel',TRUE);
						$pickup_from = $this->Mod_checkout->pickup_hotel($pick_hotel);
						if($pickup_from <= 0)
						{
							$pickup_from = $this->input->post('pick_up_hotel',TRUE);
						}
					}
					else
					{
						$pickup_from = $this->input->post('pick_up_other',TRUE);
					}

					if($to > 0)
					{
						$dropoff_to = $to;
					}
					elseif($to == "hotel")
					{
						$off_hotel = $this->input->post('drop_off_hotel',TRUE);
						$dropoff_to = $this->Mod_checkout->dropoff_hotel($off_hotel);
						if($dropoff_to <= 0)
						{
							$dropoff_to = $this->input->post('drop_off_hotel',TRUE);
						}
					}
					else
					{
						$dropoff_to = $this->input->post('drop_off_other',TRUE);
					}

					if($pickup_from == $dropoff_to)
					{
						$this->session->set_flashdata('message',"Sorry Location and Hotel Do not match!");
						redirect('Home');
						exit();
					}

					$free_time = $this->Mod_checkout->free_time($pickup_from,$dropoff_to);

					if($free_time > 0)
					{
						$secs = strtotime($free_time)-strtotime("00:00");
						$to_time = date("H:i",strtotime($from_time).'+30 minutes'+$secs);
					}
					else
					{
						$to_time = date("H:i", strtotime($from_time.'+1 hours'.'+30 minutes'));	
					}

					$check = $this->Mod_checkout->reserve($brand_id,$from_date,$from_time,$to_date,$to_time);
					if($check->num_rows() == 0)
					{
						$post['RID'] = $this->Mod_checkout->tranfer($pickup_from,$dropoff_to,$service);
						if($pickup_from > 0)
						{
							$post['pickup_location'] = $pickup_from;
						}
						else
						{
							$post['pick_up_other'] = $pickup_from;
						}

						if($dropoff_to > 0)
						{
							$post['return_location'] = $dropoff_to;
						}
						else
						{
							$post['drop_off_other'] = $dropoff_to;
						}

						$post['to_date'] = $to_date;
						$post['to_time'] = $to_time;
						$post['from_date'] = $from_date;
						$post['from_time'] = date("H:i", strtotime($from_time.'-30 minutes'));
						$post['brand_id'] = $brand_id;
						$post['car_id'] = $car_id;
						$post['services_id'] = $service;
						$post['createdate'] = date('y/m/d H:i:sa');
						$post['trn_id'] = $trn_id;
						$trn_user = $this->encrypt->encode($trn_id);
						$this->Mod_checkout->add($post);

						$newdata = array(
							'trn_user'     => $trn_user
						);

						$this->session->set_userdata($newdata);
						redirect('Reservation');
						exit();
					}
					else
					{
						$this->session->set_flashdata('message',"Sorry this Car had been Booking!");
						redirect('Home');
						exit();
					}
				}
				elseif($service == "2" OR $service == "3")
				{
					if($service == "2")
					{
						$to_time = date("H:i", strtotime($from_time.'+5 hours'));
					}
					elseif($service == "3")
					{
						$to_time = date("H:i", strtotime($from_time.'+10 hours'));	
					}
					else
					{
						$to_time = "00:00:00";
					}

					if($from > 0)
					{
						$pickup_from = $from;
					}
					elseif($from == "hotel")
					{
						$pick_hotel = $this->input->post('pick_up_hotel',TRUE);
						$pickup_from = $this->Mod_checkout->pickup_hotel($pick_hotel);

						if($pickup_from <= 0)
						{
							$pickup_from = $this->inupt->post('pick_up_hotel',TRUE);
						}
					}
					else
					{
						$pickup_from = $this->input->post('pick_up_other',TRUE);
					}

					$to_times = date("H:i", strtotime($to_time.'+30 minutes'));
					$check = $this->Mod_checkout->reserve($brand_id,$from_date,$from_time,$to_date,$to_times);

					if($check->num_rows() == 0)
					{
						if($pickup_from > 0)
						{
							$post['pickup_location'] = $pickup_from;
						}
						else
						{
							$post['pick_up_other'] = $pickup_from;
						}
							
						$post = array(
								'RID'			 =>	$this->Mod_checkout->services($pickup_from,$service),
								'to_date'		 =>	$to_date,
								'to_time'		 =>	$to_time,
								'from_date'		 =>	$from_date,
								'from_time'		 =>	date("H:i", strtotime($from_time.'-30 minutes')),
								'drop_off_other' =>	$this->input->post('visit_place',TRUE),
								'brand_id'		 =>	$brand_id,
								'car_id'		 =>	$car_id,
								'services_id'	 =>	$service,
								'createdate'	 =>	date('y/m/d H:i:sa'),
								'trn_id'		 =>	$trn_id,
						);
							
						$trn_user = $this->encrypt->encode($trn_id);
							
						$this->Mod_checkout->add($post);
						$newdata = array(
									'trn_user' => $trn_user
						);
						
						$this->session->set_userdata($newdata);
						redirect('Reservation');
						exit();
					}
					else
					{
						$this->session->set_flashdata('message',"Sorry this Car had been Booking!");
						redirect('Home');
						exit();
					}
				 }
				 elseif($service == "4")
				 {
					 $up_other = $this->input->post('up_other_location',TRUE);
					 $off_other = $this->input->post('off_other_location',TRUE);
					 $this->form_validation->set_rules('up_other_location','Pickup Other','required|max_length[100]');
					 $this->form_validation->set_rules('off_other_location','Dropoff Other','required|max_length[100]');

					 if($this->form_validation->run() == TRUE)
					 {
						 $data = array(
								 'RID'			  => $this->Mod_checkout->tranfer($from,$to,$service),
								 'pick_up_other'  => $up_other,
								 'drop_off_other' => $off_other,
								 'from_date'   	  => $from_date,
								 'from_time'      => $from_time,
								 'brand_id'	      => $brand_id,
								 'car_id'	      => $car_id,
								 'services_id'    => $service,
								 'createdate'     => date('y/m/d H:i:sa'),
								 'trn_id'	      => $trn_id,
						 );

						$trn_user = $this->encrypt->encode($trn_id);
						$this->Mod_checkout->add($data);
						$newdata = array(
									'trn_user'     => $trn_user
						);

						$this->session->set_userdata($newdata);
						redirect('Reservation');
						exit();
					 }
				 }
				 else
				 {
						$this->session->set_flashdata('message',"Please Booking Again!");
						redirect('Home');	 
						exit();
				 }
				//Exit-Services	
			}

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

        }

		function getlocation()
	    {	
			$ID = $this->input->POST('Id');
			$data['routes'] = $this->Mod_checkout->getlocation($ID);
			$routes = $data['routes']->result_array();
			
			echo'<option value="0">Choose pickup location</option>';
			echo'<option value="hotel">Hotel</option>';
			foreach($routes as $v)
			{
				echo'<option value="'.$v['id'].'">'.$v['locationname'].'</option>';	 
			}
			echo'<option value="other">Other</option>';
	    }

		function check_default($post_string)
		{
		  return $post_string == '0' or $post_string == ''  ? FALSE : TRUE;
		}
	
}