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

class Booktable extends CI_Controller {
     
	public function __construct()
	{
	    parent::__construct();
	    
	} 
	
	public function book_table()
	{	
		$this->form_validation->set_rules('book_date','Date', 'required');	
		$this->form_validation->set_rules('book_time','Time', 'required');
		$this->form_validation->set_rules('book_name','Name', 'required');
		$this->form_validation->set_rules('book_email','Email', 'required');
		 
		if ($this->form_validation->run() == FALSE)
		{
			echo 0; 
			
		}else{
			
			$resv_tabid = $this->input->post('booktable_id');
			$availability = 0;
			if($resv_tabid != 0)
			{
				$resv_date = $this->input->post('book_date');
				$resv_time = $this->input->post('book_time');
				$check_availability = $this->Data_access_model->select_data('*','hs_reservation_details',array('booked_table'=>$resv_tabid,'reservation_date'=>$resv_date,'reservation_time'=>$resv_time));
				if($check_availability->num_rows() > 0)
				{
					$check_table = $check_availability->row();
					if($check_table->reservation_date == $resv_date && $check_table->reservation_time == $resv_time){
						$availability++;
					}							
				}
			}
			
			if($availability == 0)
			{		
				$data = array('reservation_uniqid'=>substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 8),
				'reservation_date' =>$this->input->post('book_date'),
				'reservation_time' =>$this->input->post('book_time'),
				'people' =>$this->input->post('no_person'), 
				'booked_table' =>$this->input->post('booktable_id'),
				'occasion' =>$this->input->post('book_occasion'),
				'preferred_food' => $this->input->post('book_preferredfood'),
				'massage' =>  $this->input->post('book_msg'),
				'name' =>$this->input->post('book_name'),
				'email' =>$this->input->post('book_email'),
				'reserv_receivedate' =>date('d M Y H:i:s'),
				'reservation_status' =>0,
				);
				$this->Data_access_model->insert_data('hs_reservation_details',$data);
				$reservation_id = $this->db->insert_id();  
				if($this->db->affected_rows() >0)
				{
					$resrvation_details = $this->Data_access_model->select_data('*','hs_reservation_details',array('reservation_id'=>$reservation_id));
					if($resrvation_details->num_rows() > 0)
					{
						$reserv_info = $resrvation_details->row();
						$user_email = $reserv_info->email;
						$table_details = $this->Data_access_model->select_data('*','hs_table_details',array('table_id'=>$reserv_info->booked_table));
						if($table_details->num_rows()>0)
						{
							$table_info = $table_details->row();
							$table_no = $table_info->table_no; 
						}else{
							$table_no ='';
						}
								
						$email_content = $this->hs->getwebsettings('tablebook_template');  
						$email_content = str_replace("[booking_number]",$reserv_info->reservation_uniqid, $email_content);
						$email_content = str_replace("[username]",$reserv_info->name, $email_content);
						$email_content = str_replace("[booking_date]", $reserv_info->reservation_date,$email_content);
						$email_content = str_replace("[booking_time]", $reserv_info->reservation_time,$email_content);
						$email_content = str_replace("[table_number]", $table_no,$email_content);
						$email_content = str_replace("[break]", "<br>", $email_content);
						$data['emcontent'] = $email_content;
						$email_template = $this->load->view('theme/email_template',$data,true);
						$subject = $this->hs->getwebsettings('tablebook_subject');
						//sending mail to user
						$this->hs->send_mail($user_email,$subject,$email_template);
						//sending mail to admin
						$admin_emailid = $this->hs->getwebsettings('sender_mailid'); 
						$admin_emcontent = $this->hs->getwebsettings('admintbook_template');  
						$admin_emcontent = str_replace("[booking_number]",$reserv_info->reservation_uniqid, $admin_emcontent);
						$admin_emcontent = str_replace("[username]",$reserv_info->name, $admin_emcontent);
						$admin_emcontent = str_replace("[booking_date]", $reserv_info->reservation_date,$admin_emcontent);
						$admin_emcontent = str_replace("[booking_time]", $reserv_info->reservation_time,$admin_emcontent);
						$admin_emcontent = str_replace("[table_number]", $table_no,$admin_emcontent);
						$admin_emcontent = str_replace("[break]", "<br>", $admin_emcontent);
						$data['emcontent'] = $admin_emcontent;
						$admin_emtemplate = $this->load->view('theme/email_template',$data,true);
						$admin_subject = $this->hs->getwebsettings('admintbook_subject');
						//sending mail to admin
						$this->hs->send_mail($admin_emailid,$admin_subject,$admin_emtemplate);
					} 
						echo 1;
					}
			}else{
				echo 2;
			}
		}
    }	
}

 