<?php
/*******************************************************************************
 *                    _ _ _ _
 *                   / _ _  /
 *            _ _   / /__/ /_ _   ____ _ _   _ _ _    __
 *           / _ ` /   _ _// _ ` / __// _ ` / _ \\ \ / /
 *          / _ _// /\ \  / _ _/ \_ \/ _ _// / \/ \ ` /
 *          \_ , /_/  \_\ \_ ,  /__ /\_ , /_/      \_/
 *
 *******************************************************************************
 *    Application:  eReserv
 *    Version:      6
 *    Author:       David Di Certo
 *    Website:      http://www.cogites.com
 *    Email:        contact@cogites.com
 *    Copyright:    2013 - David Di Certo
 *******************************************************************************
 *    == FR ==      La revente et la redistribution de l'application sont
 *                  strictement interdites.
 *
 *                  Vous êtes tenu d'avoir pris connaissance des
 *                  Conditions Générales de Vente disponibles sur Cogites.com
 *                  (y compris pour la version Free).
 *
 *    == EN ==      Resale and redistribution of the application are
 *                  Strictly prohibited.
 *
 *                  You are required to have read the Terms and Conditions
 *                  available on Cogites.com (including the Free version).
 *
 *******************************************************************************
*/

	ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . '../../services');
	ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . '../../services/lib');
	require_once('config/Func.php');
	require_once('config/Logger.php');
	require_once('business/ConfigMgr.php');

	include_once ('business/ReservationMgr.php');
	include_once ('business/TenantMgr.php');
	include_once ('business/TenancyMgr.php');
	include_once ('business/UserMgr.php');
	require_once("config/System.php");

	$reservationMgr = new ReservationMgr();
	$tenantMgr = new TenantMgr();
	$tenancyMgr = new TenancyMgr();
	$userMgr = new UserMgr();
	$sys = new System();

function random_color_part() {
    return str_pad( dechex( mt_rand( 0, 255 ) ), 2, '0', STR_PAD_LEFT);
}

function random_color() {
    return random_color_part() . random_color_part() . random_color_part();
}

	Logger::info('AJAXXXXX: begin');
	if(isset($_POST['func']) && !empty($_POST['func'])) {
		$func = $_POST['func'];
		Logger::info('    AJAXXXXX: func= ' . $func);
		switch($func) {
			case 'isReservationAvailable':
				Logger::info('    AJAXXXXX' . print_r($_POST, true));
				$start = $_POST['start_date'];
				$end = $_POST['end_date'];
				$tenancyid = $_POST['tenancyid'];
				$reservationid = $_POST['reservationid'];
				$arrAllReservations = $reservationMgr->isReservationAvailable($start, $end, $tenancyid, ReservationMgr::RESERV_NEW);
				if (!empty($reservationid)) {
					foreach($arrAllReservations as $k => $v) {
						if($v->reservation_id == $reservationid) {
							unset($arrAllReservations[$k]);
						}
					}
				}
				$res = json_encode($arrAllReservations);
				break;
			case 'getReservationRange':
				Logger::info('    AJAXXXXX' . print_r($_POST, true));
				$range_start = $_POST['start'];
				$range_end = $_POST['end'];
				$arrAllReservations = $reservationMgr->getReservationRange($range_start, $range_end);
				$result = '[';
				$count = 0;
				$len = count($arrAllReservations);
				foreach ($arrAllReservations as $reserv) {
						$result .= '{';
						$result .= '"id": "' . $reserv->reservation_id . '",';
						$result .= '"title": "' . $reserv->tenant_lastname . '",';
						$result .= '"start": "'. $reserv->arrival_date . '",';
						$result .= '"end": "'. $reserv->departure_date . '",';
						$result .= '"allDay": false,';
						$result .= '"ccolor": "#378006",';
						$result .= '"backgroundColor": "#'.random_color() . '"';
						$result .= '}';
						if (($count >= 0) && ($count < $len -1 )) {
							$result .= ',';
						}
						$count++;
				}
				$result .= ']';
				Logger::info('    AJAXXXXX - res before encode: ' . print_r($result, true));
				$res = $result;
				Logger::info('    AJAXXXXX - res after encode: ' . print_r($result, true));
				break;
			case 'getTenancy':
				Logger::info('    AJAXXXXX' . print_r($_POST, true));
				$tenancyid = $_POST['tenancyid'];
				$arrTenancy = $tenancyMgr->getTenancy($tenancyid);
				$res = json_encode($arrTenancy);
				break;
			case 'getTenant':
				Logger::info('    AJAXXXXX' . print_r($_POST, true));
				$tenantid = $_POST['tenantid'];
				$arrTenant = $tenantMgr->getTenant($tenantid);
				Logger::info('    AJAXXXXX' . print_r($arrTenant, true));
				$res = "";
				foreach ($arrTenant as $k => $v) {
					$res .= "<strong>" . $tr->translate("thTenantName") . "</strong>: " . $v->name . '<br>';
					$res .= "<strong>" . $tr->translate("thTenantLastName") . "</strong>: " . $v->lastname . '<br>';
					$res .= "<strong>" . $tr->translate("thTenantName") . "</strong>: " . $v->address1 . '<br>';
					$res .= "<strong>" . $tr->translate("thTenantZipCode") . "</strong>: " . $v->zipcode . '<br>';
					$res .= "<strong>" . $tr->translate("thTenantCity") . "</strong>: " . $v->city . '<br>';
					$res .= "<strong>" . $tr->translate("thTenantCountry") . "</strong>: " . $v->country . '<br>';
					$res .= "<strong>" . $tr->translate("thTenantPhone") . "</strong>: " . $v->phone . '<br>';
					$res .= "<strong>" . $tr->translate("thTenantMobile") . "</strong>: " . $v->cellphone . '<br>';
					$res .= "<strong>" . $tr->translate("thTenantEmail") . "</strong>: " . $v->email;
					$res .= ($v->email) ? ' <a class="icon-envelope" href="mailto:' . $v->email . '"></a><br>' : "<br>";
					$res .= "<strong>" . $tr->translate("thTenantRefer") . "</strong>: " . $v->reference . '<br>';
					$res .= "<strong>" . $tr->translate("thTenantComment") . "</strong>: " . $v->comment . '<br>';
				}
				$res = $res . '</tbody></table>';
				break;
			case 'getReservation':
				Logger::info('    AJAXXXXX POST: ' . print_r($_POST, true));
				$reservid = $_POST['reservid'];
				$arrReserv = $reservationMgr->getReservation($reservid);
				$res = "";
				$res .= "<strong>" . $tr->translate("thTenantName") . "</strong>: " . $arrReserv[0]->tenancy_name . '<br>';
				$res .= "<strong>" . $tr->translate("thTenantZipCode") . "</strong>: " . $arrReserv[0]->arrival_date . '<br>';
				$res .= "<strong>" . $tr->translate("thTenantZipCode") . "</strong>: " . $arrReserv[0]->departure_date . '<br>';
				$res .= "<strong>" . $tr->translate("thTenantLastName") . "</strong>: " . $arrReserv[0]->price . '<br>';
				$res .= "<strong>" . $tr->translate("thTenantName") . "</strong>: " . $arrReserv[0]->tenant_lastname . '<br>';
				$res .= "<strong>" . $tr->translate("thTenantEmail") . "</strong>: " . $arrReserv[0]->tenant_email;
				$res .= ($arrReserv[0]->tenant_email) ? ' <a class="icon-envelope" href="mailto:' . $arrReserv[0]->tenant_email . '"></a><br>' : "<br>";
				$res .= "<strong>" . $tr->translate("thTenantRefer") . "</strong>: " . $arrReserv[0]->reference . '<br>';
				$res .= "<strong>" . $tr->translate("thTenantComment") . "</strong>: " . $arrReserv[0]->comment . '<br>';
				$res = $res . '</tbody></table>';
				break;
			case 'xxxxx':
				// $res = json_encode($val);
				break;
			// ...etc...
		}
	}
	Logger::info('AJAXXXXX: end');
	echo $res;
?>