<?php include "conexion.inc" ?>
<?php include "validacion.php" ?>
<!doctype html>
<html lang="es">
	<head>
		<?php include '../index/head.php'; ?>
		<?php
			if (isset($_POST['Guardar']))
			{
				//Crear variables
				$cR01 = $_POST['cV01'];			// Serie
				$cR02 = $_POST['cV02'];			// Numero
				$cR03 = $_POST['cV04'];			// Estado
				// 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
				{
					/* ACTUALIZO RUTA */
					$sql = "UPDATE PEDVENTACAMPOSLIBRES SET ESTADO_AD = '".$cR03."' WHERE (NUMSERIE = '".$cR01."') AND (NUMPEDIDO = ".$cR02.")";
					$params = array();
					$options = array("scrollable" => SQLSRV_CURSOR_KEYSET);
					// Lanzar la consulta
					$resultado = sqlsrv_query($conexion, $sql);
					if ( $resultado )
					{
						 $something = "Submission successful.";
					}
					else
					{
						 $something = "Submission unsuccessful.";
						 die( print_r( sqlsrv_errors(), true));
					}
					sqlsrv_free_stmt($resultado);
				}
				sqlsrv_close($conexion);
				echo "
				<script languaje='javascript' type='text/javascript'>
					self.close();
				</script>
				";
			}
		?>
	</head>
	<body class="nav-md" onLoad="recargar();">
		<div class="container body">
			<div class="main_container">
				<div class="right_col" role="main">
				<div class="">
					<!-- page content -->
					<div class="clearfix"></div>
					<div class="row">
						<div class="col-md-12 col-sm-12 col-xs-12">	
							<div class="x_panel">
								<div class="x_content">
									<form name="unidades" class="form-vertical" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" onsubmit="return confirmar();">
										<div class="row">
											<div class="form-group">
												<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">Serie <span class="required">*</span>
												</label>
												<div class="col-md-6 col-sm-6 col-xs-12">
													<input type="text" name="cV01" id="first-name" value="<?php echo $_GET['id']; ?>" required="required" class="form-control col-md-7 col-xs-12" readonly>
												</div>
											</div>
											<div class="form-group">
												<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">Pedido <span class="required">*</span>
												</label>
												<div class="col-md-6 col-sm-6 col-xs-12">
													<input type="text" name="cV02" id="first-name" value="<?php echo $_GET['id2']; ?>" required="required" class="form-control col-md-7 col-xs-12" readonly>
												</div>
											</div>
											<div class="form-group">
												<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">Estado actual <span class="required">*</span>
												</label>
												<div class="col-md-6 col-sm-6 col-xs-12">
													<input type="text" name="cV03" id="first-name" value="<?php echo $_GET['id3']; ?>" required="required" class="form-control col-md-7 col-xs-12" readonly>
												</div>
											</div>
											<div class="form-group">
												<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">Nuevo estado <span class="required">*</span>
												</label>
												<div class="col-md-6 col-sm-6 col-xs-12">
													<?php
														echo "
														<select class='form-control' name='cV04'>";
															// 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
															{
																// CONSULTA SQL
																$sql = "SELECT VALOR \n"
																	  ."FROM CAMPOSLIBRESPOSIBLES \n"
																	  ."WHERE (TABLA = 12) AND (CAMPO = 'ESTADO_AD') \n"
																	  ."ORDER BY VALOR DESC";
																$pa1 = array();
																$op1 = array('scrollable' => SQLSRV_CURSOR_KEYSET);
																// Lanzar la consulta
																$re1 = sqlsrv_query($conexion, $sql, $pa1, $op1);
																while($fila = sqlsrv_fetch_array($re1))
																{
																	$cDefualt = '';
																	if ($fila['VALOR'] == 'NINGUNO')
																		$cDefualt = 'selected';
																	echo "
																	<option value = '".$fila['VALOR']."' ".$cDefualt.">".$fila['VALOR']."</option>
																	";
																}
																sqlsrv_free_stmt($re1);
															}
															sqlsrv_close($conexion);
															echo"
														</select>";
													?>
												</div>
											</div>
										</div>
										</br>
										</br>
										</br>
										<div class="row">
											<div class="form-group">
												<button type="submit" class="btn btn-success" name="Guardar">
													<span class="glyphicon glyphicon-floppy-disk" aria-hidden="true"></span> Guardar/Actualizar
												</button>
												<button type="button" class="btn btn-danger" onclick="self.close();">
													<span class="glyphicon glyphicon-remove-circle" aria-hidden="true"></span> Cancelar
												</button>
											</div>
										</div>
									</form>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<?php include '../index/fbody.php'; ?>
	</body>
</html>
<script>function confirmar(){if(confirm('Confirma el cambio de estado?'))return true;	else return false;}</script>