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

	$userMgr = new UserMgr();

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

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

	$arrUser = $userMgr->getUserById ($id);
	if (empty($arrUser)) $arrUser = array(new UserVo());

?>

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

				<div class="row-fluid">
					<div class="span6">
						<div class="control-group">
							<label class="control-label" for="inputName"><?php echo $tr->translate("thUserName"); ?></label>
							<div class="controls">
								<input type="text" class="input-medium" id="inputName" name="inputName" value="<?php echo $arrUser[0]->username; ?>" <?php if (!$add) echo "disabled"; ?>>
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="inputName"><?php echo $tr->translate("thUserEmail"); ?></label>
							<div class="controls">
								<input type="text" class="input-medium" id="inputEmail" name="inputEmail" value="<?php echo $arrUser[0]->email; ?>">
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="inputOwner"><?php echo $tr->translate("thUserPassword"); ?></label>
							<div class="controls">
								<input type="password" class="input-medium" id="inputPassword1" name="inputPassword1" value="<?php if (!$add) echo "thisisafakevalue"; ?>">
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="inputOwner"><?php echo $tr->translate("thUserConfirmPassword"); ?></label>
							<div class="controls">
								<input type="password" class="input-medium" id="inputPassword2" name="inputPassword2" value="<?php if (!$add) echo "thisisafakevalue"; ?>">
								<?php if (!$add) { ?>
								<button type="button" name="changePassword" id="changePassword" class="btn btn-info btn-mini" onclick="goAction('CHANGE_PASSWORD');"><?php echo $tr->translate("btnChangePassword"); ?></button>
								<?php } ?>
							</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("thUserComment"); ?></label>
							<div class="controls">
							<textarea class="span6" id="inputComment" name="inputComment" rows="6"><?php echo $arrUser[0]->comment; ?></textarea>
							</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"><?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() {
		$("#userForm").validate({
			rules: {
				inputName: {required: true},
				inputPassword1: {required: true, minlength: 5},
				inputPassword2: {required: true, minlength: 5, equalTo: "#inputPassword1"}
			},
			errorPlacement: function(error, element) {
				error.appendTo( $(element).parent());
			}
		});
	});
	$("#add, #update").click(function(e){
		var isvalidate=$("#userForm").valid();
		if(isvalidate){
			$("#action").val($(this).attr("id"));
			blockUI();
			userForm.submit();
		}else{
			e.preventDefault();
			return false;
		}
	});
	function goAction(action, id,status){
		if (action = "CHANGE_PASSWORD"){
			if ($("#inputPassword1").val() != "thisisafakevalue") {
				var isvalidate=$("#userForm").valid();
				if(isvalidate){
					$("#action").val(action);
					blockUI();
					userForm.submit();
				}
			}else{
				alert("<?php echo $tr->translate("msgPasswordDifferent"); ?>");
			}
		}
	}
</script>
