<div id="center-column">
<div class="top-bar">
				<a href="index.php?act=uploadgames" title="Upload Games" class="button">Añadir juego</a>
				<h1>Cpanel - Categorias</h1>
				<div class="breadcrumbs"><a href="index.php?act=general" title="Settings">Configuracion </a> / <a href="index.php?act=links">Enlaces</a></div>
			</div><br />
		  <div class="select-bar">
		    <label>
		    <h3>Administrar categorias</h3>
		    </label>
		  </div>
<?php
if(!empty($_GET['edit'])) {
	$query = yasDB_select("SELECT * FROM categories WHERE id = '{$_GET['edit']}'",false);
	if($query->num_rows == 0) {
		echo '<center>No se puede editar una categoría que no existe.</center>';
	} else {
		$row = $query->fetch_array(MYSQLI_ASSOC);
		$query->close();
		
		?>
		<div class="table">
		    <img src="img/bg-th-left.gif" width="8" height="7" alt="" class="left" />
			<img src="img/bg-th-right.gif" width="7" height="7" alt="" class="right" />
			<form name="update" method="get" action="index.php">
			<input type="hidden" name="act" value="categories"/>
			<table class="listing form" cellpadding="0" cellspacing="0">
	        <tr>
	        <th class="full" colspan="2">Editar - Categoria</th>
			</tr>
			<tr>
			<td class="first" width="172"><strong>Nombre</strong></td>
			<td class="last">
			<?php if ($row['id'] > 10) { ?>
				<input type="text" name="name" value="<?php echo $row['name'];?>"  /></td>
			<?php } else {
				?> <label style="font-weight:700;"><?php echo $row['name'];?></label>
			<?php } ?>
			</tr>
			<tr class="bg">
			<td class="first"><strong>Activa</strong></td>
			<td class="last">Activo:<br/>
		<select name="active">
        <option value ="<?php echo $row['active'];?>" /><?php echo $row['active'];?></option>
        <option value ="si">si</option>
        <option value ="no">no</option>
        </select></td>
		    </tr>
			<tr>
			<td class="first"><strong>Mostrar Orden</strong></td>
			<td class="last"><input type="text" name="order" size=3 value="<?php echo $row['order'];?>"  /></td>
			</tr>
			<tr class="bg">
			<td class="first"><strong>categoria Superior</strong></td>
			<td class="last">
			<select name="parent">
            <option value ="<?php echo $row['parent'];?>" /><?php echo $row['parent'];?></option>
            <option value ="si">si</option>
            <option value ="no">no</option>
            </select>
			</td>
			</tr>
            <tr>
			<td class="first"><strong>Mostrar en tu pagina de inicio</strong></td>
			<td class="last"><select name="home">
        <option value ="<?php echo $row['home'];?>" /><?php echo $row['home'];?></option>
        <option value ="si">si</option>
        <option value ="no">no</option>
        </select></td>
			</tr>
			<tr class="bg">
			<td class="first"><strong>Si no es de la matriz, la identificacion de la categoria principal</strong></td>
			<td class="last"><input type="text" name="pid" size=3 value="<?php echo $row['pid'];?>"  /></td>
			</tr>
			<tr>
			<td class="first"><strong>Meta descripcion</strong></td>
			<td class="last"><input type="text" name="desc" value="<?php echo $row['desc'];?>" size="40" /></td>
			</tr> 
			<tr class="bg">
			<td class="first"></td>
			<td class="last"><input type="hidden" name="id" value="<?php echo $row['id'];?>" /><input type="submit" class="button" name="check" value="Editar!" />
		<input type="reset" class="button" value="Reiniciar" /></td>
			</tr>
		</table>
			</div>
			</form>
	<?php		
	}	
} elseif(isset($_GET['check'])) {
	if(empty($_GET['name'])) {
		echo '<center>Uno o mas campos se quedo vacio.<br />';
		echo '<a href="index.php?act=categories&edit=' . $_GET['id'] . '">Haga clic aqui para volver</a></center>';
	} else {
		yasDB_update("UPDATE categories SET name = '{$_GET['name']}', active = '{$_GET['active']}', `order` = '{$_GET['order']}', `parent` = '{$_GET['parent']}', `home` = '{$_GET['home']}', `desc` = '{$_GET['desc']}', `pid` = '{$_GET['pid']}' where id = '{$_GET['id']}'",false);
		echo '<center>Categoria editada con exito!<br/><br/>';
		echo '<a href="index.php?act=categories">Haga clic aqui para continuar</a></center>';
	}
} elseif(!empty($_GET['delete'])) {
	$query = yasDB_select("SELECT id FROM categories WHERE id = '{$_GET['delete']}'",false);
	if($query->num_rows == 0) {
		echo '<center>No se puede eliminar una categoria que no existe!<br />';
		echo '<a href="index.php?act=categories">Haga clic aqui para volver</a></center>';
		$query->close();
		
	} else {
		$query->close();		
		yasDB_delete("DELETE FROM categories WHERE id = '{$_GET['delete']}'");
		echo '<center>La categoria se ha eliminado correctamente.<br />';
		echo '<a href="index.php?act=categories">Haga clic aqui para continuar</a></center>';
	}
} elseif(isset($_POST['deletechecked'])) {
    $count = count($_POST['checkbox']);
    ?>
    <script type = "text/javascript">
        var response = confirm("¿Esta seguro que desea eliminar<?php echo $count;?> categoria(s)?");
        if (!response) {
            window.location.href = '<?php echo $setting['siteurl'];?>admin/index.php?act=categories&remove=notta';
            
        }
    </script>
    <?php
    foreach ($_POST['checkbox'] as $box) {
        $query = yasDB_select("SELECT id FROM categories WHERE id = '$box'",false);
        if($query->num_rows == 0) {
            echo '<center>You cannot delete this categorie that does not exist!<br />';
            echo '<a href="index.php?act=categories">Click here to go back</a></center>';
        } else {
            if ($_GET['remove'] != 'notta') {
                $row = $query->fetch_array(MYSQLI_ASSOC);
                $query->close();            
                yasDB_delete("DELETE FROM categories WHERE id = '$box'",false);
                ?>
                <meta http-equiv="refresh" content="0;URL=<?php echo $setting['siteurl'];?>admin/index.php?act=categories" />
                <?php
            }
        } 
	}
} elseif(isset($_POST['add'])) {
	$query = yasDB_select("SELECT id FROM `categories` where name = '{$_POST['name']}'",false);
	if($query->num_rows != 0) {
		$query->close();		
		echo '<center>Lo sentimos, esa categoria ya existe.<br/>';
		echo '<a href="index.php?act=categories">Haga clic aqui para volver</a></center>';
	} else {
		$query->close();		
		$query = yasDB_select("SELECT `order`*1 AS `neworder` FROM `categories` ORDER BY `neworder` DESC LIMIT 1");
		$order = $query->fetch_array(MYSQLI_NUM);
		$neworder = $order[0];
		$neworder++;
		yasDB_insert("INSERT INTO categories (`id`,`name`,`active`,`order`) VALUES ('', '{$_POST['name']}', 'yes', '$neworder')");
		echo '<center>Categoria agregado con exito!<br />';
		echo '<a href="index.php?act=categories">Haga clic aqui para continuar!</a></center>';
	}
} else { 
if (isset($_GET['page'])) {
		$pageno = $_GET['page'];
	} else {
		$pageno = 1;
	} 
	$result = yasDB_select("SELECT count(id) FROM categories",false);
	$query_data = $result->fetch_array(MYSQLI_NUM);
	$numrows = $query_data[0];
	$result->close();
	
	$rows_per_page = 15;
	$lastpage = ceil($numrows/$rows_per_page);
	$pageno = (int)$pageno;
	if ($pageno > $lastpage) {
	   $pageno = $lastpage;
	} 
	if ($pageno < 1) {
	   $pageno = 1;
	} 
	$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
	$query = yasDB_select("select * from categories $limit",false);
	if($query->num_rows == 0) {
		echo '<center>No categories in the database!</center>';
	} else {?>
	<br/><center>
	<form name="add" method="post" action="">
	Nombre: <input type="text" name="name" /> <input type="submit" class="button" value="Añadir Categoria" name="add">
	</form></center><br />
	
	<div class="table">
		    <img src="img/bg-th-left.gif" width="8" height="7" alt="" class="left" />
			<img src="img/bg-th-right.gif" width="7" height="7" alt="" class="right" />
			<table class="listing" cellpadding="0" cellspacing="0">
			<tr>
						<th class="first" width="30px">ID</th>
						<th>Categoria</th>
						<th width="40px">Activa</th>
						<th width="30px">Orden</th>
						<th>matriz</th>
						<th>Sub catigoria (id)</th>
						<th>Mostrar en tu pagina de inicio</th>
						<th width="100px">Meta descripcion</th>
						<th width="40px"></th>
						<th class="last" width="50px"></th>
					</tr>
	<?php
	while($row = $query->fetch_array(MYSQLI_ASSOC)) {
		?>
		<tr><form name="deleteform" method="post" action="">
		<td class="first style1" style="height:45px;"><?php echo $row['id'];?></td>
		<td style="overflow:hidden;height:45px;"><?php echo $row['name'];?></td>
		<td style="height:45px;"><?php echo $row['active'];?></td>
		<td style="height:45px;"><?php echo $row['order'];?></td>
		<td style="height:45px;"><?php echo $row['parent'];?></td>
		<td style="height:45px;"><?php echo $row['pid'];?></td>
		<td style="height:45px;"><?php echo $row['home'];?></td>
		<td style="overflow:hidden;height:45px;"><?php echo $row['desc'];?></td>
		<td style="height:45px;"><a href="index.php?act=categories&edit=<?php echo $row['id'];?>"><font color="#cc0000">Editar</font></a></td>
		<td class="last" style="height:45px;">
		<?php
		if ($row['id'] > 10) { ?>
		<a href="index.php?act=categories&delete=<?php echo $row['id'];?>" onclick="return confirm(\'Are you sure you want to delete this category?\')"><font color="#cc0000">Eliminar</font></a><br/><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['id'];?>">
		<?php } ?>
		</td>
		</tr>
		
	<?php
	} 
	$query->close();
	
	?>
	</table>
	</div>
	<div style="text-align:right;padding-right:20px;padding-top:10px;margin-bottom:4px;">
        <input name="deletechecked" type="submit" class="button" id="deletechecked" value="Chequear la eliminacion">
        </form>
    </div>
	<br /><div style="text-align:center;padding-bottom:10px;">
		<?php
		if ($pageno == 1) {
			echo ' PRIMERA ANTERIOR ';
		} else {
			echo ' <a href="index.php?act=categories&page=1">PRIMERA</a> ';
			$prevpage = $pageno-1;
			echo ' <a href="index.php?act=categories&page=' . $prevpage . '">ANTERIOR</a> ';
		} 
		echo ' ( Pagina ' . $pageno . ' de ' . $lastpage . ' ) ';
		if ($pageno == $lastpage) {
			echo ' SIGUIENTE ULTIMA ';
		} else {
			$nextpage = $pageno+1;
			echo ' <a href="index.php?act=categories&page=' . $nextpage . '">SIGUIENTE</a> ';
			echo ' <a href="index.php?act=categories&page=' . $lastpage . '">ULTIMA</a> ';
		} ?> 
	</div>
<?php	
}
}
?>
</div>