<?php
if (!defined("ROOT_PATH"))
{
	header("HTTP/1.1 403 Forbidden");
	exit;
}
class pjFrontPublic extends pjFront
{
	public function __construct()
	{
		parent::__construct();
		
		$this->setAjax(true);
		
		$this->setLayout('pjActionEmpty');
	}
	
	public function pjActionFleets()
	{
		if($this->isXHR())
		{
			$pjFleetModel = pjFleetModel::factory();
			$fleet_arr = $pjFleetModel
				->join('pjMultiLang', "t2.model='pjFleet' AND t2.foreign_id=t1.id AND t2.field='fleet' AND t2.locale='".$this->getLocaleId()."'", 'left outer')
				->join('pjMultiLang', "t3.model='pjFleet' AND t3.foreign_id=t1.id AND t3.field='description' AND t3.locale='".$this->getLocaleId()."'", 'left outer')
				->select("t1.*, t2.content as fleet, t3.content as description")
				->where('t1.status', 'T')
				->orderBy("fleet ASC")
				->findAll()->getData();
			
			$service_arr = array();
			$fleet_id_arr = $pjFleetModel->findAll()->getDataPair(null, 'id');
			if(!empty($fleet_id_arr))
			{
				$temp_service_arr = pjServiceModel::factory()
					->join('pjMultiLang', "t2.model='pjService' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')
					->select("t1.*, t2.content as name")
					->whereIn('fleet_id', $fleet_id_arr)
					->orderBy("name ASC")
					->findAll()->getData();
				foreach($temp_service_arr as $k => $v)
				{
					$service_arr[$v['fleet_id']][] = $v;
				}
			}
							
			$this->set('fleet_arr', $fleet_arr);
			$this->set('service_arr', $service_arr);
		}
	}
	
	public function pjActionCheckout()
	{
		if($this->isXHR())
		{
			if (isset($_SESSION[$this->defaultStore]) &&
					count($_SESSION[$this->defaultStore]) > 0 &&
					isset($_SESSION[$this->defaultStore]['fleet_id']) &&
					isset($_SESSION[$this->defaultStore]['service_id']))
			{
				if(isset($_POST['lbs_checkout']))
				{
					if ((int) $this->option_arr['o_bf_include_captcha'] === 3 && (!isset($_POST['captcha']) ||
							!pjCaptcha::validate($_POST['captcha'], $_SESSION[$this->defaultCaptcha]) ))
					{
						pjAppController::jsonResponse(array('status' => 'ERR', 'code' => 110));
					}
					
					$date_time = pjUtil::formatDate($_POST['booking_date'], $this->option_arr['o_date_format']) . ' ' . date("H:i:s", strtotime($_POST['booking_time']));
					$date_time_ts = strtotime($date_time);
					if(time() + $this->option_arr['o_hour_earlier'] * 3600 > $date_time_ts)
					{
						pjAppController::jsonResponse(array('status' => 'ERR', 'code' => 120));
					}
					
					$_SESSION[$this->defaultForm] = $_POST;
						
					pjAppController::jsonResponse(array('status' => 'OK', 'code' => 200));
				}else{
					$fleet_arr = pjFleetModel::factory()
						->join('pjMultiLang', "t2.model='pjFleet' AND t2.foreign_id=t1.id AND t2.field='fleet' AND t2.locale='".$this->getLocaleId()."'", 'left outer')
						->join('pjMultiLang', "t3.model='pjFleet' AND t3.foreign_id=t1.id AND t3.field='description' AND t3.locale='".$this->getLocaleId()."'", 'left outer')
						->select("t1.*, t2.content as fleet, t3.content as description")
						->find($_SESSION[$this->defaultStore]['fleet_id'])->getData();
					$this->set('fleet_arr', $fleet_arr);
					
					$country_arr = pjCountryModel::factory()
						->select('t1.id, t2.content AS country_title')
						->join('pjMultiLang', "t2.model='pjCountry' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')
						->orderBy('`country_title` ASC')
						->findAll()
						->getData();
					
					$this->set('country_arr', $country_arr);
					
					$service_arr = pjServiceModel::factory()
						->join('pjMultiLang', "t2.model='pjService' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')
						->select("t1.*, t2.content as name")
						->find($_SESSION[$this->defaultStore]['service_id'])->getData();
					$this->set('service_arr', $service_arr);
					
					$reason_id_arr = pjReasonServiceModel::factory()->where('service_id', $_SESSION[$this->defaultStore]['service_id'])->findAll()->getDataPair(null, 'reason_id');
					if(!empty($reason_id_arr))
					{
						$reason_arr = pjReasonModel::factory()
							->select('t1.id, t2.content AS name')
							->join('pjMultiLang', "t2.model='pjReason' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')
							->whereIn('t1.id', $reason_id_arr)
							->orderBy('t2.content ASC')
							->findAll()
							->getData();
							
						$this->set('reason_arr', $reason_arr);
					}
					
					$pjServiceExtraModel = pjServiceExtraModel::factory();
					$included_extra_arr = $pjServiceExtraModel
						->join('pjMultiLang', "t2.model='pjExtra' AND t2.foreign_id=t1.extra_id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')
						->join('pjExtra', "t1.extra_id=t3.id", 'left')
						->select("t1.*, t2.content as name, t3.price, t3.per")
						->where('t1.service_id', $_SESSION[$this->defaultStore]['service_id'])
						->orderBy("name ASC")
						->findAll()->getData();
					$this->set('included_extra_arr', $included_extra_arr);
					
					$included_extra_id_arr = $pjServiceExtraModel->findAll()->getDataPair(null, 'extra_id');
					
					$pjFleetExtraModel = pjFleetExtraModel::factory()
						->join('pjMultiLang', "t2.model='pjExtra' AND t2.foreign_id=t1.extra_id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')
						->join('pjExtra', "t1.extra_id=t3.id", 'left')
						->select("t1.*, t2.content as name, t3.price, t3.per")
						->where('t1.fleet_id', $_SESSION[$this->defaultStore]['fleet_id'])
						->orderBy("name ASC");
					if(!empty($included_extra_id_arr))
					{
						$pjFleetExtraModel->whereNotIn('extra_id', $included_extra_id_arr);
					}
					$avail_extra_arr = $pjFleetExtraModel->findAll()->getData();
					$this->set('avail_extra_arr', $avail_extra_arr);
					
					$passengers = isset($_SESSION[$this->defaultForm]['passengers']) ? $_SESSION[$this->defaultForm]['passengers'] : 1;
					$extra_id_arr = isset($_SESSION[$this->defaultForm]['extra_id']) && is_array($_SESSION[$this->defaultForm]['extra_id']) ? array_keys($_SESSION[$this->defaultForm]['extra_id']) : array();
					$price_arr = pjAppController::calPrice($_SESSION[$this->defaultStore]['fleet_id'], $_SESSION[$this->defaultStore]['service_id'], $extra_id_arr, $passengers, $this->option_arr);
					
					$this->set('price_arr', $price_arr);
					$this->set('passengers', $passengers);
					$this->set('extra_id_arr', $extra_id_arr);
				}
			}else{
				pjAppController::jsonResponse(array('status' => 'ERR', 'code' => 100, 'text' => ''));
			}
		}
	}
	
	public function pjActionGetPrices()
	{
		if($this->isXHR())
		{
			if (isset($_SESSION[$this->defaultStore]) &&
					count($_SESSION[$this->defaultStore]) > 0 &&
					isset($_SESSION[$this->defaultStore]['fleet_id']) &&
					isset($_SESSION[$this->defaultStore]['service_id']))
			{
				
				$pjServiceExtraModel = pjServiceExtraModel::factory();
				$included_extra_arr = $pjServiceExtraModel
					->join('pjMultiLang', "t2.model='pjExtra' AND t2.foreign_id=t1.extra_id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')
					->join('pjExtra', "t1.extra_id=t3.id", 'left')
					->select("t1.*, t2.content as name, t3.price, t3.per")
					->where('t1.service_id', $_SESSION[$this->defaultStore]['service_id'])
					->orderBy("name ASC")
					->findAll()->getData();
				$this->set('included_extra_arr', $included_extra_arr);
				
				$included_extra_id_arr = $pjServiceExtraModel->findAll()->getDataPair(null, 'extra_id');
				
				$pjFleetExtraModel = pjFleetExtraModel::factory()
					->join('pjMultiLang', "t2.model='pjExtra' AND t2.foreign_id=t1.extra_id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')
					->join('pjExtra', "t1.extra_id=t3.id", 'left')
					->select("t1.*, t2.content as name, t3.price, t3.per")
					->where('t1.fleet_id', $_SESSION[$this->defaultStore]['fleet_id'])
					->orderBy("name ASC");
				if(!empty($included_extra_id_arr))
				{
					$pjFleetExtraModel->whereNotIn('extra_id', $included_extra_id_arr);
				}
				$avail_extra_arr = $pjFleetExtraModel->findAll()->getData();
				$this->set('avail_extra_arr', $avail_extra_arr);
	
				$passengers = isset($_POST['passengers']) ? $_POST['passengers'] : 1;
				$extra_id_arr = isset($_POST['extra_id']) && is_array($_POST['extra_id']) ? array_keys($_POST['extra_id']) : array();
				
				$price_arr = pjAppController::calPrice($_SESSION[$this->defaultStore]['fleet_id'], $_SESSION[$this->defaultStore]['service_id'], $extra_id_arr, $passengers, $this->option_arr);
	
				$this->set('price_arr', $price_arr);
				$this->set('passengers', $passengers);
				$this->set('extra_id_arr', $extra_id_arr);
			}else{
				pjAppController::jsonResponse(array('status' => 'ERR', 'code' => 100, 'text' => ''));
			}
		}
	}
	
	public function pjActionPreview()
	{
		if($this->isXHR())
		{
			if (isset($_SESSION[$this->defaultStore]) &&
					count($_SESSION[$this->defaultStore]) > 0 &&
					isset($_SESSION[$this->defaultStore]['fleet_id']) &&
					isset($_SESSION[$this->defaultStore]['service_id']))
			{
				$fleet_arr = pjFleetModel::factory()
					->join('pjMultiLang', "t2.model='pjFleet' AND t2.foreign_id=t1.id AND t2.field='fleet' AND t2.locale='".$this->getLocaleId()."'", 'left outer')
					->select("t1.*, t2.content as fleet")
					->find($_SESSION[$this->defaultStore]['fleet_id'])->getData();
				$this->set('fleet_arr', $fleet_arr);
				
				$service_arr = pjServiceModel::factory()
				->join('pjMultiLang', "t2.model='pjService' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')
				->select("t1.*, t2.content as name")
				->find($_SESSION[$this->defaultStore]['service_id'])->getData();
				$this->set('service_arr', $service_arr);
				
				$passengers = isset($_SESSION[$this->defaultForm]['passengers']) ? $_SESSION[$this->defaultForm]['passengers'] : 1;
				$extra_id_arr = isset($_SESSION[$this->defaultForm]['extra_id']) && is_array($_SESSION[$this->defaultForm]['extra_id']) ? array_keys($_SESSION[$this->defaultForm]['extra_id']) : array();
				$price_arr = pjAppController::calPrice($_SESSION[$this->defaultStore]['fleet_id'], $_SESSION[$this->defaultStore]['service_id'], $extra_id_arr, $passengers, $this->option_arr);
					
				$this->set('price_arr', $price_arr);
				$this->set('passengers', $passengers);
				
				$reason_arr = array();
				if (isset($_SESSION[$this->defaultForm]['reason_id']) && $_SESSION[$this->defaultForm]['reason_id'])
				{
					$reason_arr = pjReasonModel::factory()
						->select('t1.id, t2.content AS name')
						->join('pjMultiLang', "t2.model='pjReason' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')
						->orderBy('t2.content ASC')
						->find($_SESSION[$this->defaultForm]['reason_id'])
						->getData();
				}
				$this->set('reason_arr', $reason_arr);
				
				$country_arr = pjCountryModel::factory()
					->select('t1.id, t2.content AS country_title')
					->join('pjMultiLang', "t2.model='pjCountry' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')
					->find($_SESSION[$this->defaultForm]['c_country'])
					->getData();
				$this->set('country_arr', $country_arr);
			}else{
				pjAppController::jsonResponse(array('status' => 'ERR', 'code' => 100, 'text' => ''));
			}
		}
	}
	
	public function pjActionGetPaymentForm()
	{
		if ($this->isXHR())
		{
			$arr = pjBookingModel::factory()->find($_GET['booking_id'])->getData();
	
			if (!empty($arr))
			{
				switch ($arr['payment_method'])
				{
					case 'paypal':
						$this->set('params', array(
						'name' => 'lbsPaypal',
						'id' => 'lbsPaypal',
						'business' => $this->option_arr['o_paypal_address'],
						'client_id' => $this->option_arr['o_paypal_client_id'],
						'client_secret' => $this->option_arr['o_paypal_client_secret'],
						'item_name' => pjSanitize::html($arr['uuid']),
						'custom' => $arr['id'],
						'amount' => $arr['deposit'],
						'currency_code' => $this->option_arr['o_currency'],
						'return' => $this->option_arr['o_thankyou_page'],
						'failure_url' => $this->option_arr['o_paypal_cancel_url'],
						'notify_url' => PJ_INSTALL_URL . 'index.php?controller=pjFrontEnd&action=pjActionConfirmPaypal',
						'target' => '_self',
						'charset' => 'utf-8'
								));
					break;
					case 'authorize':
						$this->set('params', array(
						'name' => 'lbsAuthorize',
						'id' => 'lbsAuthorize',
						'target' => '_self',
						'timezone' => $this->option_arr['o_authorize_timezone'],
						'transkey' => $this->option_arr['o_authorize_transkey'],
						'x_login' => $this->option_arr['o_authorize_merchant_id'],
						'x_description' => pjSanitize::html($arr['uuid']),
						'x_amount' => $arr['deposit'],
						'x_invoice_num' => $arr['id'],
						'x_receipt_link_url' => $this->option_arr['o_thankyou_page'],
						'x_relay_url' => PJ_INSTALL_URL . 'index.php?controller=pjFrontEnd&action=pjActionConfirmAuthorize'
								));
					break;
					case 'stripe':
						$this->set('params', array(
						'name' => 'lbsStripe',
						'id' => 'lbsStripe',
						'target' => '_self',
						'public_key' => $this->option_arr['o_stripe_public_key'],
						'private_key' => $this->option_arr['o_stripe_private_key'],
						'item_name' => pjSanitize::html($arr['uuid']),
						'custom' => $arr['id'],
						'amount' => $arr['deposit'],
						'currency_code' => $this->option_arr['o_currency'],
						'locale_id' => @$arr['locale_id'],
						'cancel_hash' => sha1($arr['uuid'].strtotime($arr['created']).PJ_SALT),
						'return' => $this->option_arr['o_thankyou_page'],
						'notify_url' => PJ_INSTALL_URL . 'index.php?controller=pjFrontEnd&action=pjActionConfirmStripe'
								));
					break;
				}
			}
			$this->set('arr', $arr);
			$this->set('get', $_GET);
		}
	}
}
?>