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

	require "../_container/container_top.php";
	require_once ('business/TenantMgr.php');
	require_once ('business/TenancyMgr.php');
	require_once ('business/ReservationMgr.php');

	$tenantMgr = new TenantMgr();
	$tenancyMgr = new TenancyMgr();
	$arrTenancy = $tenancyMgr->getTenancys();
	$reservationMgr = new ReservationMgr();

// print_r($_POST);
	$action = getPost('action');
	$tenantid = getPost('tenantid');

	$arrParam = array();

	$filter_year = getPost('filter_year');
	if (empty($filter_year)) {
		// $filter_year = date("Y");
	}else{
		if ($filter_year <> "All")
			$arrParam['filter_year'] = $filter_year;
	}

	$filter_tenancy = getPost('filter_tenancy');
	if (!empty($filter_tenancy)){
		if ($filter_tenancy <> "All")
			$arrParam['tenancy_id'] = $filter_tenancy;
	}

include_once ("actionList.php");

$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);

	$arrTenant = $tenantMgr->getTenants($arrParam);
?>
	<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("menuTenants"); ?></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">
			<span class="icon32 icon-red icon-user"></span>
			<div><?php echo $tr->translate("AddTenant"); ?></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">
				<div class="row-fluid sortable">
					<div class="span4">
				<form class="form-horizontal" method="post" name="tenantForm" id="tenantForm" action="">
					<input type="hidden" name="action" id="action" value="" />
					<input type="hidden" name="tenantid" id="tenantid" value="" />
					<fieldset>
						<div class="control-group">
							<label class="control-label" for="filter_tenancy"><?php echo $tr->translate("Tenancy"); ?></label>
							<div class="controls">
								<select name="filter_tenancy" id="filter_tenancy" data-rel="chosen" class="filter-action">
									<option value="All" <?php if ($filter_tenancy == "All") echo " selected"; ?>><?php echo $tr->translate("All"); ?></option>
								<?php foreach ($arrTenancy as $tenancy) { ?>
									<option value="<?php echo $tenancy->tenancy_id;?>" <?php if ($filter_tenancy == $tenancy->tenancy_id) echo " selected"; ?>><?php echo $tenancy->name;?></option>
								<?php } ?>
								</select>
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="filter_year"><?php echo $tr->translate("Year"); ?></label>
							<div class="controls">
								<select name="filter_year" id="filter_year" data-rel="chosen" class="filter-action">
									<option value="All" <?php if ($filter_tenancy == "All") echo " selected"; ?>><?php echo $tr->translate("All"); ?></option>
									<?php foreach ($arrYears as $val) { ?>
										<option value="<?php echo $val;?>" <?php if ($filter_year == $val) echo " selected"; ?>><?php echo $val;?></option>
									<?php } ?>
								</select>
							</div>
						</div>
					</fieldset>
				</form>
					</div>
				</div>
			</div>
		</div>
	</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("Tenant"); ?></h2>
			</div>
			<div class="box-content">
				<div class="table-responsive">
				<table id="tableTenants" name="tableTenants" class="table table-condensed table-striped table-bordered bootstrap-datatable datatable">
				  <thead>
					  <tr>
						  <th><?php echo $tr->translate("thTenantLastName"); ?></th>
						  <th><?php echo $tr->translate("thTenantName"); ?></th>
						  <th><?php echo $tr->translate("thTenantAddress1"); ?></th>
						  <th><?php echo $tr->translate("thTenantZipCode"); ?></th>
						  <th><?php echo $tr->translate("thTenantCity"); ?></th>
						  <th><?php echo $tr->translate("thTenantCountry"); ?></th>
						  <th><?php echo $tr->translate("thTenantPhone"); ?></th>
						  <th><?php echo $tr->translate("thTenantEmail"); ?></th>
						  <th><?php echo $tr->translate("thTenantComment"); ?></th>
						  <th><?php echo $tr->translate("thTenantAction"); ?></th>
					  </tr>
				  </thead>   
				  <tbody>
				  <?php
					foreach ($arrTenant as $tenant) {
				  ?>
					<tr>
						<td><?php echo $tenant->lastname; ?></td>
						<td><?php echo $tenant->name; ?></td>
						<td><?php echo $tenant->address1; ?></td>
						<td><?php echo $tenant->zipcode; ?></td>
						<td><?php echo $tenant->city; ?></td>
						<td><?php echo $tenant->country; ?></td>
						<td><?php echo $tenant->phone; ?></td>
						<td>
						<?php if ($tenant->email) { ?>
							<a class="icon-envelope" href="mailto:<?php echo $tenant->email; ?>"></a>
						<?php } ?>
						<?php echo $tenant->email; ?></td>
						<td><?php echo getTextSummary($tenant->comment); ?></td>
						<td>
							<a class="btn btn-info btn-mini btn-action" title="<?php echo $tr->translate("tipTenantEdit"); ?>" data-rel="tooltip" href="#" _id="<?php echo $tenant->tenant_id; ?>" _page="tenantDetail.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("tipTenantDelete"); ?>" data-rel="tooltip" href="#" onclick="goAction('DELETE_TENANT', <?php echo $tenant->tenant_id; ?>);">
								<i class="icon-trash icon-white"></i>
							</a>
						</td>
					</tr>
					<?php } ?>
				  </tbody>
			  </table>
			  </div>
			</div>
		</div><!--/span-->
	
	</div><!--/row-->

	<div class="row-fluid sortable" id="editTenant">
	</div><!--/row-->
<?php require "../_container/container_bottom.php"; ?>
<script>
	function deleteAction() {
		blockUI();
		tenantForm.submit();
	}

	function goAction(action, id) {
		if (action == "DELETE_TENANT") {
			$("#action").val(action);
			$("#tenantid").val(id);
			$("#dialog-confirm").dialog("open");
		}
	}
</script>