<?php include "../index/conexion.inc" ?>
<?php include "../index/validacion.php" ?>
<!doctype html>
<html lang="es">
	<head>
		<?php include '../index/head.php'; ?>
		<?php
			$mensaje = '';
			if (isset($_POST['Guardar']))
			{
				//Crear variables
				$cR01 = $_POST['cV01'];			// Login
				$cR02 = $_POST['cV02'];			// Nombre
				$cR03 = '';						// Confirma password
				$cR04 = $_SESSION['mImagen'];	// Imagen
				$cR05 = $_SESSION["mDpto"];		// Departamento
				$cR06 = $_POST['cV04'];			// Correo
				$cR07 = '';						// Password Correo
				// VARIABLES PARA COMPRACION
				$cC01 = $_POST['cV07'];			// Password Actual
				$cC02 = $_POST['cV08'];			// Nuevo password
				$cC03 = $_POST['cV09'];			// Confirma Nuevo password
				$cC10 = $_POST['cV05'];			// Password correo
				$cC11 = $_POST['cV06'];			// Password correo confirmación
				$cError = 0;
				// PASSWORD
				if ($cC03 != '')
				{
					if ($cC02 != $cC03)
					{
						$mensaje = "El password ingresado no coincide, por favor revísalo";
						$cError = 1;
					}
					else
					{
						$cR03 = encrypt($cC03, "Apeesys8");
					}
				}
				else
				{
					$cR03 = encrypt($cC01, "Apeesys8");
				}
				// PASSWORD CORREO
				if ($cC11 != '')
				{
					if ($cC10 != $cC11)
					{
						$mensaje = "El password del correo no coincide, por favor revísalo";
						$cError = 1;
					}
					else
					{
						$cR07 = encrypt($cC11, "Apeesys8");
					}
				}
				else
				{
					$cR07 = '';
				}
				// PROCESO DE AUTORIZACION
				if ($cError == 0)
				{
					// conexión
					$servername = $svr_servidor;
					$conectioninfo = array("Database"=>$svr_base, "UID"=>$svr_usuario, "PWD"=>$svr_contrasena, "CharacterSet"=>"UTF-8");
					$conexion = sqlsrv_connect($servername, $conectioninfo);
					if ($conexion == false)
					{
						$mensaje = 'Existe un error al intentar conectarse con el servidor...';
					}
					else
					{
						$sql = "UPDATE Usuarios SET \n"
							  ."Nombre = '".$cR02."', \n"
							  ."Password = '".$cR03."', \n"
							  ."Correo = '".$cR06."', \n"
							  ."PasswordCorreo = '".$cR07."' \n"
							  ."WHERE (Login = '".$cR01."')";
						$pa1 = array();
						$op1 = array("scrollable" => SQLSRV_CURSOR_KEYSET);
						// Lanzar la consulta
						$re1 = sqlsrv_query($conexion, $sql);
						if ( $re1 )
						{
							 $something = "Submission successful.";
						}
						else
						{
							 $something = "Submission unsuccessful.";
							 die( print_r( sqlsrv_errors(), true));
						}
						sqlsrv_free_stmt($re1);
					}
					sqlsrv_close($conexion);
				}
			}
		?>
		<?php
			// conexión
			$servername = $svr_servidor;
			$conectioninfo = array("Database"=>$svr_base, "UID"=>$svr_usuario, "PWD"=>$svr_contrasena, "CharacterSet"=>"UTF-8");
			$conexion = sqlsrv_connect($servername, $conectioninfo);
			if ($conexion == false)
			{
				$mensaje = "Existe un error al intentar conectarse con el servidor...";
			}
			else
			{
				$SQL01 = '';			// Codigo
				$SQL02 = '';			// Nombre
				$SQL03 = 0;				// Departamento
				$SQL04 = '';			// Correo
				$SQL05 = '';			// Password Correo
				$SQL06 = '';			// Password Actual
				// Consulta BASE TIPO Y UNIDADES
				$sql = "SELECT usuarios.* FROM usuarios WHERE (Login = '".$_SESSION['mLogin']."')";
				$params = array();
				$options = array("scrollable" => SQLSRV_CURSOR_KEYSET);
				// Lanzar la consulta
				$re1 = sqlsrv_query($conexion, $sql, $params, $options);
				while($fila = sqlsrv_fetch_array($re1))
				{
					$SQL01 = $fila['Login'];
					$SQL02 = $fila['Nombre'];
					$SQL03 = $fila['IdDepartamento'];
					$SQL04 = $fila['Correo'];
					$SQL05 = decrypt($fila['PasswordCorreo'], "Apeesys8");
					$SQL06 = decrypt($fila['Password'], "Apeesys8");
				}
				sqlsrv_free_stmt($re1);
			}
			sqlsrv_close($conexion);
		?>
	</head>
	<body class="nav-md">
		<div class="container body">
			<div class="main_container">
				<?php include '../index/body.php'; ?>
				<!-- page content -->
				<div class="right_col" role="main">
					<div class="">
						<!-- Pedidos cabecera -->
						<div class="clearfix"></div>
						<div class="x_panel">
							<div class="x_title">
								<h2>Cuenta <small>Datos</small></h2>
								<ul class="nav navbar-right panel_toolbox">
									<li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
									</li>
								</ul>
								<div class="clearfix"></div>
							</div>
							<div class="x_content">
								<form name="neo" class="form-vertical" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
									<div class="col-md-3 col-sm-3 col-xs-12 profile_left">
										<div class="profile_img">
											<div id="crop-avatar">
												<!-- Current avatar -->
												<img class="img-responsive avatar-view" src="<?php echo $_SESSION['mImagen']; ?>" alt="Avatar" title="Change the avatar">
											</div>
										</div>
										<h3><?php echo $_SESSION['mNombre']; ?></h3>
									</div>
									<div class="col-md-9 col-sm-9 col-xs-12">
										<div class="" role="tabpanel" data-example-id="togglable-tabs">
											<ul id="myTab" class="nav nav-tabs bar_tabs" role="tablist">
											<li role="presentation" class="active"><a href="#tab_content1" id="home-tab" role="tab" data-toggle="tab" aria-expanded="true">Datos</a>
											</li>
											<li role="presentation" class=""><a href="#tab_content2" role="tab" id="profile-tab" data-toggle="tab" aria-expanded="false">Correo empresarial</a>
											</li>
											<li role="presentation" class=""><a href="#tab_content3" role="tab" id="profile-tab" data-toggle="tab" aria-expanded="false">Password</a>
											</li>
											</ul>
											<div id="myTabContent" class="tab-content">
												<!-- Datos generales -->
												<div role="tabpanel" class="tab-pane fade active in" id="tab_content1" aria-labelledby="home-tab">
													<h3 class="prod_title">Datos generales</h3>
													
													<div class="row">
														<div class="col-md-2">
															<div class="form-group">
																<label>Código</label>
																<input type="text" class="form-control" value="<?php echo $SQL01; ?>" name="cV01" readonly>	<!-- Codigo de usuario -->
															</div>
														</div>
													</div>
													<div class="row">
														<div class="col-md-6">
															<div class="form-group">
																<label>Nombre</label>
																<input type="text" class="form-control" value="<?php echo $SQL02; ?>" name="cV02">	<!-- Nombre de usuario -->
															</div>
														</div>
													</div>
													<div class="row">
														<div class="col-md-6">
															<div class="form-group">
																<label>Departamento</label>
																<input type="text" class="form-control" value="<?php echo $SQL03; ?>" name="cV03">	<!-- Departamento -->
															</div>
														</div>
													</div>
												</div> <!-- end Datos -->
												<!-- Correo -->
												<div role="tabpanel" class="tab-pane fade" id="tab_content2" aria-labelledby="profile-tab">
													<h3 class="prod_title">Correo empresarial</h3>
													<div class="row">
														<div class="col-md-6">
															<div class="form-group">
																<label>Correo</label>
																<input type="email" class="form-control" placeholder="ejemplo@prophar.com" pattern=".+@prophar.com" size="30" value="<?php echo $SQL04; ?>" name="cV04">	<!-- Correo -->
															</div>
														</div>
													</div>
													<div class="row">
														<div class="col-md-6">
															<div class="form-group">
																<label>Password</label>
																<input type="password" class="form-control" value="<?php echo $SQL05; ?>" name="cV05">	<!-- Password -->
															</div>
														</div>
													</div>
													<div class="row">
														<div class="col-md-6">
															<div class="form-group">
																<label>Confirme password</label>
																<input type="password" class="form-control" value="<?php echo $SQL05; ?>" name="cV06">	<!-- Confirme password -->
															</div>
														</div>
													</div>
												</div> <!-- end correo -->
												<!-- Password -->
												<div role="tabpanel" class="tab-pane fade" id="tab_content3" aria-labelledby="profile-tab">
													<h3 class="prod_title">Configuración de password</h3>
													<div class="row">
														<div class="col-md-6">
															<div class="form-group">
																<label>Password actual</label>
																<input type="password" class="form-control" value="<?php echo $SQL06; ?>" name="cV07" readonly>	<!-- Password actual -->
															</div>
														</div>
													</div>
													<div class="row">
														<div class="col-md-6">
															<div class="form-group">
																<label>Nuevo password</label>
																<input type="password" class="form-control" value="" name="cV08">	<!-- Nuevo Password -->
															</div>
														</div>
													</div>
													<div class="row">
														<div class="col-md-6">
															<div class="form-group">
																<label>Confirme nuevo password</label>
																<input type="password" class="form-control" value="" name="cV09">	<!-- Confirme nuevo password -->
															</div>
														</div>
													</div>
												</div> <!-- end password -->
											</div>
										</div>
									</div>
									<div class="x_content">
										<button type="submit" class="btn btn-dark" name="Guardar">
											Guardar/Actualizar
										</button>
									</div>
								</form>
								<?php echo $mensaje; ?>
							</div>
						</div>
					</div>
				</div>
				<!-- /page content -->
			</div>
		</div>
		<?php include '../index/footer.php'; ?>
		<?php include '../index/fbody.php'; ?>
	</body>
</html>