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

// TODO: Prevent server side resubmitting data: http://bjw.co.nz/developer/general/75-how-to-prevent-form-resubmission
// TODO: CSS background split in 2 colors
// TODO: http://stackoverflow.com/questions/8541081/css-set-a-background-color-which-is-50-of-the-width-of-the-window
	require "../_container/container_top.php";
	require_once ('business/TenantMgr.php');
	require_once ('business/TenancyMgr.php');
	require_once ('business/ReservationMgr.php');
	require_once ('business/PaymentMgr.php');
	require_once ('business/UserMgr.php');
	require_once ('business/MailMgr.php');
	require_once ('config/Tagger.php');

	require_once("config/System.php");
	$sys = new System();
	$res = $sys->backupDatabase();
	$sys->deleteFilesOlderThan(ROOT . "data/backup/*.sql", $config['delete_backup_older_than']);
	$sys->deleteFilesOlderThan(ROOT . "data/log/*.log", $config['delete_log_older_than']);

	$tenancyMgr = new TenancyMgr();
	$reservationMgr = new ReservationMgr();
	$paymentMgr = new PaymentMgr();
	$userMgr = new UserMgr();
	$mailMgr = new MailMgr();
	$tagger = new Tagger();
	
// print_r($_POST);

	include_once ("actionList.php");

	$arrTenancy = $tenancyMgr->getTenancys();
	$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);

	$arrUsers = $userMgr->getUsers();
	$disableUserCount = 0;
	foreach ($arrUsers as $u) {
		if (!($u->enable)) {
			$disableUserCount++;
		}
	}

	$arrNextReservations = $reservationMgr->getNextReservations("ALL");
	$arrCurrentReservations = $reservationMgr->getCurrentReservations("ALL");
	$arrTodayDepartureReservations = $reservationMgr->getTodayDepartureReservations();
	$arrTodayArrivalReservations = $reservationMgr->getTodayArrivalReservations();
	$arrPayment = $paymentMgr->getPayments();

	$arrNewReservations = array_map(
		function($item){
			return $item->status;
		},
		$arrAllReservations);

	$arrBalance = array_reduce($arrPayment, function($result, $item) {
		if (!isset($result[$item->reservation_id])) $result[$item->reservation_id] = 0;
		$result[$item->reservation_id] += $item->amount;
		return $result;
	}, array());
	// print_r($arrBalance);

	$arrNewReservations = array_unique($arrNewReservations);

	$filter_year = getPost('filter_year');
	if (empty($filter_year)) {
		$filter_year = date("Y");
	}

?>
	<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("menuDashboard"); ?></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("AddTenant"); ?>" class="well span2 top-block btn-action" href="#" name="newTenant" id="newTenant" _page="tenantDetail.php" _action="Add">
			<i class="icon-red icon-user"></i>
			<div><?php echo $tr->translate("AddTenant"); ?></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>

<?php if (empty($arrTenancy)) { ?>
	<div class="alert alert-error">
		<h1><small><?php echo $tr->translate("msgNoTenancy"); ?></small></h1>
		<p><?php echo $tr->translate("msgAddTenancy"); ?></p>
		<p class="">
			<a href="tenancyDetail.php" class="btn btn-large btn-primary"><i class="icon-chevron-left icon-white"></i> <?php echo $tr->translate("AddTenancy"); ?></a>
		</p>
		<div class="clearfix"></div>
	</div>
<?php } ?>

<form class="form-horizontal" method="post" name="trackingForm" id="trackingForm" action="index.php">
<input type="hidden" name="action" id="action" value="" />
<input type="hidden" name="status" id="status" value="" />
<input type="hidden" name="reservprice" id="reservprice" value="" />
<input type="hidden" name="lease_date" id="lease_date" value="" />
<input type="hidden" name="reservationid" id="reservationid" value="" />
<input type="hidden" name="selectTenancy" id="selectTenancy" value="" />
	<?php if (in_array(ReservationMgr::RESERV_NEW, $arrNewReservations)) { ?>
	<div class="row-fluid sortable">
		<div class="box span12">
			<div class="box-header well" data-original-title>
				<h2><i class="icon-tasks"></i> <?php echo $tr->translate("NewIncoming"); ?></h2>
			</div>
			<div class="box-content">
				<div>
				<div class="table-responsive">
				<table id="tableNewIncoming" name="tableNewIncoming" class="table table-condensed table-striped table-bordered bootstrap-datatable datatable">
				  <thead>
					  <tr>
						  <th><?php echo $tr->translate("ID"); ?></th>
						  <th><?php echo $tr->translate("RecordDate"); ?></th>
						  <th><?php echo $tr->translate("thReservTenancy"); ?></th>
						  <th><?php echo $tr->translate("thReservLastName"); ?></th>
						  <th><?php echo $tr->translate("thReservName"); ?></th>
						  <th><?php echo $tr->translate("thReservFrom"); ?></th>
						  <th><?php echo $tr->translate("thReservTo"); ?></th>
						  <th><?php echo $tr->translate("Days"); ?></th>
						  <th><?php echo $tr->translate("thReservPrice"); ?></th>
						  <th><?php echo $tr->translate("Status"); ?></th>
						  <th><?php echo $tr->translate("thReservAction"); ?></th>
					  </tr>
				  </thead>   
				  <tbody>
				  <?php
					foreach ($arrAllReservations as $reservation) {
						if ($reservation->status == ReservationMgr::RESERV_NEW) {
				  ?>
					<tr id="<?php echo $reservation->reservation_id; ?>" class="">
						<td><?php echo $reservation->reservation_id; ?></td>
						<td><?php echo $reservation->recorddate; ?></td>
						<td><?php echo $reservation->tenancy_name; ?></td>
						<td>
						<div>
							<a data-toggle="modal" data-target="#myModal" data-tenantid="<?php echo $reservation->tenant_id; ?>" class="tenant-popup icon-question-sign" href="#"></a>
						<?php echo $reservation->tenant_lastname; ?>
						</div>
						</td>
						<td><?php echo $reservation->tenant_name; ?></td>
						<td class="<?php if (strtotime($reservation->arrival_date) < strtotime('now')){echo "td-error";} ?>"><?php echo date($config['date_format'], strtotime($reservation->arrival_date)); ?></td>
						<td class="<?php if (strtotime($reservation->departure_date) < strtotime('now')){echo "td-error";} ?>"><?php echo date($config['date_format'], strtotime($reservation->departure_date)); ?></td>
						<td><?php echo round((strtotime($reservation->departure_date) - strtotime($reservation->arrival_date))/86400); ?></td>
						<td><?php echo $reservation->price; ?></td>
						<td>
							<span class="tracking" data-search="<?php echo $reservation->status; ?>">
								<a name="<?php echo ReservationMgr::RESERV_OPTION; ?>" title="<?php echo $tr->translate("Tracking1"); ?>" data-rel="tooltip" data-placement="top" class="tracking-element <?php if ($reservation->status >= ReservationMgr::RESERV_OPTION) echo "current"; ?>"><i class="badge icon-question-sign icon-large"></i></a>
								<a name="<?php echo ReservationMgr::RESERV_LEASE_SENT; ?>" title="<?php echo $tr->translate("Tracking2"); ?>" data-rel="tooltip" data-placement="top" class="_tracking-element <?php if ($reservation->status >= ReservationMgr::RESERV_LEASE_SENT) echo "current"; if ($reservation->status == ReservationMgr::RESERV_LEASE_SENT && $reservation->lease_date && ($leaseDays >= $config['lease_reminder_delay'])) echo " error"; ?>"><i class="badge icon-envelope icon-large"></i><small><?php if ($reservation->status == ReservationMgr::RESERV_LEASE_SENT && $reservation->lease_date && ($leaseDays >= $config['lease_reminder_delay'])) echo $leaseDays; ?></small></a>
								<a name="<?php echo ReservationMgr::RESERV_VALIDATE; ?>" title="<?php echo $tr->translate("Tracking3"); ?>" data-rel="tooltip" data-placement="top" class="_tracking-element <?php if ($reservation->status >= ReservationMgr::RESERV_VALIDATE) echo "current"; ?>"><i class="badge icon-ok icon-large"></i></a>
								<a name="<?php echo ReservationMgr::RESERV_CLOSED; ?>" title="<?php echo $tr->translate("Tracking4"); ?>" data-rel="tooltip" data-placement="top" class="_tracking-element <?php if ($reservation->status >= ReservationMgr::RESERV_CLOSED) echo "current"; ?>"><i class="badge icon-usd icon-large"></i></a>
							</span>
						</td>
							<td>
								<a class="btn btn-info btn-mini btn-action" title="<?php echo $tr->translate("tipEditReserv"); ?>" data-rel="tooltip" href="#" _id="<?php echo $reservation->reservation_id; ?>" _page="reservationDetail.php" _action="<?php echo $tr->translate("Edit"); ?>">
									<i class="icon-edit icon-white"></i>
								</a>
								<a class="btn btn-danger btn-mini" title="<?php echo $tr->translate("tipDeleteReserv"); ?>" data-rel="tooltip" href="#" onclick="goAction('DELETE_RESERV', <?php echo $reservation->reservation_id; ?>);">
									<i class="icon-trash icon-white"></i>
								</a>
								<a target="calendar" target="calendar" class="btn btn-success btn-mini" title="<?php echo $tr->translate("tipShowCal"); ?>" data-rel="tooltip" href="calendar.php?id=<?php echo $reservation->tenancy_id; ?>&y=<?php echo $filter_year; ?>">
									<i class="icon-calendar icon-white"></i>
								</a>
							</td>
					</tr>
					<?php } } ?>
				  </tbody>
			  </table>
			  </div>
			  </div>
			</div>
		</div><!--/span-->
	</div><!--/row-->
<?php } ?>
	<div class="row-fluid sortable">
		<div class="box span12">
			<div class="box-header well" data-original-title>
				<h2><i class="icon-tasks"></i> <?php echo $tr->translate("FollowUp"); ?></h2>
			</div>
			<div class="box-content">
			<div class="table-responsive">
				<table id="tableFollowUp" name="tableFollowUp" class="table table-condensed table-striped table-bordered bootstrap-datatable datatable">
				  <thead>
					  <tr>
						  <th><?php echo $tr->translate("ID"); ?></th>
						  <th><?php echo $tr->translate("thReservTenancy"); ?></th>
						  <th><?php echo $tr->translate("thReservLastName"); ?></th>
						  <th><?php echo $tr->translate("thReservName"); ?></th>
						  <th><?php echo $tr->translate("thReservFrom"); ?></th>
						  <th><?php echo $tr->translate("thReservTo"); ?></th>
						  <th><?php echo $tr->translate("Days"); ?></th>
						  <th><?php echo $tr->translate("thReservPrice"); ?></th>
						  <th><?php echo $tr->translate("thReservBalance"); ?></th>
						  <th><?php echo $tr->translate("LeaseDate"); ?></th>
						  <th><?php echo $tr->translate("Status"); ?></th>
						  <th><?php echo $tr->translate("thReservAction"); ?></th>
					  </tr>
				  </thead>   
				  <tbody>
				  <?php
					foreach ($arrAllReservations as $reservation) {
						if ($reservation->status > ReservationMgr::RESERV_NEW && $reservation->status < ReservationMgr::RESERV_VALIDATE) {
							$leaseDays =  round((time() - strtotime($reservation->lease_date))/86400);
							$arrPayment = $paymentMgr->getPaymentByReservation($reservation->reservation_id);
							$arr = array();
							$arr[] = $reservation;

				  ?>
					<tr id="<?php echo $reservation->reservation_id; ?>" data-deposit="<?php echo $reservation->deposit; ?>">
						<td><?php echo $reservation->reservation_id; ?></td>
						<td><?php echo $reservation->tenancy_name; ?></td>
						<td>
						<div>
							<a data-toggle="modal" data-target="#myModal" data-tenantid="<?php echo $reservation->tenant_id; ?>" class="tenant-popup icon-question-sign" href="#"></a>
						<?php echo $reservation->tenant_lastname; ?>
						</div>
						</td>
						<td><?php echo $reservation->tenant_name; ?></td>
						<td class="<?php if (strtotime($reservation->arrival_date) < strtotime('now')){echo "td-error";} ?>"><?php echo date($config['date_format'], strtotime($reservation->arrival_date)); ?></td>
						<td class="<?php if (strtotime($reservation->departure_date) < strtotime('now')){echo "td-error";} ?>"><?php echo date($config['date_format'], strtotime($reservation->departure_date)); ?></td>
						<td><?php echo round((strtotime($reservation->departure_date) - strtotime($reservation->arrival_date))/86400); ?></td>
						<td class="<?php $v = intval($reservation->price); if (empty($v)){echo "td-error";} ?>"><?php echo $reservation->price; ?></td>
						<td class="<?php 
							$payment = (isset($arrBalance[$reservation->reservation_id])) ? $arrBalance[$reservation->reservation_id] : 0;
							// $balance = number_format((float)$reservation->price - $payment + $extra, 2, '.', '');
							$balance = number_format((float)getBalance($arr, array(), $arrPayment, $config), 2, '.', '');

							// if (($payment == 0) || ($balance < 0)) {echo "td-error";} ?>">
							<?php echo $balance; ?>
						</td>
						<td>
							<div class="row-fuild">
								<span><?php
										if (isset($reservation->lease_date)) {
											echo date($config['date_format'], strtotime($reservation->lease_date));
										if ($reservation->status < ReservationMgr::RESERV_VALIDATE) {
										?>
										<a class="btn btn-danger btn-mini" title="<?php echo $tr->translate("tipClearLeaseDate"); ?>" data-rel="tooltip" href="#" onclick="goAction('CLEAR_LEASE_DATE', <?php echo $reservation->reservation_id; ?>);">
											<i class="icon-trash"></i>
										</a>

										<?php } ?>
										<?php }else{ ?>

									<div class="input-append"><input type="text" class="datepicker input-small" name="leaseDate<?php echo $reservation->reservation_id; ?>" id="leaseDate<?php echo $reservation->reservation_id; ?>" value="<?php
										echo date($config['date_format']);
										?>"><span class="add-on"><i class="icon-calendar"></i></span>

									</div>
										<a class="btn btn-primary btn-mini" title="<?php echo $tr->translate("tipSetLeaseDate"); ?>" data-rel="tooltip" href="#" onclick="goAction('SET_LEASE_DATE', <?php echo $reservation->reservation_id; ?>);">
											<i class="icon-ok"></i>
										</a>

										<?php } ?>
								</span>
							</div>
						</td>
						<td>
							<span class="tracking" data-search="<?php echo $reservation->status; ?>">
								<a name="<?php echo ReservationMgr::RESERV_OPTION; ?>" title="<?php echo $tr->translate("Tracking1"); ?>" data-rel="tooltip" data-placement="top" class="tracking-element <?php if ($reservation->status >= ReservationMgr::RESERV_OPTION) echo "current"; ?>"><i class="badge icon-question-sign icon-large"></i></a>
								<a name="<?php echo ReservationMgr::RESERV_LEASE_SENT; ?>" title="<?php echo $tr->translate("Tracking2"); ?>" data-rel="tooltip" data-placement="top" class="tracking-element <?php if ($reservation->status >= ReservationMgr::RESERV_LEASE_SENT) echo "current"; if ($reservation->status == ReservationMgr::RESERV_LEASE_SENT && $reservation->lease_date && ($leaseDays >= $config['lease_reminder_delay'])) echo " error"; ?>"><i class="badge icon-envelope icon-large"></i><small><?php if ($reservation->status == ReservationMgr::RESERV_LEASE_SENT && $reservation->lease_date && ($leaseDays >= $config['lease_reminder_delay'])) echo $leaseDays; ?></small></a>
								<a name="<?php echo ReservationMgr::RESERV_VALIDATE; ?>" title="<?php echo $tr->translate("Tracking3"); ?>" data-rel="tooltip" data-placement="top" class="tracking-element <?php if ($reservation->status >= ReservationMgr::RESERV_VALIDATE) echo "current"; ?>"><i class="badge icon-ok icon-large"></i></a>
								<a name="<?php echo ReservationMgr::RESERV_CLOSED; ?>" title="<?php echo $tr->translate("Tracking4"); ?>" data-rel="tooltip" data-placement="top" class="tracking-element <?php if ($reservation->status >= ReservationMgr::RESERV_CLOSED) echo "current"; ?>"><i class="badge icon-usd icon-large"></i></a>
							</span>
						</td>
						<td>
							<a class="btn btn-info btn-mini btn-action" title="<?php echo $tr->translate("tipEditReserv"); ?>" data-rel="tooltip" href="#" _id="<?php echo $reservation->reservation_id; ?>" _page="reservationDetail.php" _action="<?php echo $tr->translate("Edit"); ?>">
								<i class="icon-edit icon-white"></i>
							</a>
							<a class="btn btn-danger btn-mini" title="<?php echo $tr->translate("tipDeleteReserv"); ?>" data-rel="tooltip" href="#" onclick="goAction('DELETE_RESERV', <?php echo $reservation->reservation_id; ?>);">
								<i class="icon-trash icon-white"></i>
							</a>
							<a target="calendar" target="calendar" class="btn btn-success btn-mini" title="<?php echo $tr->translate("tipShowCal"); ?>" data-rel="tooltip" href="calendar.php?id=<?php echo $reservation->tenancy_id; ?>&y=<?php echo $filter_year; ?>">
								<i class="icon-calendar icon-white"></i>
							</a>
						</td>
					</tr>
					<?php } } ?>
				  </tbody>
			  </table>
			  </div>
			</div>
		</div><!--/span-->
	</div><!--/row-->
	<?php if (in_array(ReservationMgr::RESERV_VALIDATE, $arrNewReservations)) { ?>
	<div class="row-fluid sortable">
		<div class="box span12">
			<div class="box-header well" data-original-title>
				<h2><i class="icon-tasks"></i> <?php echo $tr->translate("Validated"); ?></h2>
			</div>
			<div class="box-content">
				<div>
				<div class="table-responsive">
				<table id="tableValidated" name="tableValidated" class="table table-condensed table-striped table-bordered bootstrap-datatable datatable">
				  <thead>
					  <tr>
						  <th><?php echo $tr->translate("ID"); ?></th>
						  <th><?php echo $tr->translate("thReservTenancy"); ?></th>
						  <th><?php echo $tr->translate("thReservLastName"); ?></th>
						  <th><?php echo $tr->translate("thReservName"); ?></th>
						  <th><?php echo $tr->translate("thReservFrom"); ?></th>
						  <th><?php echo $tr->translate("thReservTo"); ?></th>
						  <th><?php echo $tr->translate("Days"); ?></th>
						  <th><?php echo $tr->translate("thReservPrice"); ?></th>
						  <th><?php echo $tr->translate("thReservBalance"); ?></th>
						  <th><?php echo $tr->translate("LeaseDate"); ?></th>
						  <th><?php echo $tr->translate("Status"); ?></th>
						  <th><?php echo $tr->translate("thReservAction"); ?></th>
					  </tr>
				  </thead>   
				  <tbody>
				  <?php
					foreach ($arrAllReservations as $reservation) {
						if ($reservation->status == ReservationMgr::RESERV_VALIDATE) {
							$arrPayment = $paymentMgr->getPaymentByReservation($reservation->reservation_id);
							$arr = array();
							$arr[] = $reservation;

							$balanceDays = round(abs(time() - strtotime($reservation->arrival_date))/86400);
				  ?>
					<tr id="<?php echo $reservation->reservation_id; ?>" class="">
						<td><?php echo $reservation->reservation_id; ?></td>
						<td><?php echo $reservation->tenancy_name; ?></td>
						<td>
						<div>
							<a data-toggle="modal" data-target="#myModal" data-tenantid="<?php echo $reservation->tenant_id; ?>" class="tenant-popup icon-question-sign" href="#"></a>
						<?php echo $reservation->tenant_lastname; ?>
						</div>
						</td>
						<td><?php echo $reservation->tenant_name; ?></td>
						<td class="<?php if (strtotime($reservation->arrival_date) < strtotime('now')){echo "td-error";} ?>"><?php echo date($config['date_format'], strtotime($reservation->arrival_date)); ?></td>
						<td class="<?php if (strtotime($reservation->departure_date) < strtotime('now')){echo "td-error";} ?>"><?php echo date($config['date_format'], strtotime($reservation->departure_date)); ?></td>
						<td><?php echo round((strtotime($reservation->departure_date) - strtotime($reservation->arrival_date))/86400); ?></td>
						<td class="<?php $v = intval($reservation->price); if (empty($v)){echo "td-error";} ?>"><?php echo $reservation->price; ?></td>
						<td class="<?php 
							$payment = (isset($arrBalance[$reservation->reservation_id])) ? $arrBalance[$reservation->reservation_id] : 0;
							// $balance = number_format((float)$reservation->price - $payment + $extra, 2, '.', '');
							$balance = number_format((float)getBalance($arr, array(), $arrPayment, $config), 2, '.', '');
							// if (($payment == 0) || ($balance < 0)) {echo "td-error";} ?>">
							<?php echo $balance; ?>
						</td>
						<td>
							<div class="row-fuild">
								<span><?php
										if (isset($reservation->lease_date)) {
											echo date($config['date_format'], strtotime($reservation->lease_date));
										if ($reservation->status < ReservationMgr::RESERV_VALIDATE) {
										?>
										<a class="btn btn-danger btn-mini" title="<?php echo $tr->translate("tipClearLeaseDate"); ?>" data-rel="tooltip" href="#" onclick="goAction('CLEAR_LEASE_DATE', <?php echo $reservation->reservation_id; ?>);">
											<i class="icon-trash"></i>
										</a>

										<?php } ?>
										<?php }else{ ?>

									<div class="input-append"><input type="text" class="datepicker input-small" name="leaseDate<?php echo $reservation->reservation_id; ?>" id="leaseDate<?php echo $reservation->reservation_id; ?>" value="<?php
										echo date($config['date_format']);
										?>"><span class="add-on"><i class="icon-calendar"></i></span>

									</div>
										<a class="btn btn-primary btn-mini" title="<?php echo $tr->translate("tipSetLeaseDate"); ?>" data-rel="tooltip" href="#" onclick="goAction('SET_LEASE_DATE', <?php echo $reservation->reservation_id; ?>);">
											<i class="icon-ok"></i>
										</a>

										<?php } ?>
								</span>
							</div>
						</td>
						<td>
							<span class="tracking" data-search="<?php echo $reservation->status; ?>">
								<a name="<?php echo ReservationMgr::RESERV_OPTION; ?>" title="<?php echo $tr->translate("Tracking1"); ?>" data-rel="tooltip" data-placement="top" class="tracking-element <?php if ($reservation->status >= ReservationMgr::RESERV_OPTION) echo "current"; ?>"><i class="badge icon-question-sign icon-large"></i></a>
								<a name="<?php echo ReservationMgr::RESERV_LEASE_SENT; ?>" title="<?php echo $tr->translate("Tracking2"); ?>" data-rel="tooltip" data-placement="top" class="tracking-element <?php if ($reservation->status >= ReservationMgr::RESERV_LEASE_SENT) echo "current"; if ($reservation->status == ReservationMgr::RESERV_LEASE_SENT && $reservation->lease_date && ($leaseDays >= $config['lease_reminder_delay'])) echo " error"; ?>"><i class="badge icon-envelope icon-large"></i><small><?php if ($reservation->status == ReservationMgr::RESERV_LEASE_SENT && $reservation->lease_date && ($leaseDays >= $config['lease_reminder_delay'])) echo $leaseDays; ?></small></a>
								<a name="<?php echo ReservationMgr::RESERV_VALIDATE; ?>" title="<?php echo $tr->translate("Tracking3"); ?>" data-rel="tooltip" data-placement="top" class="tracking-element <?php if ($reservation->status >= ReservationMgr::RESERV_VALIDATE) echo "current"; ?>"><i class="badge icon-ok icon-large"></i></a>
								<a name="<?php echo ReservationMgr::RESERV_CLOSED; ?>" title="<?php echo $tr->translate("Tracking4"); ?>" data-rel="tooltip" data-placement="top" class="tracking-element <?php if ($reservation->status >= ReservationMgr::RESERV_CLOSED) echo "current"; if ($balanceDays <= $config['balance_reminder_delay']) echo " error"; ?>"><i class="badge icon-usd icon-large"></i><small><?php if ($balanceDays <= $config['balance_reminder_delay']) echo $balanceDays; ?></small></a>
							</span>
						</td>
						<td>
							<a class="btn btn-info btn-mini btn-action" title="<?php echo $tr->translate("tipEditReserv"); ?>" data-rel="tooltip" href="#" _id="<?php echo $reservation->reservation_id; ?>" _page="reservationDetail.php" _action="<?php echo $tr->translate("Edit"); ?>">
								<i class="icon-edit icon-white"></i>
							</a>
							<a class="btn btn-danger btn-mini" title="<?php echo $tr->translate("tipDeleteReserv"); ?>" data-rel="tooltip" href="#" onclick="goAction('DELETE_RESERV', <?php echo $reservation->reservation_id; ?>);">
								<i class="icon-trash icon-white"></i>
							</a>
							<a target="calendar" class="btn btn-success btn-mini" title="<?php echo $tr->translate("tipShowCal"); ?>" data-rel="tooltip" href="calendar.php?id=<?php echo $reservation->tenancy_id; ?>&y=<?php echo $filter_year; ?>">
								<i class="icon-calendar icon-white"></i>
							</a>
							<?php if ($reservation->status == ReservationMgr::RESERV_VALIDATE) { ?>
							<a target="calendar" class="btn btn-inverse btn-mini" title="<?php echo $tr->translate("tipRequestBalance"); ?>" data-rel="tooltip" href="#" onclick="goAction('REQUEST_BALANCE', <?php echo $reservation->reservation_id; ?>);">
								<i class="icon-dollar icon-white"></i>
							</a>
							<?php } ?>
						</td>
					</tr>
					<?php } } ?>
				  </tbody>
			  </table>
			  </div>
			  </div>
			</div>
		</div><!--/span-->
	</div><!--/row-->
<?php } ?>

</form>

	<div class="row-fluid sortable">
		<div class="box span6">
			<div class="box-header well" data-original-title>
				<h2><i class="icon-user"></i> <?php echo $tr->translate("TodayDeparture"); ?></h2>
			</div>
			<div class="box-content">
				<div class="well" id="pageTodayDepartureContent"></div>
				<div id="pagerTodayDeparture">
				</div>
				<?php $j=0;$pageDepartureCount=0; foreach ($arrTodayDepartureReservations as $reservation) {
					if (($j % $config['max_list_page'] === 0) || ($j == 0)) {
						$pageDepartureCount++;
				?>
					<div class="hidden pageN" id="pageTodayDeparture<?php echo $pageDepartureCount;?>">
					<?php } ?>
						<ul class="dashboard-list ___btn-action<?php if (!$reservation->paid) {echo " alert-error";}?>" _id="<?php echo $reservation->reservation_id; ?>" _page="reservationDetail.php" _action="<?php echo $tr->translate("Edit"); ?>">
							<li>
								<div class="row-fluid" id="<?php echo $reservation->reservation_id; ?>">
									<div>
										<img class="dashboard-avatar" alt="<?php echo $reservation->tenancy_name; ?>" src="<?php echo TENANCY_IMAGE_PATH_RESS."/";?><?php echo $reservation->tenancy_imagepath; ?>">
									</div>
									<div class="pull-left">
										<strong><?php echo $tr->translate("Tenancy"); ?>:</strong> <?php echo $reservation->tenancy_name; ?><br>
										<strong><?php echo $tr->translate("Tenant"); ?>:</strong> <?php echo $reservation->tenant_lastname; ?>
										<?php echo $reservation->tenant_name; ?>
											<a data-toggle="modal" data-target="#myModal" data-tenantid="<?php echo $reservation->tenant_id; ?>" class="tenant-popup icon-question-sign" href="#"></a>
										<br>
										<strong><?php echo $tr->translate("From"); ?>:</strong> <?php echo date($config['date_format'], strtotime($reservation->arrival_date)); ?>
										<strong><?php echo $tr->translate("To"); ?>:</strong> <?php echo date($config['date_format'], strtotime($reservation->departure_date)); ?><br>
										<strong>ETD:</strong> <span class="label label-warning"><?php echo round((strtotime($reservation->departure_date) - strtotime(date("Y-m-d"))) / (60 * 60 * 24)); ?></span>
										<strong><?php echo $tr->translate("Days"); ?>:</strong> <span class="label label-info"><?php echo round((strtotime($reservation->departure_date) - strtotime($reservation->arrival_date)) / (60 * 60 * 24)); ?></span>
										<span style="cursor: pointer;" class="label label-success icon icon-edit" onclick="goAction('EDIT_RESERV', <?php echo $reservation->reservation_id; ?>);"></span>
									</div>
								</div>
							</li>
						</ul>
					<?php if ((($j+1) % $config['max_list_page'] === 0) || ($j == count($arrTodayDepartureReservations) - 1)) { ?>
					</div>
				<?php } $j++; } ?>
			</div>
		</div><!--/span-->
				
		<div class="box span6">
			<div class="box-header well" data-original-title>
				<h2><i class="icon-user"></i> <?php echo $tr->translate("TodayArrival"); ?></h2>
			</div>
			<div class="box-content">
				<p class="well" id="pageTodayArrivalContent"></p>
				<p id="pagerTodayArrival"></p>
				<?php $j=0;$pageArrivalCount=0; foreach ($arrTodayArrivalReservations as $reservation) {
					if (($j % $config['max_list_page'] === 0) || ($j == 0)) {
						$pageArrivalCount++;
				?>
					<div class="hidden pageN" id="pageTodayArrival<?php echo $pageArrivalCount;?>">
					<?php } ?>
						<ul class="dashboard-list ____btn-action <?php if (!$reservation->paid) {echo " alert-error";}?>" _id="<?php echo $reservation->reservation_id; ?>" _page="reservationDetail.php" _action="<?php echo $tr->translate("Edit"); ?>">
							<li>
								<div class="row-fluid" id="<?php echo $reservation->reservation_id; ?>">
									<div>
										<img class="dashboard-avatar" alt="<?php echo $reservation->tenancy_name; ?>" src="<?php echo TENANCY_IMAGE_PATH_RESS."/";?><?php echo $reservation->tenancy_imagepath; ?>">
									</div>
									<div class="pull-left">
										<strong><?php echo $tr->translate("Tenancy"); ?>:</strong> <?php echo $reservation->tenancy_name; ?><br>
										<strong><?php echo $tr->translate("Tenant"); ?>:</strong> <?php echo $reservation->tenant_lastname; ?>
										<?php echo $reservation->tenant_name; ?>
											<a data-toggle="modal" data-target="#myModal" data-tenantid="<?php echo $reservation->tenant_id; ?>" class="tenant-popup icon-question-sign" href="#"></a>
										<br>
										<strong><?php echo $tr->translate("From"); ?>:</strong> <?php echo date($config['date_format'], strtotime($reservation->arrival_date)); ?>
										<strong><?php echo $tr->translate("To"); ?>:</strong> <?php echo date($config['date_format'], strtotime($reservation->departure_date)); ?><br>
										<strong>ETA:</strong> <span class="label label-warning"><?php echo round((strtotime($reservation->arrival_date) - strtotime(date("Y-m-d"))) / (60 * 60 * 24)); ?></span>
										<strong><?php echo $tr->translate("Days"); ?>:</strong> <span class="label label-info"><?php echo round((strtotime($reservation->departure_date) - strtotime($reservation->arrival_date)) / (60 * 60 * 24)); ?></span>
										<span style="cursor: pointer;" class="label label-success icon icon-edit" onclick="goAction('EDIT_RESERV', <?php echo $reservation->reservation_id; ?>);"></span>
									</div>
								</div>
							</li>
						</ul>
					<?php if ((($j+1) % $config['max_list_page'] === 0) || ($j == count($arrTodayArrivalReservations) - 1)) { ?>
					</div>
				<?php } $j++; } ?>
			</div>
		</div><!--/span-->
				
	</div><!--/row-->

	<div class="row-fluid sortable">
		<div class="box span6">
			<div class="box-header well" data-original-title>
				<h2><i class="icon-user"></i> <?php echo $tr->translate("CurrReserv"); ?></h2>
			</div>
			<div class="box-content">
				<div class="well" id="pageCurrentContent"></div>
				<div id="pagerCurrent"></div>
				<?php $j=0;$pageCurrentCount=0; foreach ($arrCurrentReservations as $reservation) {
					if (($j % $config['max_list_page'] === 0) || ($j == 0)) {
						$pageCurrentCount++;
				?>
					<div class="hidden pageN" id="pageCurrent<?php echo $pageCurrentCount;?>">
					<?php } ?>
						<ul class="dashboard-list __btn-action<?php if (!$reservation->paid) {echo " alert-error";}?>" _id="<?php echo $reservation->reservation_id; ?>" _page="reservationDetail.php" _action="<?php echo $tr->translate("Edit"); ?>">
							<li>
								<div class="row-fluid" id="<?php echo $reservation->reservation_id; ?>">
									<div>
										<img class="dashboard-avatar" alt="<?php echo $reservation->tenancy_name; ?>" src="<?php echo TENANCY_IMAGE_PATH_RESS."/";?><?php echo $reservation->tenancy_imagepath; ?>">
									</div>
									<div class="pull-left" id="<?php echo $reservation->reservation_id; ?>">
										<strong><?php echo $tr->translate("Tenancy"); ?>:</strong> <?php echo $reservation->tenancy_name; ?><br>
										<strong><?php echo $tr->translate("Tenant"); ?>:</strong> <?php echo $reservation->tenant_lastname; ?>
										<?php echo $reservation->tenant_name; ?>
											<a data-toggle="modal" data-target="#myModal" data-tenantid="<?php echo $reservation->tenant_id; ?>" class="tenant-popup icon-question-sign" href="#"></a>
										<br>
										<strong><?php echo $tr->translate("From"); ?>:</strong> <?php echo date($config['date_format'], strtotime($reservation->arrival_date)); ?>
										<strong><?php echo $tr->translate("To"); ?>:</strong> <?php echo date($config['date_format'], strtotime($reservation->departure_date)); ?><br>
										<strong>ETD:</strong> <span class="label label-warning"><?php echo round((strtotime($reservation->departure_date) - strtotime(date("Y-m-d"))) / (60 * 60 * 24)); ?></span>
										<strong><?php echo $tr->translate("Days"); ?>:</strong> <span class="label label-info"><?php echo round((strtotime($reservation->departure_date) - strtotime($reservation->arrival_date)) / (60 * 60 * 24)); ?></span>
										<span style="cursor: pointer;" class="label label-success icon icon-edit" onclick="goAction('EDIT_RESERV', <?php echo $reservation->reservation_id; ?>);"></span>
										<span>&nbsp;</span>
									</div>
								</div>
							</li>
						</ul>
					<?php if ((($j+1) % $config['max_list_page'] === 0) || ($j == count($arrCurrentReservations) - 1)) { ?>
					</div>
				<?php } $j++; } ?>
			</div>
		</div><!--/span-->
				
		<div class="box span6">
			<div class="box-header well" data-original-title>
				<h2><i class="icon-user"></i> <?php echo $tr->translate("NextReserv"); ?></h2>
			</div>
			<div class="box-content">
				<p class="well" id="pageNextContent"></p>
				<p id="pagerNext"></p>
				<?php $j=0;$pageNextCount=0; foreach ($arrNextReservations as $reservation) {
					if (round((strtotime($reservation->arrival_date) - strtotime(date("Y-m-d"))) / (60 * 60 * 24)) <> 0) {
					if (($j % $config['max_list_page'] === 0) || ($j == 0)) {
						$pageNextCount++;
				?>
					<div class="hidden pageN" id="pageNext<?php echo $pageNextCount;?>">
					<?php } ?>
						<ul class="dashboard-list __btn-action <?php if (!$reservation->paid) {echo " alert-error";}?>" _id="<?php echo $reservation->reservation_id; ?>" _page="reservationDetail.php" _action="<?php echo $tr->translate("Edit"); ?>">
							<li>
								<div class="row-fluid" id="<?php echo $reservation->reservation_id; ?>">
									<div>
										<img class="dashboard-avatar" alt="<?php echo $reservation->tenancy_name; ?>" src="<?php echo TENANCY_IMAGE_PATH_RESS."/";?><?php echo $reservation->tenancy_imagepath; ?>">
									</div>
									<div class="pull-left">
										<strong><?php echo $tr->translate("Tenancy"); ?>:</strong> <?php echo $reservation->tenancy_name; ?><br>
										<strong><?php echo $tr->translate("Tenant"); ?>:</strong> <?php echo $reservation->tenant_lastname; ?>
										<?php echo $reservation->tenant_name; ?>
											<a data-toggle="modal" data-target="#myModal" data-tenantid="<?php echo $reservation->tenant_id; ?>" class="tenant-popup icon-question-sign" href="#"></a>
										<br>
										<strong><?php echo $tr->translate("From"); ?>:</strong> <?php echo date($config['date_format'], strtotime($reservation->arrival_date)); ?>
										<strong><?php echo $tr->translate("To"); ?>:</strong> <?php echo date($config['date_format'], strtotime($reservation->departure_date)); ?><br>
										<strong>ETA:</strong> <span class="label label-warning"><?php echo round((strtotime($reservation->arrival_date) - strtotime(date("Y-m-d"))) / (60 * 60 * 24)); ?></span>
										<strong><?php echo $tr->translate("Days"); ?>:</strong> <span class="label label-info"><?php echo round((strtotime($reservation->departure_date) - strtotime($reservation->arrival_date)) / (60 * 60 * 24)); ?></span>
										<span style="cursor: pointer;" class="label label-success icon icon-edit" onclick="goAction('EDIT_RESERV', <?php echo $reservation->reservation_id; ?>);"></span>
									</div>
								</div>
							</li>
						</ul>
					<?php if ((($j+1) % $config['max_list_page'] === 0) || ($j == count($arrNextReservations) - 1)) { ?>
					</div>
				<?php } $j++; } } ?>
			</div>
		</div><!--/span-->
	</div><!--/row-->

<?php require "../_container/container_bottom.php"; ?>
<script>
$('#pagerTodayDeparture').bootpag({
   total: <?php echo $pageDepartureCount; ?>,
   page: 1,
   next: '&rarr;',
   prev: '&larr;',
   maxVisible: 5
}).on('page', function(event, num){
    $("#pageTodayDepartureContent").html($("#pageTodayDeparture"+num).html());
})<?php if ($pageDepartureCount <= 1) echo ".hide()"; ?>;
$("#pageTodayDepartureContent").html($("#pageTodayDeparture1").html());

$('#pagerTodayArrival').bootpag({
   total: <?php echo $pageArrivalCount; ?>,
   page: 1,
   next: '&rarr;',
   prev: '&larr;',
   maxVisible: 5
}).on('page', function(event, num){
    $("#pageTodayArrivalContent").html($("#pageTodayArrival"+num).html());
})<?php if ($pageArrivalCount <= 1) echo ".hide()"; ?>;
$("#pageTodayArrivalContent").html($("#pageTodayArrival1").html());

$('#pagerCurrent').bootpag({
   total: <?php echo $pageCurrentCount; ?>,
   page: 1,
   next: '&rarr;',
   prev: '&larr;',
   maxVisible: 5
}).on('page', function(event, num){
    $("#pageCurrentContent").html($("#pageCurrent"+num).html());
})<?php if ($pageCurrentCount <= 1) echo ".hide()"; ?>;
$("#pageCurrentContent").html($("#pageCurrent1").html());

$('#pagerNext').bootpag({
   total: <?php echo $pageNextCount; ?>,
   page: 1,
   next: '&rarr;',
   prev: '&larr;',
   maxVisible: 5
}).on('page', function(event, num){
    $("#pageNextContent").html($("#pageNext"+num).html());
})<?php if ($pageNextCount <= 1) echo ".hide()"; ?>;
$("#pageNextContent").html($("#pageNext1").html());


function deleteAction() {
	// $('#content').slideUp("slow");
	blockUI();
	trackingForm.submit();
}

function goAction(action, id) {
	if (action == "SET_LEASE_DATE") {
		$("#action").val(action);
		$("#reservationid").val(id);

		var lease_date = moment($("#leaseDate"+id).val(),"<?php echo $config['date_format_js_moment']; ?>").format("YYYY-MM-DD HH:mm:ss");
		if (!(moment($("#leaseDate"+id).val(),"<?php echo $config['date_format_js_moment']; ?>").isValid())){
			alert("<?php echo $tr->translate("msgInvalidDate"); ?>");
			return false;
		}
		$("#lease_date").val(lease_date);
		// $('#content').slideUp("slow");
		blockUI();
		trackingForm.submit();
	}
	if (action == "CLEAR_LEASE_DATE") {
		$("#action").val(action);
		$("#reservationid").val(id);
		$("#dialog-confirm").dialog("open");
	}
	if (action == "DELETE_RESERV") {
		$("#action").val(action);
		$("#reservationid").val(id);
		$("#dialog-confirm").dialog("open");
	}
	if (action == "EDIT_RESERV") {
		// $('#content').slideUp("slow");
		blockUI();
		$('#trackingForm').attr('action','reservationDetail.php');
		$().redirect(
			$('#trackingForm').attr('action'),{
				'action': "Edit",
				'id': id
			},
			'POST'
		);
	}
	if (action == "REQUEST_BALANCE") {
		$("#action").val(action);
		$("#reservationid").val(id);
		trackingForm.submit();
	}
}
	$(document).ready(function() {
		if ($('#tableNewIncoming').length) {
			myTable = $('#tableNewIncoming').dataTable();
			var oSettings = myTable.fnSettings();
			oSettings.aoColumns[5].sType = "custom-date";
			oSettings.aoColumns[6].sType = "custom-date";
			oSettings.aoColumns[9].sType = "custom-numeric";
		}

		if ($('#tableFollowUp').length) {
			myTable = $('#tableFollowUp').dataTable();
			var oSettings = myTable.fnSettings();
			oSettings.aoColumns[4].sType = "custom-date";
			oSettings.aoColumns[5].sType = "custom-date";
			oSettings.aoColumns[9].sType = "custom-date";
			oSettings.aoColumns[10].sType = "custom-numeric";
		}

		if ($('#tableValidated').length) {
			myTable = $('#tableValidated').dataTable();
			var oSettings = myTable.fnSettings();
			oSettings.aoColumns[4].sType = "custom-date";
			oSettings.aoColumns[5].sType = "custom-date";
			oSettings.aoColumns[9].sType = "custom-date";
			oSettings.aoColumns[10].sType = "custom-numeric";
		}
	});
</script>
