<?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).
 *
 *******************************************************************************
*/

if (!isset($_GET['show'])){
	$no_visible_elements = true;
}
	require "../_container/container_top.php";
	require_once ('lib/Cal/activecalendar.php');

	require_once ('business/TenantMgr.php');
	require_once ('business/TenancyMgr.php');
	require_once ('business/ReservationMgr.php');

	$tenantMgr = new TenantMgr();
	$tenancyMgr = new TenancyMgr();
	$reservationMgr = new ReservationMgr();

	// print_r($_POST);

	$show = (isset($_GET['show'])) ? "show" : "";

	$arrTenancy = $tenancyMgr->getTenancys();
	$selectTenancy = getPost('selectTenancy');
	if (empty($selectTenancy)){
		if (isset($_GET['id'])){
			$selectTenancy = $_GET['id'];
		}else{
			if (!empty($arrTenancy)){
				$selectTenancy = $arrTenancy[0]->tenancy_id;
			}else{
				$selectTenancy = 0;
			}
		}
	}

	if (isset($_GET['year'])){
		$year = $_GET['year'];
	}else{
		$year = getPost('year');
	}

	if (empty($year)){
		$year = date('Y');
	}

	$startMonth = ($config['cal_start_current_month']) ? date("n") : 1;
	$monthNum = 12;
	$col = 3;

	$arrReservationsPerYear = $reservationMgr->getReservationsPerYear($year);

	$arrAllReservations = $reservationMgr->getReservations();

// Get all years where there is a reservation
	$start_years = ( array_map(
		function($item){
			return date("Y",strtotime($item->arrival_date));
		},
		$arrAllReservations));
	$end_years = ( array_map(
		function($item){
			return date("Y",strtotime($item->departure_date));
		},
		$arrAllReservations));

	$arrYears = array_unique(array_merge($start_years,$end_years));
	asort($arrYears);

	// print_r($arrAllReservations);
	// print_r($arrReservationsPerYear);
	$state_count = array_count_values( array_map(
		function($item){
			// print_r($item);
			return $item->tenancy_name;
			},
			$arrAllReservations));
	
	// print_r($state_count);

?>
	<div>
		<ul class="breadcrumb">
			<li><a href="./"><?php echo $tr->translate("menuHome"); ?></a> <span class="divider">/</span></li>
			<li class="active"><?php echo $tr->translate("menuCalendar"); ?></li>
		</ul>
	</div>
	<div class="sortable row-fluid">
		<a data-rel="tooltip" title="<?php echo $tr->translate('AddReserv'); ?>" class="well span2 top-block btn-action" href="#" name="newReservation" id="newReservation" _page="reservationDetail.php" _action="Add">
			<i class="icon-red icon-user"></i>
			<div><?php echo $tr->translate("AddReserv"); ?></div>
			<div></div>
		</a>
		<a data-rel="tooltip" title="<?php echo $tr->translate("AddPayment"); ?>" class="well span2 top-block btn-action" href="#" name="newPayment" id="newPayment" _page="paymentDetail.php" _action="Add">
			<i class="icon-cog"></i>
			<div><?php echo $tr->translate("AddPayment"); ?></div>
			<div></div>
		</a>
	</div>

	<div class="row-fluid sortable">
		<div class="box span12">
			<div class="box-header well" data-original-title>
				<h2><i class="icon-user"></i> <?php echo $tr->translate("Filter"); ?></h2>
			</div>
			<div class="box-content">
				<form class="form-horizontal" method="post" name="calendarForm" id="calendarForm" action="">
					<input type="hidden" name="action" id="action" value="" />
					<fieldset>
						<div class="control-group">
							<label class="control-label" for="selectTenancy"><?php echo $tr->translate("Tenancy"); ?></label>
							<div class="controls input-medium">
								<select id="selectTenancy" name="selectTenancy" data-rel="chosen" class="filter-action">
									<?php foreach ($arrTenancy as $tenancy) {
										echo "<option value='" . $tenancy->tenancy_id . "'";
										if ($selectTenancy == $tenancy->tenancy_id) {echo " selected";}
										echo ">" . $tenancy->name . "</option>";
									} ?>
								</select>
							</div>
						</div>
					</fieldset>
				</form>
			</div>
		</div>
	</div>
	<div class="row-fluid sortable">
		<div class="box span12">
			<div class="box-header well" data-original-title>
				<h2><i class="icon-calendar"></i> <?php echo $tr->translate("thCalendar"); ?></h2>
			</div>
			<div class="box-content">
<?php
$cal=new activeCalendar($year);
$cal->yearID = "year";
$cal->monthID = "startMonth";
// $cal->enableYearNav(false);
// $cal->enableMonthNav(false);

if ($config['cal_8days_week']) $cal->enable8DaysWeek();
if ($config['cal_show_week_number']) $cal->enableWeekNum();
$cal->showNoMonthDays = $config['cal_show_nomonth_days'];
$showReservOption = $config['cal_show_reserv_option'];
$cal->setFirstWeekDay($config['cal_first_weekday']); // 0=sunday, 1=Monday, 6=saturday

$cal->setMonthNames($tr->translate('calMonthNames'));
$cal->setDayNames($tr->translate('calDayNames'));

$cal->cssYearTitle = "year-title";
$cal->cssNoMonthDay = "nomonth-day";
$cal->cssMonthTitle = "month-title";
$cal->cssMonthTable = "month-table";
$cal->cssMonthDay = "month-day";
$cal->cssSaturday = "weekend-day";
$cal->cssSunday = "weekend-day";
$cal->cssWeekNum = "weeknum-day";
$cal->cssWeekDay = "week-day";
$cal->cssWeekDays = "week-days";

	// construction des tableaux des jours réservés et des saison pour l'année (+début et fin de réservation)

	$tab_tres_haute_1 = array();
	$tab_haute_1 = array();
	$tab_moyenne_1 = array();
	$tab_indispo_1 = array();

	$tab_res = array();
	$tab_res_deb = array();
	$tab_res_fin = array();
	$tab_option = array();
	$tab_option_deb = array();
	$tab_option_fin = array();
	foreach ($arrAllReservations as $reservation) {
		if ($reservation->tenancy_id == $selectTenancy) {
			$date_req = date('Y-m-d', strtotime($reservation->arrival_date));
			$datefin_req = date('Y-m-d', strtotime($reservation->departure_date));
			if ($reservation->status >= ReservationMgr::RESERV_VALIDATE){
				$tab_res_deb[] = date('Y-m-d', strtotime($date_req)); // on place chaque jours réservés dans le tableau
				$tab_res_fin[] = date('Y-m-d', strtotime($datefin_req)); // on place chaque jours réservés dans le tableau
				$date_req = date('Y-m-d', strtotime($date_req . ' + 1 day'));
				while ($date_req < $datefin_req){
					$tab_res[] = date('Y-m-d', strtotime($date_req));
					$date_req = date('Y-m-d', strtotime($date_req . ' + 1 day'));
				}
			}else{
				$tab_option_deb[] = date('Y-m-d', strtotime($date_req)); // on place chaque jours réservés dans le tableau
				$tab_option_fin[] = date('Y-m-d', strtotime($datefin_req)); // on place chaque jours réservés dans le tableau
				$date_req = date('Y-m-d', strtotime($date_req . ' + 1 day'));
				while ($date_req < $datefin_req) {
					$tab_option[] = date('Y-m-d', strtotime($date_req));
					$date_req = date('Y-m-d', strtotime($date_req . ' + 1 day'));
				}
			}
		}
	}
	// print_r($tab_res);
	// print_r($tab_option);

foreach ($arrAllReservations as $reservation) {
	if ($reservation->tenancy_id == $selectTenancy) {
	$date_req = $reservation->arrival_date;
	$datefin_req = $reservation->departure_date;
	if ($reservation->status >= ReservationMgr::RESERV_VALIDATE){

		$d = date('Y-m-d', strtotime($date_req));
		// debut de reserv_moyenne
		if (in_array ($d, $tab_res_deb) && in_array ($d, $tab_res_fin)){$class = " booked ";}
		elseif (in_array ($d, $tab_option_fin)){$class = " option-booked ";}
		elseif (in_array ($d, $tab_moyenne_1)){$class = " moyenne-booked ";}
		// debut de reserv_haute
		elseif (in_array ($d, $tab_haute_1)){$class = " haute-booked ";}
		// debut de reserv_tres_haute
		elseif (in_array ($d, $tab_tres_haute_1)){$class = " tres-haute-booked ";}
		// debut de reserv_indispo
		elseif (in_array ($d, $tab_indispo_1)){$class = " indispo-booked ";}
		else{$class = " free-booked ";}

		$cal->setEvent(
			date("Y", strtotime($date_req)),
			date("m", strtotime($date_req)),
			date("d", strtotime($date_req)),
			$class,
			"#",
			$reservation->tenant_lastname . " " . $reservation->tenant_name . " // " . date($config['date_format'], strtotime($reservation->arrival_date)) ."->" . date($config['date_format'], strtotime($reservation->departure_date))
		);

		$d = date('Y-m-d', strtotime($datefin_req));
		// debut de reserv_moyenne
		if (in_array ($d, $tab_res_deb) && in_array ($d, $tab_res_fin)){$class = " booked ";}
		elseif (in_array ($d, $tab_option_deb)){$class = " booked-option ";}
		elseif (in_array ($d, $tab_moyenne_1)){$class = " booked-moyenne ";}
		// debut de reserv_haute
		elseif (in_array ($d, $tab_haute_1)){$class = " booked-haute ";}
		// debut de reserv_tres_haute
		elseif (in_array ($d, $tab_tres_haute_1)){$class = " booked-tres-haute ";}
		// debut de reserv_indispo
		elseif (in_array ($d, $tab_indispo_1)){$class = " booked-indispo ";}
		else{$class = " booked-free ";}

		$cal->setEvent(
			date("Y", strtotime($datefin_req)),
			date("m", strtotime($datefin_req)),
			date("d", strtotime($datefin_req)),
			$class,
			"#",
			$reservation->tenant_lastname . " " . $reservation->tenant_name . " // " . date($config['date_format'], strtotime($reservation->arrival_date)) ."->" . date($config['date_format'], strtotime($reservation->departure_date))
		);

	}else{
if ($showReservOption){
		$d = date('Y-m-d', strtotime($date_req));
	// if (!in_array($d, $tab_res)){
		// debut de reserv_moyenne
		// if (in_array ($d, $tab_res)){$class = " booked ";}
		if (in_array ($d, $tab_option_deb) && in_array ($d, $tab_option_fin)){$class = " option ";}
		// elseif (in_array ($d, $tab_option)){$class = " option ";}
		elseif (in_array ($d, $tab_res_fin)){$class = " booked-option ";}
		elseif (in_array ($d, $tab_moyenne_1)){$class = " moyenne-option ";}
		// debut de reserv_haute
		elseif (in_array ($d, $tab_haute_1)){$class = " haute-option ";}
		// debut de reserv_tres_haute
		elseif (in_array ($d, $tab_tres_haute_1)){$class = " tres-haute-option ";}
		// debut de reserv_indispo
		elseif (in_array ($d, $tab_indispo_1)){$class = " indispo-option ";}
		else{$class = " free-option ";}

		$cal->setEvent(
			date("Y", strtotime($date_req)),
			date("m", strtotime($date_req)),
			date("d", strtotime($date_req)),
			$class,
			"#",
			$reservation->tenant_lastname . " " . $reservation->tenant_name . " // " . date($config['date_format'], strtotime($reservation->arrival_date)) ."->" . date($config['date_format'], strtotime($reservation->departure_date))
		);

		$d = date('Y-m-d', strtotime($datefin_req));
		// debut de reserv_moyenne
		// if (in_array ($d, $tab_res)){$class = " booked ";}
		if (in_array ($d, $tab_option_deb) && in_array ($d, $tab_option_fin)){$class = " option ";}
		// elseif (in_array ($d, $tab_option)){$class = " option ";}
		elseif (in_array ($d, $tab_res_deb)){$class = " option-booked ";}
		elseif (in_array ($d, $tab_moyenne_1)){$class = " option-moyenne ";}
		// debut de reserv_haute
		elseif (in_array ($d, $tab_haute_1)){$class = " option-haute ";}
		// debut de reserv_tres_haute
		elseif (in_array ($d, $tab_tres_haute_1)){$class = " option-tres-haute ";}
		// debut de reserv_indispo
		elseif (in_array ($d, $tab_indispo_1)){$class = " option-indispo ";}
		else{$class = " option-free ";}

		$cal->setEvent(
			date("Y", strtotime($datefin_req)),
			date("m", strtotime($datefin_req)),
			date("d", strtotime($datefin_req)),
			$class,
			"#",
			$reservation->tenant_lastname . " " . $reservation->tenant_name . " // " . date($config['date_format'], strtotime($reservation->arrival_date)) ."->" . date($config['date_format'], strtotime($reservation->departure_date))
		);

	// }
	}
} // end $showReservOption
	$date_req = date('Y-m-d', strtotime($date_req . ' + 1 day'));
	while ($date_req <= date('Y-m-d', strtotime($datefin_req . ' - 1 day'))) {
		if ($reservation->status >= ReservationMgr::RESERV_VALIDATE){
			$cal->setEvent(
				date("Y", strtotime($date_req)),
				date("m", strtotime($date_req)),
				date("d", strtotime($date_req)),
				" booked ",
				"#",
				$reservation->tenant_lastname . " " . $reservation->tenant_name . " // " . date($config['date_format'], strtotime($reservation->arrival_date)) ."->" . date($config['date_format'], strtotime($reservation->departure_date))
			);

		}else{
		if ($showReservOption) {
			if (!in_array($date_req, $tab_res)){
				if (in_array ($date_req, $tab_res_fin)){$class = " booked-option ";}
				elseif (in_array ($date_req, $tab_res_deb)){$class = " option-booked ";}
				else{$class = " option ";}

				$cal->setEvent(
					date("Y", strtotime($date_req)),
					date("m", strtotime($date_req)),
					date("d", strtotime($date_req)),
					$class,
					"#",
					$reservation->tenant_lastname . " " . $reservation->tenant_name . " // " . date($config['date_format'], strtotime($reservation->arrival_date)) ."->" . date($config['date_format'], strtotime($reservation->departure_date))
				);
			}
		} // end $showReservOption
		}
		$date_req = date('Y-m-d', strtotime($date_req . ' + 1 day'));
	}
}
}

?>
				<div class="well span12">
<?php
	echo $cal->showYear($col,$startMonth,$monthNum);
?>
				</div>

				<div class="row-fluid well span12 center">
					<div class="row-fluid span8 center">
						<div class="span6">
							<h4><?php echo $tr->translate('thCalformAvailability'); ?></h4>
							<div class="indispo"><?php echo $tr->translate('NotAvailable'); ?></div>
							<div class="free"><?php echo $tr->translate('Available'); ?></div>
							<div class="option"><?php echo $tr->translate('Option'); ?></div>
							<div class="booked"><?php echo $tr->translate('Booked'); ?></div>

						</div>

						<div class="span6">
							<h4><?php echo $tr->translate('thCalformNavigation'); ?></h4>
							<?php $today = date("Y");
							if ($today != $year) { ?>
								<a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $show; ?>&year=<?php echo $year-1; ?>&id=<?php echo $selectTenancy; ?>"><img src="<?php echo CALENDAR_IMAGE_PATH_RESS; ?>/arrow-gauche.png" width="40" height="40" border="0" align="absmiddle"></a> 
							<?php } ?>
							<select class="input-small" id="year" name="year">
								<?php $x = 5; for($i=0; $i<=$x; $i++){ $d = $today + $i;
								?>
								<option value="<?php echo $d; ?>" <?php if ($d == $year) echo " selected"; ?>><?php echo $d; ?></option>
								<?php } ?>
							</select>
							<?php if ($year != $today + $x) { ?>
								<a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $show; ?>&year=<?php echo $year+1; ?>&id=<?php echo $selectTenancy; ?>"><img src="<?php echo CALENDAR_IMAGE_PATH_RESS; ?>/arrow-droite.png" width="40" height="40" border="0" align="absmiddle"></a> 
							<?php } ?>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div><!--/row-->

<?php require "../_container/container_bottom.php"; ?> 

<script>
	$("td.weekend-day, td.month-day").click(function(){
		$().redirect(
			"reservationDetail.php",
			{
				'action': "Add",
			},
			'POST'
		);
	});
</script>
