<?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";
	include_once ('business/TenancyMgr.php');
	$tenancyMgr = new TenancyMgr();

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

	$tenancyId = getPost('tenancyid');

	if (!empty($action) && ($action == "update" || $action == "add_image" || $action == "DELETE_IMAGE" || $action == "DELETE_TENANCY" || $action == "disable" || $action == "add" || $action == "uploadfile" || $action == "ENABLE_FILE" || $action == "DELETE_FILE")) {
		if ($action == "add") {
			$tenancy2 = array();
			$tenancy2['title'] = getPost('inputTitle');
			$tenancy2['name'] = getPost('inputName');
			$tenancy2['lastname'] = getPost('inputLastname');
			$tenancy2['address1'] = getPost('inputAddress1');
			$tenancy2['address2'] = getPost('inputAddress2');
			$tenancy2['zipcode'] = getPost('inputZipcode');
			$tenancy2['city'] = getPost('inputCity');
			$tenancy2['country'] = getPost('inputCountry');
			$tenancy2['state'] = getPost('inputState');
			$tenancy2['vat'] = getPost('inputVat');
			$tenancy2['phone'] = getPost('inputPhone');
			$tenancy2['cellphone'] = getPost('inputCellphone');
			$tenancy2['fax'] = getPost('inputFax');
			$tenancy2['email'] = getPost('inputEmail');
			$tenancy2['newsletter'] = getPost('inputNewsletter');
			$tenancy2['comment'] = getPost('inputComment');
			$tenancy2['field1'] = getPost('inputField1');
			$tenancy2['beds'] = getPost('inputBeds');
			$res = $tenancyMgr->insert($tenancy2);
			if ($res > 0) {
				$msgType = "success";
				$msg = $tr->translate("msgSuccess");
			}else {
				$msgType = "error";
				$msg = $tr->translate("msgError");
			}
		}
		if ($action == "update") {
			$tenancy2 = array();
			$tenancy2['title'] = getPost('inputTitle');
			$tenancy2['name'] = getPost('inputName');
			$tenancy2['lastname'] = getPost('inputLastname');
			$tenancy2['address1'] = getPost('inputAddress1');
			$tenancy2['address2'] = getPost('inputAddress2');
			$tenancy2['zipcode'] = getPost('inputZipcode');
			$tenancy2['city'] = getPost('inputCity');
			$tenancy2['country'] = getPost('inputCountry');
			$tenancy2['state'] = getPost('inputState');
			$tenancy2['vat'] = getPost('inputVat');
			$tenancy2['phone'] = getPost('inputPhone');
			$tenancy2['cellphone'] = getPost('inputCellphone');
			$tenancy2['fax'] = getPost('inputFax');
			$tenancy2['email'] = getPost('inputEmail');
			$tenancy2['newsletter'] = getPost('inputNewsletter');
			$tenancy2['comment'] = getPost('inputComment');
			$tenancy2['field1'] = getPost('inputField1');
			$tenancy2['beds'] = getPost('inputBeds');
			$tenancy2['tenancy_id'] = $tenancyId;
			$res = $tenancyMgr->update($tenancy2);
			if($res == 1) {
				$msgType = "success";
				$msg = $tr->translate("msgSuccess");
			}else{
				$msgType = "error";
				$msg = $tr->translate("msgError");
			}
		}
		if ($action == "add_image") {
			$arrTenancy = $tenancyMgr->getTenancy($tenancyId);
			if ((file_exists(TENANCY_IMAGE_PATH . "/" . $arrTenancy[0]->imagepath)) && ($arrTenancy[0]->imagepath != "no-image.jpg")){
				unlink(TENANCY_IMAGE_PATH . "/" . $arrTenancy[0]->imagepath);
			}

			require_once("lib/myUpload.php");
			$upload = new MYUPLOAD('uploadfile_ImagePath');
			$upload->set_directory(TENANCY_IMAGE_PATH);
			$upload->set_file_name("tenancy_" . $tenancyId . "_" . str_replace(' ', '_', $_FILES['uploadfile_ImagePath']['name']));
			$upload->SetDeniedExtension("bmp");
			$res = $upload->copy_file();
			if ($res && ($res[0] != 'error')){
				$msgType = $res[0];
				$msg = $res[1];
				$upload->resize(100,100);
				$tenancy2 = array();
				$tenancy2['tenancy_id'] = $tenancyId;
				$tenancy2['imagepath'] = $upload->user_file_name;
				$tenancyMgr->update($tenancy2);
			}else{
				$msgType = "error";
				$msg = $tr->translate("msgError");
			}
		
		}
		if ($action == "DELETE_TENANCY") {
			$res = $tenancyMgr->delete($tenancyId);
			if($res == 1) {
				$msgType = "success";
				$msg = $tr->translate("msgSuccess");
			}else{
				$msgType = "error";
				$msg = $tr->translate("msgError");
			}
		}

		if ($action == "DELETE_IMAGE") {
			$arrTenancy = $tenancyMgr->getTenancy($tenancyId);
			if ((file_exists(TENANCY_IMAGE_PATH . "/" . $arrTenancy[0]->imagepath)) && ($arrTenancy[0]->imagepath != "no-image.jpg")){
				unlink(TENANCY_IMAGE_PATH . "/" . $arrTenancy[0]->imagepath);
			}
			$tenancy2['imagepath'] = "no-image.jpg";
			$tenancy2['tenancy_id'] = $tenancyId;
			$tenancyMgr->update($tenancy2);
			$msgType = "success";
			$msg = $tr->translate("msgSuccess");
		}

		if ($action == "uploadfile") {
		}
		if ($action == "ENABLE_FILE") {
		}
		if ($action == "DELETE_FILE") {
		}
		echo '<div class="alert alert-' . $msgType . '">
			<strong>' . $msg . '</strong>
			</div>';
	}
	$arrTenancy = $tenancyMgr->getTenancys();

?>
	<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("menuTenancys"); ?></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("AddTenancy"); ?>" class="well span2 top-block btn-action" href="#" name="newTenancy" id="newTenancy" _page="tenancyDetail.php" _action="Add">
			<i class="icon-red icon-user"></i>
			<div><?php echo $tr->translate("AddTenancy"); ?></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>
	<form class="form-horizontal" method="post" name="tenancyForm" id="tenancyForm" action="">
		<input type="hidden" name="action" id="action" value="" />
		<input type="hidden" name="tenancyid" id="tenancyid" value="" />
	</form>
	<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("Tenancys"); ?></h2>
			</div>
			<div class="box-content">
				<div class="table-responsive">
				<table id="tableTenancys" name="tableTenancys" class="table table-condensed table-striped table-bordered bootstrap-datatable datatable">
				  <thead>
					  <tr>
						  <th></th>
						  <th><?php echo $tr->translate("thTenancyName"); ?></th>
						  <th><?php echo $tr->translate("thTenancyAddress1"); ?></th>
						  <th><?php echo $tr->translate("thTenancyZipCode"); ?></th>
						  <th><?php echo $tr->translate("thTenancyCity"); ?></th>
						  <th><?php echo $tr->translate("thTenancyCountry"); ?></th>
						  <th><?php echo $tr->translate("thTenancyComment"); ?></th>
						  <th><?php echo $tr->translate("thReservAction"); ?></th>
					  </tr>
				  </thead>   
				  <tbody>
				  <?php
					foreach ($arrTenancy as $tenancy) {
				  ?>
					<tr>
						<td><img style="height:25px;" src='<?php echo TENANCY_IMAGE_PATH_RESS . "/";?><?php echo $tenancy->imagepath; ?>' /></div>
						<td><?php echo $tenancy->name; ?></td>
						<td><?php echo $tenancy->address1; ?></td>
						<td><?php echo $tenancy->zipcode; ?></td>
						<td><?php echo $tenancy->city; ?></td>
						<td><?php echo $tenancy->country; ?></td>
						<td><?php echo getTextSummary($tenancy->comment); ?></td>
						<td>
							<a class="btn btn-info btn-mini btn-action" title="<?php echo $tr->translate("tipTenancyEdit"); ?>" data-rel="tooltip" href="#" _id="<?php echo $tenancy->tenancy_id; ?>" _page="tenancyDetail.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("tipTenancyDelete"); ?>" data-rel="tooltip" href="#" onclick="goAction('DELETE_TENANCY', <?php echo $tenancy->tenancy_id; ?>);">
								<i class="icon-trash icon-white"></i>
							</a>
							<a target="calendar" class="btn btn-success btn-mini" title="<?php echo $tr->translate("tipTenancyShowCalendar"); ?>" data-rel="tooltip" href="calendar.php?id=<?php echo $tenancy->tenancy_id; ?>&y=<?php echo date("Y"); ?>">
								<i class="icon-calendar icon-white"></i>
							</a>
						</td>
					</tr>
					<?php } ?>
				  </tbody>
			  </table>
			  </div>
			</div>
		</div><!--/span-->
	
	</div><!--/row-->

	<div class="row-fluid sortable" id="editTenancy">
	</div><!--/row-->
<?php require "../_container/container_bottom.php"; ?>
<script>
	function deleteAction() {
		// $('#content').slideUp("slow");
		blockUI();
		tenancyForm.submit();
	}

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