<?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/TenancyMgr.php');

	$tenancyMgr = new TenancyMgr();

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

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

	$arrTenancy = $tenancyMgr->getTenancy($id);
	if (empty($arrTenancy)) $arrTenancy = array(new TenancyVo());

?>

	<div>
		<ul class="breadcrumb">
			<li><a href="./"><?php echo $tr->translate("menuHome"); ?></a> <span class="divider">/</span></li>
			<li><a href="tenancy.php"><?php echo $tr->translate("menuTenancys"); ?></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("Tenancy"); ?></h2>
			</div>
			<div class="box-content">
			<?php if (!$add) { ?>
			<h6>[id: <?php echo $arrTenancy[0]->tenancy_id; ?>] // [recorddate: <?php echo $arrTenancy[0]->recorddate; ?>]</h6> 
			<?php } ?>
				<form enctype="multipart/form-data" class="form-horizontal" method="post" name="tenancyForm" id="tenancyForm" action="tenancy.php">
				<input type="hidden" name="action" id="action" value="" />
				<input type="hidden" name="tenancyid" id="tenancyid" value="<?php echo $id; ?>" /> 
				<input type="hidden" name="fileid" id="fileid" value="" /> 
				<input type="hidden" name="enable" id="enable" value="" /> 

				<?php if (!$add) { ?>
				<div class="row-fluid">
					<div class="span12">
						<?php if (!empty($arrTenancy[0]->imagepath)) {?>
						<div class="control-group">
							<label class="control-label" for="currentImage"><?php echo $tr->translate("thTenancyCurrentImage"); ?></label>
							<div class="controls">
								<img class="dashboard-avatar" src='<?php echo TENANCY_IMAGE_PATH_RESS . "/";?><?php echo $arrTenancy[0]->imagepath;?>' />
								<?php if (!(empty($arrTenancy[0]->imagepath)) && (!($arrTenancy[0]->imagepath == "no-image.jpg"))) { ?>
									<button type="button" name="deleteImage" id="deleteImage" class="btn btn-danger btn-mini"><?php echo $tr->translate("btnTenancyDeleteImage"); ?></button>
								<?php } ?>
							</div>
						</div>
						<?php } ?>
						<div class="control-group">
							<div class="controls">
								<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
								<input type="file" id="uploadfile_ImagePath" name="uploadfile_ImagePath" size="24" title="<?php echo $tr->translate("ChooseFileTitle"); ?>" />
								<button type="button" class="btn btn-primary btn-mini" id="add_image" name="add_image" onclick="goAction('add_image', <?php echo $arrTenancy[0]->tenancy_id; ?>)"> <?php echo $tr->translate("btnTenancyNewImage"); ?> <i class=" icon-white  icon-picture"></i> </button>
							</div>
						</div>
					</div>
				</div>
				<?php } ?>
				<div class="row-fluid">
					<div class="span6">
						<div class="control-group">
							<label class="control-label" for="inputName"><?php echo $tr->translate("thTenancyName"); ?></label>
							<div class="controls">
							<input type="text" class="input-medium" id="inputName" name="inputName" value="<?php echo $arrTenancy[0]->name; ?>">
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="inputBeds"><?php echo $tr->translate("thTenancyBeds"); ?></label>
							<div class="controls">
								<div class="input-prepend">
									<a href="#"><span class="add-on icon-minus-sign qtyminus" data-field="inputBeds"></span></a><input type="text" class="input-mini" id="inputBeds" name="inputBeds" value="<?php echo (empty($arrTenancy[0]->beds)) ? 0 : $arrTenancy[0]->beds; ?>"><a href="#"><span class="add-on icon-plus-sign qtyplus" data-field="inputBeds"></span></a>
								</div>
							</div>
						</div>
					</div>
					<div class="span6">
						<div class="control-group">
							<label class="control-label" for="inputAddress1"><?php echo $tr->translate("thTenancyAddress1"); ?></label>
							<div class="controls">
								<input type="text" class="input-medium" id="inputAddress1" name="inputAddress1" value="<?php echo $arrTenancy[0]->address1; ?>">
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="inputAddress2"><?php echo $tr->translate("thTenancyAddress2"); ?></label>
							<div class="controls">
								<input type="text" class="input-medium" id="inputAddress2" name="inputAddress2" value="<?php echo $arrTenancy[0]->address2; ?>">
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="inputZipcode"><?php echo $tr->translate("thTenancyZipCode"); ?></label>
							<div class="controls">
								<input type="text" class="input-medium" id="inputZipcode" name="inputZipcode" value="<?php echo $arrTenancy[0]->zipcode; ?>">
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="inputCity"><?php echo $tr->translate("thTenancyCity"); ?></label>
							<div class="controls">
								<input type="text" class="input-medium" id="inputCity" name="inputCity" value="<?php echo $arrTenancy[0]->city; ?>">
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="inputCountry"><?php echo $tr->translate("thTenancyCountry"); ?></label>
							<div class="controls">
								<input type="text" class="input-medium" id="inputCountry" name="inputCountry" value="<?php echo $arrTenancy[0]->country; ?>">
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="inputState"><?php echo $tr->translate("thTenancyState"); ?></label>
							<div class="controls">
								<input type="text" class="input-medium" id="inputState" name="inputState" value="<?php echo $arrTenancy[0]->state; ?>">
							</div>
						</div>
					</div>
				</div>
				<div class="row-fluid">
					<div class="span12">
						<div class="control-group">
							<label class="control-label" for="inputComment"><?php echo $tr->translate("thTenancyComment"); ?></label>
							<div class="controls">
							<textarea class="span6" id="inputComment" name="inputComment" rows="6"><?php echo $arrTenancy[0]->comment; ?></textarea>
							</div>
						</div>
					</div>
				</div>
				<div class="row-fluid">
					<div class="span12">
						<div class="control-group">
							<label class="control-label" for="inputField1"><?php echo $tr->translate("thTenancyField1"); ?></label>
							<div class="controls">
								<input id="inputField1" name="inputField1" type="text" class="tags span6" value="<?php echo $arrTenancy[0]->field1; ?>">
							</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" id="add" class="btn btn-primary"><?php echo $tr->translate("btnAdd"); ?></button>
						<?php }else{ ?>
							<button type="btn" name="update" id="update" class="btn btn-primary"><?php echo $tr->translate("btnSave"); ?></button>
							<button type="button" name="delete" id="delete" class="btn btn-danger" onclick="goAction('DELETE_TENANCY',<?php echo $arrTenancy[0]->tenancy_id; ?>)"><?php echo $tr->translate("btnDelete"); ?></button>
						<?php } ?>
						</div>
						</div>
					</fieldset>
					</div>
				</div> 
			</form>
			</div>
		</div><!--/span-->
	</div>
<?php require "../_container/container_bottom.php"; ?>
<script>
	$(document).ready(function() {
		$("#tenancyForm").validate({
			rules: {
				inputName: {required: true},
				inputBeds: {number: true}
			},
			errorPlacement: function(error, element) {
				error.appendTo( $(element).parent());
			}
		});
	});
	$("#uploadfile").click(function(e){
		e.preventDefault();
		if ($("#uploadfile_Path").val() != '') {
		blockUI();
			tenancyFileForm.submit();
		}else{
			alert("<?php echo $tr->translate("ChooseFileTitle"); ?>");
			return false;
		}
	});
	$("#add, #update").click(function(e){
		var isvalidate = $("#tenancyForm").valid();
		if(isvalidate){
			$("#action").val($(this).attr("id"));
			blockUI();
			tenancyForm.submit();
		}else{
			e.preventDefault();
			return false;
		}
	});
	$("#deleteImage").click(function(e){
		e.preventDefault();
		$("#action").val("DELETE_IMAGE");
		blockUI();
		tenancyForm.submit();
	});
	$("#filePreview").click(function(){
		$().redirect(
			"filePreview.php",
			{
				'id': $(this).attr("_id")
			},
			'POST'
		);
		return false;
	});

	function goAction(action, id,status){
		$("#action").val(action);
		$("#fileid").val(id);
		$("#enable").val(status);
		blockUI();
		tenancyForm.submit();
	}
</script>
