<?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/ReservationMgr.php');
	require_once ('business/PaymentMgr.php');

	$tenantMgr = new TenantMgr();
	$reservationMgr = new ReservationMgr();
	$paymentMgr = new PaymentMgr();

	$action = getPost('action');
	$id = getPost('id');

	if (empty($action)) $action = "Add";
	$add = ($action == "Add") ? true : false;

	$arrTenant = $tenantMgr->getTenant($id);
	$arrReservation = $reservationMgr->getReservationFromTenant($id);
	$arrPayment = $paymentMgr->getPaymentByTenant($id);
	
	if (empty($arrTenant)) $arrTenant = array(new TenantVo());
?>

	<div>
		<ul class="breadcrumb">
			<li><a href="./"><?php echo $tr->translate("menuHome"); ?></a> <span class="divider">/</span></li>
			<li><a href="tenant.php"><?php echo $tr->translate("menuTenants"); ?></a> <span class="divider">/</span></li>
			<li class="active"><?php echo $tr->translate($action); ?></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 ui-sortable">
		<div class="box span12">
			<div class="box-header well" data-original-title="">
				<h2><i class="icon-th"></i> <?php echo $tr->translate("Tenant"); ?></h2>
			</div>
			<div class="box-content">
			<?php if (!$add) { ?>
			<h6>[id: <?php echo $arrTenant[0]->tenant_id; ?>] // [recorddate: <?php echo $arrTenant[0]->recorddate; ?>]</h6>
			<?php } ?>
				<form class="form-horizontal" method="post" name="tenantForm" id="tenantForm" action="tenant.php">
				<input type="hidden" name="action" id="action" value="" />
				<input type="hidden" name="tenantid" id="tenantid" value="<?php echo $id; ?>" /> 

				<div class="row-fluid">
					<div class="span6">
						<div class="control-group">
							<label class="control-label" for="inputTitle"><?php echo $tr->translate("thTenantTitle"); ?></label>
							<div class="controls">
								<select id="inputTitle" name="inputTitle">
									<option></option>
									<?php $arrTitle = $tr->translate("thUserformTitles");
										foreach($arrTitle as $title) { ?>
											<option value="<?php echo $title; ?>" <?php if ($title == $arrTenant[0]->title) echo " selected"; ?>><?php echo $title; ?></option>
									<?php } ?>
								</select>
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="inputLastname"><?php echo $tr->translate("thTenantLastName"); ?></label>
							<div class="controls">
								<input type="text" class="input-medium" id="inputLastname" name="inputLastname" value="<?php echo $arrTenant[0]->lastname; ?>" required="">
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="inputName"><?php echo $tr->translate("thTenantName"); ?></label>
							<div class="controls">
								<input type="text" class="input-medium" id="inputName" name="inputName" value="<?php echo $arrTenant[0]->name; ?>" required="">
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="inputReference"><?php echo $tr->translate("thTenantRefer"); ?></label>
							<div class="controls">
								<select id="inputReference" name="inputReference">
									<option></option>
									<?php $arrTitle = explode(",", $config['tenant_reference']);
										foreach($arrTitle as $title) { ?>
											<option value="<?php echo $title; ?>" <?php if ($title == $arrTenant[0]->reference) echo " selected"; ?>><?php echo $title; ?></option>
									<?php } ?>
								</select>
							</div>
						</div> 				
					</div>
					<div class="span6">
						<div class="control-group">
						<label class="control-label" for="inputAddress1"><?php echo $tr->translate("thTenantAddress1"); ?></label>
							<div class="controls">
								<input type="text" class="input-medium" id="inputAddress1" name="inputAddress1" value="<?php echo $arrTenant[0]->address1; ?>">
							</div>
						</div>
						<div class="control-group">
						<label class="control-label" for="inputAddress2"><?php echo $tr->translate("thTenantAddress2"); ?></label>
							<div class="controls">
								<input type="text" class="input-medium" id="inputAddress2" name="inputAddress2" value="<?php echo $arrTenant[0]->address2; ?>">
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="inputZipcode"><?php echo $tr->translate("thTenantZipCode"); ?></label>
							<div class="controls">
								<input type="text" class="input-medium" id="inputZipcode" name="inputZipcode" value="<?php echo $arrTenant[0]->zipcode; ?>">
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="inputCity"><?php echo $tr->translate("thTenantCity"); ?></label>
							<div class="controls">
								<input type="text" class="input-medium" id="inputCity" name="inputCity" value="<?php echo $arrTenant[0]->city; ?>">
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="inputCountry"><?php echo $tr->translate("thTenantCountry"); ?></label>
							<div class="controls">
								<input type="text" class="input-medium" id="inputCountry" name="inputCountry" value="<?php echo $arrTenant[0]->country; ?>">
							</div>
						</div> 				
					</div>
				</div>
				<div class="row-fluid">
					<div class="span6">
						<div class="control-group">
							<label class="control-label" for="inputPhone"><?php echo $tr->translate("thTenantPhone"); ?></label>
							<div class="controls">
								<input type="text" class="input-medium" id="inputPhone" name="inputPhone" value="<?php echo $arrTenant[0]->phone; ?>">
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="inputCellphone"><?php echo $tr->translate("thTenantMobile"); ?></label>
							<div class="controls">
								<input type="text" class="input-medium" id="inputCellphone" name="inputCellphone" value="<?php echo $arrTenant[0]->cellphone; ?>">
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="inputFax"><?php echo $tr->translate("thTenantFax"); ?></label>
							<div class="controls">
								<input type="text" class="input-medium" id="inputFax" name="inputFax" value="<?php echo $arrTenant[0]->fax; ?>">
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="inputEmail"><?php echo $tr->translate("thTenantEmail"); ?></label>
							<div class="controls">
								<input type="text" class="input-medium" id="inputEmail" name="inputEmail" value="<?php echo $arrTenant[0]->email; ?>">
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="inputNewsletter"><?php echo $tr->translate("thTenantNewsletter"); ?></label>
							<div class="controls">
								<input type="checkbox" id="inputNewsletter" name="inputNewsletter"<?php if (!empty($arrTenant[0]->newsletter)) echo " checked"; ?>>
							</div>
						</div> 
					</div>
					<div class="span6">
						<div class="control-group">
							<label class="control-label" for="inputLanguage"><?php echo $tr->translate("thTenantLanguage"); ?></label>
							<div class="controls">
								<select id="inputLanguage" name="inputLanguage">
									<option></option>
									<?php $arrLang = $tr->translate("thTenantLanguageNames");
										foreach($arrLang as $lng) { ?>
											<option value="<?php echo $lng; ?>" <?php if ($lng == $arrTenant[0]->language) echo " selected"; ?>><?php echo $lng; ?></option>
									<?php } ?>
								</select>
							</div>
						</div>
					</div>
					<div class="row-fluid">
						<div class="span12">
							<div class="control-group">
								<label class="control-label" for="inputComment"><?php echo $tr->translate("thTenantComment"); ?></label>
								<div class="controls">
									<textarea class="span6" id="inputComment" name="inputComment" rows="6"><?php echo $arrTenant[0]->comment; ?></textarea>
								</div>
							</div>

						</div>
					</div>

				</div>
				<div class="row-fluid">
					<div class="span12">
						<fieldset>
						<div class="control-group">
						<div class="form-actions">
						<?php if ($add) { ?>
							<button type="btn" name="ADD_TENANT" id="add" class="btn btn-primary"><?php echo $tr->translate("btnAdd"); ?></button>
						<?php }else{ ?>
							<button type="btn" name="UPDATE_TENANT" id="update" class="btn btn-primary"><?php echo $tr->translate("btnSave"); ?></button>
							<button type="btn" name="DELETE_TENANT" id="delete" class="btn btn-danger btn-action"><?php echo $tr->translate("btnDelete"); ?></button>
						<?php } ?>
						</div>
						</div>
					</fieldset>
					</div>
				</div> 
				</form>
			</div>
		</div><!--/span-->
	</div>
	<?php if (!$add) { ?>
	<div class="row-fluid sortable">
		<div class="box span6">
			<div class="box-header well" data-original-title>
				<h2><?php echo $tr->translate("thTenantReservations"); ?></h2>
			</div>
			<div class="box-content">
				<div class="table-responsive">
				<table id="tableTenantReserv" name="tableTenantReserv" class="table table-condensed table-striped table-bordered bootstrap-datatable datatable">
				  <thead>
					  <tr>
						  <th><?php echo $tr->translate("Tenancy"); ?></th>
						  <th><?php echo $tr->translate("From"); ?></th>
						  <th><?php echo $tr->translate("To"); ?></th>
						  <th><?php echo $tr->translate("Days"); ?></th>
						  <th><?php echo $tr->translate("Price") . " " . $config['currency_symbol']; ?></th>
						  <th><?php echo $tr->translate("Actions"); ?></th>
					  </tr>
				  </thead>   
				  <tbody>
				  <?php
					$totalPrice = 0;
					foreach ($arrReservation as $reservation) {
						$totalPrice += $reservation->price;
				  ?>
					<tr>
						<td><?php echo $reservation->tenancy_name; ?></td>
						<td><?php echo date($config['date_format'], strtotime($reservation->arrival_date)); ?></td>
						<td><?php echo date($config['date_format'], strtotime($reservation->departure_date)); ?></td>
						<td class="center"><?php echo round((strtotime($reservation->departure_date) - strtotime($reservation->arrival_date))/86400); ?></td>
						<td><?php echo $reservation->price; ?></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>
						</td>
					</tr>
					<?php } ?>
					</tbody>
					<tfoot>
						<tr>
							<th><?php echo $tr->translate("Total"); ?></th>
							<th></th>
							<th></th>
							<th></th>
							<th><?php echo $totalPrice . " " . $config['currency_symbol']; ?></th>
							<th></th>
						</tr>
					</tfoot>
				</table>
				</div>
			</div>	</div>
		<div class="box span6">
			<div class="box-header well" data-original-title>
				<h2><?php echo $tr->translate("Payments"); ?></h2>
			</div>
			<div class="box-content">
				<div class="table-responsive">
				<table id="tableTenantPayments" name="tableTenantPayments" class="table table-condensed table-striped table-bordered bootstrap-datatable datatable">
					<thead>
						<tr>
							<th><?php echo $tr->translate("thPaymentDate"); ?></th>
							<th><?php echo $tr->translate("thPaymentType"); ?></th>
							<th><?php echo $tr->translate("thPaymentMethod"); ?></th>
							<th><?php echo $tr->translate("thPaymentAmount") . " " . $config['currency_symbol']; ?></th>
						</tr>
					</thead>   
					<tbody>
					<?php
						$totalPrice = 0;
						foreach ($arrPayment as $payment) {
							$totalPrice += $payment->amount;
					?>
					<tr>
						<td><?php echo date($config['date_format'], strtotime($payment->payment_date)); ?></td>
						<td><?php echo $payment->type; ?></td>
						<td><?php echo $payment->method; ?></td>
						<td><?php echo $payment->amount; ?></td>
					</tr>
					<?php } ?>
					</tbody>
					<tfoot>
						<tr>
							<th><?php echo $tr->translate("Total"); ?></th>
							<th></th>
							<th></th>
							<th><?php echo $totalPrice . " " . $config['currency_symbol']; ?></th>
						</tr>
					</tfoot>
				</table>
				</div>
			</div>
		</div>
	</div>
	<?php } ?>
<?php require "../_container/container_bottom.php"; ?>
<script>
	$(document).ready(function() {
		$("#tenantForm").validate({
			rules: {
				selectTenant:{required: true, min:1},
				inputName: {required: true},
				inputLastname: {required: true},
				startDate: {required: true},
				endDate: {required: true}
			},
			errorPlacement: function(error, element) {
				error.appendTo( $(element).parent());
			},
			messages: {
				selectTenant: {
					min:"<?php echo $tr->translate("SelectTenant"); ?>"
				}
			}
		});
	});
	$("#add, #update, #delete").click(function(e){
		var isvalidate = $("#tenantForm").valid();
		if(isvalidate){
			$("#action").val($(this).attr("name"));
			blockUI();
			tenantForm.submit();
		}else{
			e.preventDefault();
			return false;
		}
	});
</script>
