<?php
session_start();

if (!session_is_registered("clave_audio")) {
	include("../login.php");
	if (!$continuar) exit;
}

include('../db.php');
include("../../_incl/code.php");
?>
<html>
<head>
<title>productos</title>
<link href="../sformi.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="../../_incl/code.js"></script>
<script language="JavaScript" type="text/JavaScript">
function doSubmit() { document.formulario.submit(); }
</script>
</head>
<body>
<?php 
if(isset($_REQUEST['op'])) {
	$operacion = $_REQUEST['op'];
	$prod_id = $_REQUEST['id'];
} else {
	$operacion = "add";
}
?>
<?php
$prod_categoria_id = "";
$prod_fab_id = "";
$prod_modelo = "";
$prod_precio = "";
$prod_colores = "";
$prod_peso = "";
$prod_dimensiones = "";
$prod_volumen = "";
$prod_descripcion = "";
$prod_intro = "";

$conn = db_connect();

if($operacion == 'edit') {
	if($conn) {
		$query = "SELECT prod_id, prod_categoria_id, prod_fab_id, prod_modelo, prod_precio, prod_colores, prod_peso, prod_codigo, prod_dimensiones, prod_volumen, prod_descripcion, prod_intro, prod_disponibilidad
					FROM shoop_productos 
					WHERE prod_id = " . $prod_id;
		$result = mysql_query($query, $conn);
		if (mysql_num_rows($result)) {
			$row = mysql_fetch_array($result);
			$prod_categoria_id = $row['prod_categoria_id'];
			$prod_fab_id = $row['prod_fab_id'];
			$prod_modelo = stripslashes($row['prod_modelo']);
			$prod_precio = $row['prod_precio'];
			$prod_colores = stripslashes($row['prod_colores']);
			$prod_peso = $row['prod_peso'];
			$prod_dimensiones = $row['prod_dimensiones'];
			$prod_volumen = $row['prod_volumen'];
			$prod_descripcion = stripslashes($row['prod_descripcion']);
			$prod_intro = stripslashes($row['prod_intro']);
			$prod_disponibilidad = $row['prod_disponibilidad'];
		}
		$query = "SELECT oferta_tipo FROM shoop_prod_ofertas WHERE oferta_prod_id = " . $prod_id;
		$result = mysql_query($query, $conn);
		if (mysql_num_rows($result)) {
			$resaltado = mysql_fetch_array($result);
			$oferta_tipo = $resaltado['oferta_tipo'];
		} else {
			$oferta_tipo = 0;
		}
	}
}

$max_size = 1024 * 1536; // the max. size for uploading
define("MAX_SIZE", $max_size);
?>
<form method="post" action="productos_db.php" name="formulario" enctype="multipart/form-data">
<input name="op" type="hidden" value="<?php echo $operacion; ?>">
<input name="id" type="hidden" value="<?php echo $prod_id; ?>">
<table border="0" cellspacing="0" cellpadding="0" width="100%">
	<tr><td class="lbl12">Detalles de este producto</td></tr>
	<tr><td bgcolor="#777777"><img src="../../_imgs/d.gif" width="100" height="1"></td></tr>
</table>
<table border="0" cellspacing="0" cellpadding="0">
		<tr>
      		<td class= "lbl">Categor&iacute;a</td>
      		<td class= "lbl">Fabricante</td>
      		<td class= "lbl">&nbsp;Imagen</td>
		</tr>
      	<tr>
      		<td><select name="prod_categoria_id"  id="prod_categoria_id" style="width:200px"  class="data">
      				<option value="" selected>Categor&iacute;as</option>
						<?php include("../../categorias2.htm"); ?>
				</select>
				<script language="JavaScript" type="text/JavaScript"> selectOption('prod_categoria_id', '<?php echo $prod_categoria_id; ?>'); </script></td>
			<td><select name="prod_fab_id"  id="prod_fab_id" style="width:200px" class="data">
					<option value="" selected>Marcas</option>
    					<?php include("menu_marcas.php"); ?>
					</select>
				<script language="JavaScript" type="text/JavaScript"> selectOption('prod_fab_id', '<?php echo $prod_fab_id; ?>'); </script></td>
			<td rowspan="16" valign="top">
<table width="250" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="3"><img src="../_imgs/d.gif" width="3" height="1"></td>
    <td bgcolor="#999999" width="1"><img src="../_imgs/d.gif" width="1" height="1"></td>
    <td width="2"><img src="../_imgs/d.gif" width="2" height="1"></td>
    <td><input name="prod_imagen" type="file" style="width:250px" class="data"></td>
  </tr>
  <tr>
    <td><img src="../_imgs/d.gif" width="3" height="1"></td>
    <td bgcolor="#999999"><img src="../_imgs/d.gif" width="1" height="1"></td>
    <td><img src="../_imgs/d.gif" width="2" height="1"></td>
    <td>
<?php 
	if(file_exists('../../_prds/' . $prod_id . '_247.jpg')) {
?>
	<table width="247" border="1" cellspacing="0" cellpadding="0" bordercolor="#333333" style="margin-top:10px; border-style:solid; border-color:#444444; border:1px; border-padding:1px;">
  <tr>
    <td align="center"><img src="../../_prds/<?php echo $prod_id; ?>_247.jpg"></td>
  </tr>
</table>
<?php
	}
?>
</td>
  </tr>
  <tr>
    <td><img src="../_imgs/d.gif" width="3" height="1"></td>
    <td bgcolor="#999999"><img src="../_imgs/d.gif" width="1" height="1"></td>
    <td><img src="../_imgs/d.gif" width="2" height="1"></td>
    <td>
<?php 
	if(file_exists('../../_prds/' . $prod_id . '_168.jpg')) {
?>
<table width="168" border="1" cellspacing="0" cellpadding="0" bordercolor="#333333" style="margin-top:10px; border-style:solid; border-color:#444444; border:1px; border-padding:1px;">
  <tr>
    <td align="center"><img src="../../_prds/<?php echo $prod_id; ?>_168.jpg"></td>
  </tr>
</table>
<?php
	}
?>
</td>
  </tr>
</table>
</td></tr>
		<tr><td colspan="2"><img src="../../_imgs/d.gif" width="100" height="2"></td></tr>
		<tr>
			<td colspan="2" class= "lbl">Modelo</td></tr>
		<tr>
			<td colspan="2"><input name="prod_modelo" type="text" size="60" maxlength="60" value="<?php echo $prod_modelo; ?>" class="data"></td></tr>
		<tr>
			<td colspan="2"><img src="../../_imgs/d.gif" width="100" height="2"></td></tr>
		<tr>
			<td colspan="2" class= "lbl">Resaltado</td></tr>
		<tr>
		  <td colspan="2">
		    <table class="data">
			    <tr>
			      <td><label><input type="radio" name="producto_resaltado" value="0" <?php echo ($oferta_tipo == 0 ? ' checked' : ''); ?>>No</label>&nbsp;&nbsp;</td>
			      <td><label><input type="radio" name="producto_resaltado" value="1" <?php echo ($oferta_tipo == 1 ? ' checked' : ''); ?>>Compacto</label>&nbsp;&nbsp;</td>
			      <td><label><input type="radio" name="producto_resaltado" value="2" <?php echo ($oferta_tipo == 2 ? ' checked' : ''); ?>>Principal</label></td></tr>
		    </table></td></tr>
		<tr>
			<td colspan="2"><img src="../../_imgs/d.gif" width="100" height="2"></td></tr>
		<tr>
			<td colspan="2" class= "lbl">Precio<span class="data"> (-1 significa CONSULTAR)</span></td></tr>
		<tr>
			<td colspan="2"><input name="prod_precio" type="text" size="10" maxlength="10" value="<?php echo $prod_precio; ?>" class="data"><span class="data">(sin comas, con decimales separados por punto)</span></td></tr>
		<tr>
			<td colspan="2" class= "lbl">Colores/Acabados</td></tr>
		<tr>
        	<td colspan="2"><input name="prod_colores" type="text" size="60" maxlength="150" value="<?php echo $prod_colores; ?>" class="data"></td></tr>
		<tr>
			<td colspan="2"><img src="../../_imgs/d.gif" width="100" height="2"></td></tr>
		<tr>
			<td colspan="2" class="lbl"><table border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td class="lbl">Dimensiones en cm <span class="data">(an x al x fd)&nbsp;&nbsp;&nbsp;</span> </td>
                <td class="lbl">Peso<span class="data"> (Kg.)&nbsp;&nbsp;&nbsp;</span></td>
                <td class="lbl">Volumen<span class="data"> (m<sup>3</sup>)</span></td></tr>
              <tr>
                <td><input name="prod_dimensiones" type="text" class="data" id="prod_dimensiones" value="<?php echo $prod_dimensiones; ?>" size=""></td>
                <td><input name="prod_peso" type="text" size="4" maxlength="4" value="<?php echo $prod_peso; ?>" class="data"></td>
                <td><input name="prod_volumen" type="text" class="data" id="prod_volumen" value="<?php echo $prod_volumen; ?>" size="4" maxlength="4"></td></tr>
            </table>
		    </td></tr>
		<tr>
			<td colspan="2"><img src="../../_imgs/d.gif" width="100" height="2"></td></tr>
		<tr>
			<td colspan="2" class= "lbl">Introducci&oacute;n</td></tr>
		<tr>
        	<td colspan="2"><textarea name="prod_descripcion" cols="58" rows="3" class="data"><?php echo $prod_descripcion; ?></textarea></td></tr>
		<tr>
			<td colspan="2"><img src="../../_imgs/d.gif" width="100" height="4"></td></tr>
		<tr>
			<td colspan="2" class= "lbl">Descripci&oacute;n</td></tr>
		<tr>
        	<td colspan="2"><textarea name="prod_intro" cols="58" rows="8" class="data"><?php echo $prod_intro; ?></textarea></td></tr>
		<tr>
			<td colspan="2"><img src="../../_imgs/d.gif" width="100" height="4"></td></tr>
</table>
<?php
if($operacion=='edit'){
?>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
	<tr><td class="lbl12">Caracter&iacute;sticas espec&iacute;ficas a la categor&iacute;a</td></tr>
	<tr><td bgcolor="#777777"><img src="../../_imgs/d.gif" width="100" height="1"></td></tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php
if($conn) {
	$query = 'SELECT procat_catop, procat_valor FROM shoop_prod_caracteristicas WHERE procat_producto=' . $prod_id ;
	$result = mysql_query($query, $conn);
	if($result){
		$datos = array();
		while($row = mysql_fetch_array($result)) {
			// relleno una matriz con los valores que tengo grabados para este producto
			$dato_tipo = $row['procat_catop'];
			$dato_valor = $row['procat_valor'];
			$datos[$dato_tipo] = $dato_valor;
		}
	}

	// en esta segunda lectura leo el interface
	$i = 0;
	$query = 'SELECT catop_id, catop_tipo, catop_label, catop_values FROM shoop_cate_caracteristicas WHERE catop_categoria="' . $prod_categoria_id . '" ORDER BY catop_orden';
	$result = mysql_query($query, $conn);
	while($row = mysql_fetch_array($result)) {
		$catop_id = $row['catop_id'];
		$catop_tipo = $row['catop_tipo'];
		$catop_label = $row['catop_label'];
		$catop_values = $row['catop_values'];
?>
	<tr>
		<td width="150" valign="top"><img src="../_imgs/d.gif" width="150" height="1"></td>
		<td></td></tr>
	<tr>
		<td width="150" class="lbr"><?php echo $catop_label; ?>:</td>
		<td width="80%">
		<input type="hidden" name="campo<?php echo $i; ?>" value="<?php echo $catop_id; ?>">
<?php
		if(strlen($catop_values)){
			$etiquetas = explode(', ', $catop_values);
		}
		switch($catop_tipo){
			case 1:		// value
?>
			<input type="text" name="valor_<?php echo $catop_id; ?>" size="40" maxlength="60" class="data" value="<?php echo (strlen($datos[$catop_id]) ? $datos[$catop_id] : '') ?>">
<?php
				break;
			case 2:		// bool
?>
			<input type="checkbox" name="valor_<?php echo $catop_id; ?>" value="S&iacute;" <?php echo (strlen($datos[$catop_id]) ? ' checked ' : '') ?>>
<?php
				break;
			case 3:		// check
				echo array2Control($etiquetas, 'valor_' . $catop_id, 'data', 'checkbox', $datos[$catop_id]);
				break;
			case 4:		// radio
				echo array2Control($etiquetas, 'valor_' . $catop_id, 'data', 'radio', $datos[$catop_id]);
				break;
			case 5:		// menu
?>
			<select name="valor_<?php echo $catop_id; ?>" class="data">
				<option value=""></option>
<?php
				echo array2Select($etiquetas, $datos[$catop_id]);
?>
			</select>
<?php
				break;
			case 6:
				break;
			case 7:
				break;
		}		// switch
?>			
		</td></tr>
	<tr>
		<td width="150"></td>
		<td bgcolor="#666666"><img src="../_imgs/d.gif" width="150" height="1"></td></tr>
	<tr>
<?php
		$i++;
	}	// for
?>
	<input type="hidden" name="contador" value="<?php echo $i; ?>">
<?php
}	 // if($conn
?>
</table>
<?php
}	// if('edit'
?>

<table border="0" cellspacing="0" cellpadding="0">
	<tr>
	  <td width="300">
		<a href="javascript:history.go(-1)" class="button">[ cancelar ]</a>&nbsp;&nbsp;
		<a href="javascript:doSubmit();" class="button">[ Ok ]</a>
	</td>
		<td>
<?php
	if($operacion == 'edit') {
?>
		<a href="productos_db.php?op=del&id=<?php echo $prod_id; ?>" onclick="return confirm('Est&aacute;s seguro de querer borrar este art&iacute;culo?')" class="button">[ borrar ]</a></td>
<?php
	}
?>
	</tr>
</table>
</form>
</body>
</html>