<div id="center-column">
<div class="top-bar">
				<a href="index.php?act=uploadgames" title="Upload Games" class="button">AGREGAR JUEGO</a>
				<h1>Cpanel - Juegos para descargar</h1>
				<div class="breadcrumbs"><a href="index.php?act=addmedia" title="Ad Media">Agregar Media</a> / <a href="index.php?act=addcode" title="Ad Code">Agregar Codigo</a> / <a href="index.php?act=uploadgames" title="Upload Games">Subir juegos</a> / <a href="index.php?act=brokenfiles" title="Broken Files">Archivos en mal estado</a></div>
			</div><br />
		  <div class="select-bar">
		    <label>
		    <h3>Administrar Juegos descargables</h3>
		    </label>
		  </div>
<?php
if(!empty($_GET['edit'])) {

	$query = yasDB_select("SELECT * FROM downgames WHERE id = '{$_GET['edit']}'",false);
	if($query->num_rows == 0) {
		echo '<center>No se puede editar un juego que no existe.</center>';
	} else {
		$row = $query->fetch_array(MYSQLI_ASSOC);?>		
	<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="edit" method="post" action="index.php?act=managedowngame">
			<table class="listing form" cellpadding="0" cellspacing="0">
	        <tr>
	        <th class="full" colspan="2">Editar - <?php echo $row['title'];?></th>
			</tr>
			<tr>
			<td class="first" width="172"><strong>Titulo</strong></td>
			<td class="last"><input type="text" name="title" value="<?php echo $row['title'];?>"  /></td>
			</tr>
			<tr class="bg">
			<td class="first"><strong>Descripcion</strong></td>
			<td class="last"><textarea name="description" style="width: 300px; height: 100px;"/><?php echo $row['description'];?></textarea></td>
		    </tr>
			<tr>
			<td class="first"><strong>Enviar</strong></td>
			<td class="last"><input type="hidden" name="id" value="<?php echo $row['id'];?>" />
			<input type="hidden" name="m" value="<?php echo $_GET['m'];?>"/>
			<input type="submit" class=button" name="edit" value="Editar!" />
			</td>
			</tr>
			</table>
			</div>
			</form>
			<?php	
    }	
	$query->close();
	
} elseif(isset($_POST['edit'])) {
	if(empty($_POST['title'])) {
		echo '<center>Uno o mas campos se quedo vacio.<br />';
		echo '<a href="index.php?act=managedowngame&edit=' . $_POST['id'] . '">Haga clic aqui para volver</a></center>';
	} else {
		yasDB_update("UPDATE downgames SET title = '{$_POST['title']}', description = '{$_POST['description']}' where id = '{$_POST['id']}'",false);
		echo '<p align="center">Archivo correctamente editado!<br />';
	 if (!empty($_POST['m'])) {
			echo '<center><a href="index.php?act=managedowngame">Haga clic aqui para continuar</a></p></center>';
		}
	}
} elseif(!empty($_GET['delete'])) {
	$query = yasDB_select("SELECT id, file, thumbnail FROM downgames WHERE id = '{$_GET['delete']}'",false);
	if($query->num_rows == 0) {
		echo '<center>You cannot delete a game that does not exist!<br /><a href="index.php?act=managedowngame">Click here to go back</a></center>';
	} else {
		$row = $query->fetch_array(MYSQLI_ASSOC);
		@unlink($row['file']);
		@unlink($row['thumbnail']);
		$query->close();
		
		yasDB_delete("delete from downgames where id = '{$_GET['delete']}'",false);
		echo '<p style="text-align:center;">Juego exitosamente eliminado de su seccion de Descarga.<br />';
		echo '<a href="index.php?act=managedowngame">Haga clic aqui para continuar</a></p>';
	}
} elseif(isset($_POST['deletechecked'])) {
    $count = count($_POST['checkbox']);
    ?>
    <script type = "text/javascript">
        var response = confirm("Esta seguro que desea eliminar <?php echo $count;?> juego(s)?");
        if (!response) {
            window.location.href = '<?php echo $setting['siteurl'];?>admin/index.php?act=managedowngame&remove=notta';
            
        }
    </script>
    <?php
    foreach ($_POST['checkbox'] as $box) {
        $query = yasDB_select("SELECT id, file, thumbnail FROM downgames WHERE id = '$box'",false);
        if($query->num_rows == 0) {
            echo '<center>No se puede eliminar un juego que no existe!<br />';
            echo '<a href="index.php?act=managedowngame">Haga clic aqui para volver</a></center>';
        } else {
            if ($_GET['remove'] != 'notta') {
                $row = $query->fetch_array(MYSQLI_ASSOC);
                @unlink('../ourgames/img/' . $row['file']);
                @unlink('../ourgames/games/' . $row['thumbnail']);
                $query->close();            
                yasDB_delete("delete from downgames where id = '$box'",false);
                ?>
                <meta http-equiv="refresh" content="0;URL=<?php echo $setting['siteurl'];?>admin/index.php?act=managedowngame" />
                <?php
            }
        }
    }
} else {
 if (isset($_GET['page'])) {
	   $pageno = $_GET['page'];
	} else {
	   $pageno = 1;
	} 
	$result = yasDB_select("SELECT count(id) FROM downgames");
	$query_data = $result->fetch_array(MYSQLI_NUM);;
	$numrows = $query_data[0];
	$result->close();
	
	$rows_per_page = 10;
	$lastpage = ceil($numrows/$rows_per_page);
	$pageno = (int)$pageno;
	if ($lastpage < 1) {
		$lastpage = 1;
	}
	if ($pageno < 1) {
	   $pageno = 1;
	} elseif ($pageno > $lastpage) {
	   $pageno = $lastpage;
	} 
	$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;?>
<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">ID</th>
						<th>Miniatura</th>
						<th>Titulo del juego</th>
						<th>Descripcion</th>
						<th>Descarga</th>
						<th>D.Mochi</th>
						<th>Editar</th>
						<th class="last">Eliminar</th>
					</tr>
<?php
	$query = yasDB_select("SELECT * FROM downgames ORDER BY `id` DESC ");
	if($query->num_rows==0) {
    } else { 
		while($row = $query->fetch_array(MYSQLI_ASSOC)) { ?>
		<form name="deleteform" method="post" action="">
			    <tr>
				<td class="first style1" width="80px"><?php echo $row['id'];?></td>
				<td><img src="<?php echo $row['thumbnail'];?>" style="width: 60px; height: 50px;" /></td>
				<td><?php echo $row['title'];?></td>
				<td><?php echo $row['description'];?></td>
				<td><?php echo $row['downloadtimes'];?></td>	
				<td><?php echo $row['mochidownloads'];?></td>
				<td ><a href="index.php?act=managedowngame&edit=<?php echo $row['id'];?>&m=1"><font color="#cc0000">Editar</font></a></td>
				<td class="last"><font color=red><a href="index.php?act=managedowngame&delete=<?php echo $row['id'];?>" onclick="return confirm(\'Esta seguro que desea eliminar este juego?\')"><font color="#cc0000">Borrar</font></a><br/><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['id'];?>"></td>
				</tr>
<?		}
$query->close();
}
?>
</table></div>
<div style="text-align:right;">
        <input name="deletechecked" class="button" type="submit" id="deletechecked" value="Chequear la eliminacion">
        </form>
    </div>
<div style="text-align:center;padding-bottom:10px;">	
<?php
     if ($pageno == 1) {
	   echo ' PRIMERA ANTERIOR ';
	} else {
	   echo ' <a href="index.php?act=managedowngame&page=1">PRIMERA</a> ';
	   $prevpage = $pageno-1;
	   echo ' <a href="index.php?act=managedowngame&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=managedowngame&page=' . $nextpage . '">SIGUIENTE</a> ';
	   echo ' <a href="index.php?act=managedowngame&page=' . $lastpage . '">ULTIMA</a> ';
	}
?>
</div>
<?php
}
?>
</div>