<?php
session_start();

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

include('../db.php');

// esta es la matriz de tipos de campos para las caracter’sticas
$tipos = array("", "Valor", "Bool", "Checkbox", "Radio", "Menu", "Radio + valor", "Check + valor");
?>
<html>
<head>
<title>categorias</title>
<link href="../sformi.css" rel="stylesheet" type="text/css">
<script language="JavaScript" type="text/JavaScript">
function doSubmit() { 
	document.formulario.submit();
}
</script>
</head>
<body>
<?php 

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

if(isset($_REQUEST['op'])) {
	$operacion = $_REQUEST['op'];
	$cat = $_REQUEST['cat'];
	$seccion = $_REQUEST['seccion'];
} else {
	$operacion = "add";
}

$conn = db_connect();

switch($operacion)
{
	case "add";
		$cat_orden = "";
		$cat_codigo = "";

		if($conn) {
			$query = 'SELECT cat_orden FROM shoop_categorias WHERE cat_seccion = "' . $seccion . '" ORDER BY cat_orden DESC LIMIT 1 ';
			$result = mysql_query($query, $conn);
			if (mysql_num_rows($result)) {
				$row = mysql_fetch_array($result);
				$cat_orden = $row['cat_orden'] + 1;
				$cat_codigo = $seccion . substr($cat_orden + 100, 1, 2);
			}
		}
		$cat_nombre = "";
		$cat_descripcion = "";
		break;

	case "edit";
		if($conn) {
			$query = 'SELECT cat_orden, cat_nombre, cat_descripcion, cat_codigo FROM shoop_categorias WHERE cat_codigo = "' . $cat . '"';
			$result = mysql_query($query, $conn);
			if (mysql_num_rows($result)) {
				$row = mysql_fetch_array($result);
				$cat_orden = $row['cat_orden'];
				$cat_nombre = $row['cat_nombre'];
				$cat_descripcion = $row['cat_descripcion'];
				$cat_codigo = $row['cat_codigo'];
			} else {
				$cat_orden = "";
				$cat_nombre = "";
				$cat_descripcion = "";
				$cat_codigo = "";
			}
		}
		break;
}
?>
<form method="post" action="categorias_db.php" name="formulario">
<input name="op" type="hidden" value="<?php echo $operacion; ?>">
<input name="categoria" type="hidden" value="<?php echo $cat; ?>">
<input name="seccion" type="hidden" value="<?php echo $seccion; ?>">
<table border="0" cellspacing="0" cellpadding="0">
<tr><td width="400"><span class= "lbl">Secci&oacute;n:&nbsp;</span><span class="data"><?php echo $seccion; ?></td></tr>
	<tr><td><img src="../_imgs/d.gif" width="100" height="3"></td></tr>
<tr><td width="200" class= "lbl">C&oacute;digo</td></tr>
	<tr><td width="" class="data"><input name="cat_codigo" type="text" size="5" maxlength="5" value="<?php echo $cat_codigo; ?>"  class="data"></td></tr>
	<tr><td><img src="../_imgs/d.gif" width="100" height="3"></td></tr>
<tr><td width="200" class= "lbl">Orden</td></tr>
	<tr><td width="" class="data"><input name="cat_orden" type="text" size="2" maxlength="2" value="<?php echo $cat_orden; ?>"  class="data"></td></tr>
	<tr><td><img src="../_imgs/d.gif" width="100" height="3"></td></tr>
<tr><td width="200" class= "lbl">Nombre</td></tr>
	<tr><td width="" class="data"><input name="cat_nombre" type="text" size="32" maxlength="32" value="<?php echo $cat_nombre; ?>" class="data"></td></tr>
	<tr><td><img src="../_imgs/d.gif" width="100" height="3"></td></tr>
<tr><td width="200" class= "lbl">Descripci&oacute;n</td></tr>
	<tr><td width="" class="data"><textarea name="cat_descripcion" cols="80" rows="2" class="data"><?php echo $cat_descripcion; ?></textarea></td></tr>
	<tr><td><img src="../_imgs/d.gif" width="100" height="3"></td></tr>
</table>

<table border="0" cellspacing="0" cellpadding="0">
	<tr>
	  <td width="300">
		<a href="javascript:history.go(-1)" class="button">[ volver atr&aacute;s ]</a>&nbsp;&nbsp;
		<a href="javascript:doSubmit();" class="button">[ grabar ]</a>
	</td>
		<td>
<?php
	if($operacion == 'edit') {
?>
		<a href="categorias_db.php?op=del&id=<?= $cat; ?>" onClick="return confirm('Est&aacute;s seguro de querer borrar esta categor&iacute;a?')" class="button">[ borrar ]</a></td>
<?php
	}
?>
	</tr>
</table>



</form>

<script language="JavaScript" type="text/JavaScript">
	document.formulario.cat_nombre.focus();
</script>

<?php
	if($operacion == "edit") {
?>

<span class="lbl">Caracter&iacute;sticas para esta categor&iacute;a&nbsp;&nbsp;</span><!-- <a href="cate_caracteristicas_edit.php?op=add" class="button">[ a&ntilde;adir una nueva caracter&iacute;stica ]</a> -->

<?php
		if($conn) {
			$query = 'SELECT catop_id, catop_tipo, catop_label, catop_values, catop_orden FROM shoop_cate_caracteristicas WHERE catop_categoria="' . $cat . '"';
			$result = mysql_query($query, $conn);
			$num_results = mysql_num_rows($result);
?>
<table border="0" cellspacing="0" cellpadding="0">
	<tr>
		<td width="90" valign="top" class="hdr">Tipo</td><td width="150" valign="top" class="hdr">Label</td><td width="300" valign="top" class="hdr">Valores</td><td width="20" valign="top" class="hdr">Orden</td><td class="hdr">&nbsp;</td></tr>

<?php
			for ($i=0; $i <$num_results; $i++)
			{
			$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'];
			$catop_orden = $row['catop_orden'];
?>
	<tr>
		<td width="90" valign="top" class="data"><?php echo $tipos[$catop_tipo]; ?></td>
		<td width="150" valign="top" class="data"><?php echo $catop_label; ?></td>
		<td width="300" valign="top" class="data"><?php echo $catop_values; ?></td>
		<td width="20" valign="top" class="data"><?php echo $catop_orden; ?></td>
<!--		<td valign="top" class="data">&nbsp;<a href="cate_caracteristicas_edit.php?op=edit&id=<?php echo $catop_id; ?>" class="button">[ editar ]</a>&nbsp;&nbsp;<a href="cate_caracteristicas_db.php?op=del&id=<?php echo $catop_id; ?>" class="button">[ borrar ]</a></td> -->
	</tr>
	<tr>
		<td bgcolor="#DBDEE6" colspan=5><img src="../_imgs/d.gif" width="5" height="1"></td>
	</tr>
<?php
			}	// FOR
		}
?>
	</table>
<?php
	}
?>
</body>
</html>