<div id="center-column">
<div class="top-bar">
				<a href="index.php?act=uploadgames" title="Upload Games" class="button">AGREGAR JUEGOS</a>
				<h1>Cpanel - Juegos</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</h3>
		    </label>
		  </div>
<?php
$qry = yasDB_select("SELECT gameid from featuredgames");
$gameids = array();
while ($game = $qry->fetch_array(MYSQLI_NUM)) {
	$gameids[] = $game[0];
}
if(!empty($_GET['edit'])) {
	$query = yasDB_select("SELECT * FROM games WHERE id = '{$_GET['edit']}'",false);
	if($query->num_rows == 0) {
		echo 'You cannot edit a file that doesnt exist.';
		$query->close();
		
	} else {
		$row = $query->fetch_array(MYSQLI_ASSOC);
		$query->close();
		$catName = array();
		$catId = array();
		$i = 0;
		$query = yasDB_select("SELECT id, name FROM categories",false);
		while($names = $query->fetch_array(MYSQLI_BOTH)) {
			$catName[$i] = $names['name'];
			$catId[$i] = $names['id'];
			$i++;
		}
		$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="edit" method="post" action="index.php?act=managegames">
			<table class="listing form" cellpadding="0" cellspacing="0">
	        <tr>
	        <th class="full" colspan="2">Edit - <?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>Juego Height</strong></td>
			<td class="last"><input type="text" name="gameheight" value="<?php echo $row['height'];?>" /></td>
		    </tr>
			<tr>
			<td class="first"><strong>Juego Width</strong></td>
			<td class="last"><input type="text" name="gamewidth" value="<?php echo $row['width'];?>" /></td>
			</tr>
			<tr class="bg">
			<td class="first"><strong>Descripcion</strong></td>
			<td class="last"><textarea name="description" /><?php echo $row['description'];?></textarea></td>
			</tr>
            <tr>
			<td class="first"><strong>Instrucciones</strong></td>
			<td class="last"><textarea name="instructions" /><?php echo $row['instructions'];?></textarea></td>
			</tr>
			<tr class="bg">
			<td class="first"><strong>Palabras clave</strong></td>
			<td class="last"><textarea name="keywords" /><?php echo $row['keywords'];?></textarea></td>
			</tr>
			<tr>
			<td class="first"><strong>Categoria</strong></td>
			<td class="last"><select name="category">
		<option value="<?php echo $row['category'];?>"><?php echo $catName[$row['category']-1];?></option>
		<?php
		$len = count($catName);
		for($i=0;$i<$len;$i++) {
		?><option value="<?php echo $catId[$i];?>"><?php echo $catName[$i];?></option>
		<?php } ?></select></td>
			</tr>
			<tr class="bg">
			<td class="first"><strong>Miniatura</strong></td>
			<td class="last"><input type="text" name="thumbnail" value="<?php echo $row['thumbnail'];?>"  /></td>
			</tr>
			<tr>
			<td class="first"><strong>Archivos</strong></td>
			<td class="last"><select name="type">
				<option value="<?php echo $row['type'];?>"><?php echo $row['type'];?></option>
				<option value="SWF">swf</option>
				<option value="DCR">dcr</option>
				<option value="FLV">flv</option>
				<option value="WMV">wmv</option>
				<option value="AVI">avi</option>
				<option value="MPG">mpg</option>
				<option value="MOV">mov</option>
				<option value="IMAGE">image</option>
				<option value="YOUTUBE">youtube</option>
				</select> <input type="text" name="file" value="<?php echo $row['file'];?>"/></td>
			</tr>
			<tr>
			<td class="first"><strong>Destacados</strong></td>
			<td class="last">
				<select name="featured">
				<option value="<?php if (in_array($row['id'], $gameids)) { echo 'yes'; } else { echo 'no'; } ?>"><?php if (in_array($row['id'], $gameids)) { echo 'Yes'; } else { echo 'No'; } ?></option>
				<option value="si">si</option>
				<option value="no">No</option>
				</select>
			</td>
			</tr>
			<tr class="bg">
			<td class="first"></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!"/>&nbsp;
		<input type="reset" class="button" name="reset" value="Reiniciar"/></td>
			</tr>
			</table>
			</div>
			</form>
	<?php			
	}
	
} elseif(isset($_POST['edit'])) {
	if(empty($_POST['title']) || empty($_POST['thumbnail'])) {
		echo '<center>One or more fields was left empty.<br />';
		echo '<a href="index.php?act=managegames&edit=' . $_POST['id'] . '">Click here to go back</a></center>';
	} else {
		yasDB_update("UPDATE games SET title = '". yasDB_clean($_POST['title'])."', description = '".yasDB_clean($_POST['description'])."', instructions = '".yasDB_clean($_POST['instructions'])."',keywords = '".yasDB_clean($_POST['keywords'])."', category = '{$_POST['category']}', height = '{$_POST['gameheight']}', width = '{$_POST['gamewidth']}', type = '{$_POST['type']}', thumbnail = '".yasDB_clean($_POST['thumbnail'])."', file = '".yasDB_clean($_POST['file'])."' where id = {$_POST['id']}",false);
		$result = yasDB_select("SELECT gameid FROM featuredgames WHERE gameid = {$_POST['id']}");
		if ($_POST['featured'] == 'yes') {
			if ($result->num_rows == 0) {
				yasDB_insert("INSERT INTO featuredgames (gameid) VALUES({$_POST['id']})");
			}
		}
		elseif ($_POST['featured'] == 'no') {
			yasDB_delete("DELETE FROM featuredgames WHERE gameid = {$_POST['id']}");
		}
		echo '<p align="center">Archivo correctamente editado!<br />';
		if (!empty($_POST['m'])) {
			echo '<center><a href="index.php?act=managegames">Haga clic aqui para continuar</a></p></center>';
		} else {
			echo '<center><a href="index.php?act=brokenfiles">Haga clic aqui para continuar</a></p></center>';
		}
	}
} elseif(!empty($_GET['delete'])) {
	$query = yasDB_select("SELECT id, file, thumbnail FROM games WHERE id = '{$_GET['delete']}'",false);
	if($query->num_rows == 0) {
		echo '<center>No se puede eliminar un juego que no existe!<br />';
		echo '<a href="index.php?act=managegames">Haga clic aqui para volver</a></center>';
	} else {
		$row = $query->fetch_array(MYSQLI_ASSOC);
		@unlink('../' . $row['file']);
		@unlink('../' . $row['thumbnail']);
		$query->close();
		
		yasDB_delete("delete from games where id = '{$_GET['delete']}'",false);
		echo '<center><p style="text-align:center;">El juego se ha borrado con exito.<br />';
		echo '<a href="index.php?act=managegames">Haga clic aqui para continuar</a></p></center>';
	}
} 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=managegames&remove=notta';
            
        }
    </script>
    <?php
    foreach ($_POST['checkbox'] as $box) {
        $query = yasDB_select("SELECT id, file, thumbnail FROM games 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=managegames">Haga clic aqui para volver</a></center>';
        } else {
            if ($_GET['remove'] != 'notta') {
                $row = $query->fetch_array(MYSQLI_ASSOC);
                @unlink('../' . $row['file']);
                @unlink('../' . $row['thumbnail']);
                $query->close();            
                yasDB_delete("delete from games where id = '$box'",false);
                ?>
                <meta http-equiv="refresh" content="0;URL=<?php echo $setting['siteurl'];?>admin/index.php?act=managegames" />
                <?php
            }
        }
    }
} else {
	if (isset($_GET['page'])) {
	   $pageno = $_GET['page'];
	} else {
	   $pageno = 1;
	} 
	$result = yasDB_select("SELECT count(id) FROM games");
	$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" width="86px">Juegos</th>
						<th width="204px">Descripcion</th>
						<th>Categoria</th>
						<th>Destacados</th>
						<th></th>
						<th class="last"></th>
					</tr>
<?php
	$query_cats = yasDB_select("SELECT name FROM categories");	
	$cats = array();
	$i = 0;
	while ($catRow = $query_cats->fetch_array(MYSQLI_ASSOC)) {;
		$cats[$i] = $catRow['name'];
		$i++;
	}
	$query_cats->close();
	$query = yasDB_select("SELECT * FROM games ORDER BY `id` DESC $limit");
	if($query->num_rows==0) {
    } else { 
		?><form name="deleteform" method="post" action=""><?php
		while($row = $query->fetch_array(MYSQLI_ASSOC)) {
			?>
			<tr>
			<td class="first style1" width="80px"><img src="<?php echo $setting['siteurl'] . $row['thumbnail'];?>" style="width: 60px; height: 50px;" /><br/><?php echo $row['title'];?></td>
			<td><?php echo $row['description'];?></td>
			<td><?php echo $cats[$row['category']-1];?></td>
			<td><?php if (in_array($row['id'], $gameids)) echo '<img src="img/checkmark.png" width="16" height="16" alt="Yes" />';?></td>
			<td ><a href="index.php?act=managegames&edit=<?php echo $row['id'];?>&m=1"><font color="#cc0000">Editar</font></a></td>
			<td class="last"><a href="index.php?act=managegames&delete=<?php echo $row['id'];?>" onclick="return confirm(\'Are you sure you want to delete this game?\')"><font color="#cc0000">Eliminar</font></a><br/><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['id'];?>"></td>
			</tr>
<?php			
		}
	}
	unset ($cats);
	$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=managegames&page=1">PRIMERA</a> ';
	   $prevpage = $pageno-1;
	   echo ' <a href="index.php?act=managegames&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=managegames&page=' . $nextpage . '">SIGUIENTE</a> ';
	   echo ' <a href="index.php?act=managegames&page=' . $lastpage . '">ULTIMA</a> ';
	}
?>
</div>
<?php
}
?>
</div>