<?php
	include("db/Test.php");
	session_start();
	if(isset($_SESSION["ingreso"]))
	{
			$codp = $_GET["codp"];
			$stmt1 = "SELECT P.cod_producto, P.nom_producto, P.pre_producto, P.img_producto, P.des_producto, 
			P.m_categoria_cod_categoria, C.nom_categoria  FROM  producto P JOIN m_categoria C
			ON(C.cod_categoria = P.m_categoria_cod_categoria)
			WHERE P.cod_producto =$codp";

			$stmt2 = "SELECT cod_categoria, nom_categoria FROM  m_categoria";


			$result1 = mysqli_query($link,$stmt1);
			$result2 = mysqli_query($link,$stmt2);
		?>
		
		<!DOCTYPE html>
		<html>
		<head>
			<title>Productos</title>
			<script type="text/javascript" src="script.js"></script>
			<link rel="stylesheet" type="text/css" href="css/estilo_tabla.css">
			<link rel="stylesheet" type="text/css" href="ghb/gh-buttons.css">
			<meta charset="UTF-8">
			<link rel="shortcut icon" href="img/logo_icon.ico">
		</head>
		<body link="blue" vlink="blue" alink="blue">
			<div id="wrapper_org"> 
          		<a href="Logout.php"><img class="imagen_close" src="img/cerrar.png" alt="Cerrar"></a>
          		<label style="padding-left:680px;"><a href="javascript:history.back(1)" title="Atrás"><img  class="imagen_bandera" src="img/atras.png" title="Atrás"></a></label>
         	</div>

         	<div id="wrapper">

				<form name="frm1" method="POST" action="Actualizar.php">
					<table id="keywords" cellspacing="0" cellpadding="0">
					<tbody>
		<?php
					while($row1 = mysqli_fetch_array($result1)){
						
						/*Fila Código*/
						echo"<tr>";
						echo"<td>Código</td>";
						echo"<td>:</td>";
						echo"<td>". $row1["cod_producto"] ."</td>";
						echo"</tr>";

						/*Fila Producto*/
						echo"<tr>";
						echo"<td>Producto</td>";
						echo"<td>:</td>";
						echo"<td><input type='text' name='txt_nombre' value='". ($row1["nom_producto"]) ."' maxlength='30' size='35' required='required' autocomplete='off'></td>";
						echo"</tr>";	

						/*Fila Precio*/
						echo"<tr>";
						echo"<td>Precio</td>";
						echo"<td>:</td>";
						echo"<td><input type='number' name='txt_precio' value='". number_format($row1["pre_producto"],2) ."' onkeypress='return valida(event)' required='required' max='999'min='0' autocomplete='off'></td>";
						echo"</tr>";

						/*Fila Descripción*/
						echo"<tr>";
						echo"<td>Descripción</td>";
						echo"<td>:</td>";
						echo"<td><input type='text' name='txt_descrip' value='". ($row1["des_producto"]) ."' maxlength='50' size='45' required='required' autocomplete='off'></td>";
						echo"</tr>";

						/*Fila SELECT*/
						echo"<tr>";
						echo"<td>Categoría</td>";
						echo"<td>:</td>";
						echo"<td>";

						?>
						<select name="nuevo_select">
						<?php 

						while($row_select = mysqli_fetch_array($result2))
						{ 
							if($row1["m_categoria_cod_categoria"] == $row_select['cod_categoria'])
							{
						?>

							 	 <option value="<?php echo $row_select['cod_categoria'] ?>" selected="selected">
							 	 <?php echo $row_select['nom_categoria'] ?>
							  	 </option>

						<?php
							}
							else{
						?>
								<option value="<?php echo $row_select['cod_categoria'] ?>">
							 	<?php echo $row_select['nom_categoria'] ?>
							  	</option>

						<?php
							} /*Fin ELSE*/

						}/*Fin WHILE*/
						?>
						</select>
						<?php
						echo"</td>";
						echo"</tr>";


						/*Fila Imagen actual*/
						echo"<tr>";
						echo"<td>Imagen</td>";
						echo"<td>:</td>";
						echo"<td>".$row1["img_producto"]."</td>";
						echo"</tr>";


						/*Fila Imagen Nueva*/
						echo"<tr>";
						echo"<td></td>";
						echo"<td></td>";
						echo"<td><input type='file' name='img_new'></td>";
						echo"</tr>";




						/*Fila CAMPOS OCULTOS*/
						echo"<tr>";
						echo"<input type='hidden' name='hcode_producto' value='". $row1["cod_producto"]."' autocomplete='off'>";
						echo"<input type='hidden' name='hcode_categoria' value='". $row1["m_categoria_cod_categoria"]."' autocomplete='off'>";
						echo"</tr>";
					}
		?>
					</tbody>
					</table>
					<br><br>
					<center><input type="submit" name="btnenviar" value="Actualizar" class="button primary">&nbsp;&nbsp;&nbsp;
					<input type="button" name="btncancelar" value="Cancelar" onclick="javascript:history.back(1)" class="button danger">
					<br><br>
					</center>
				</form>

		<script src="//static.codepen.io/assets/common/stopExecutionOnTimeout-b2a7b3fe212eaa732349046d8416e00a9dec26eb7fd347590fbced3ab38af52e.js"></script>
      	<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
      	<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.28.14/js/jquery.tablesorter.min.js"></script>
		</body>
		</html>
		<?php
		}else{
			header("Location:index.html");
		}
?>