<div id="center-column">
<div class="top-bar">
<h1>Cpanel - Comentarios del juego</h1>
<div class="breadcrumbs"><a href="index.php?act=memberscomment" title="Members Comments">Comentarios de usuarios</a> / <a href="index.php?act=newsblogcomments" title="News Blog Comments">Noticias blog Comentarios</a></div>
</div><br />
<div class="select-bar">
<label>
<h3>Administrar Comentarios</h3>
</label>
</div>
<script>
function confirmDelete(delUrl) {
	if (confirm("Esta seguro que desea eliminar este comentario?")) {
	document.location = delUrl;
	}
}
</script>
<?php
if(!empty($_GET['edit'])) {

	$query = yasDB_select("select * from comments where id = '{$_GET['edit']}'",false);
	if($query->num_rows == 0) {
		echo '<center>No se puede editar un comentario 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=comments">
			<table class="listing form" cellpadding="0" cellspacing="0">
		<tr>
	    <th class="full" colspan="2">Editar - Comentario</th>
	    </tr>
		<tr>
		<td class="first" width="172"><strong>Nombre</strong></td>
		<td class="last"><input type="text" name="name" value="<?php echo $row['name'];?>" /></td>
		</tr>
		<tr class="bg">
		<td class="first"><strong>Comentario</strong></td>
		<td class="last"><textarea name="comment" /><?php echo $row['comment'];?></textarea></td>
		</tr>
		</tr>
		<tr>
		<td class="first"><strong>Enviar</strong></td>
		<td class="last"><input type="hidden" name="id" value="<?php echo $row['id'];?>" /><input type="submit" class="button" name="edit" value="Editar!" /><input type="reset" value="Reiniciar"/></td>
		</tr>
		</table>
		</div>
		</form>		
	<?php
	}
	$query->close();
	
} elseif(isset($_POST['edit'])) {
	if(empty($_POST['name']) || empty($_POST['comment'])) {
		echo '<center>Uno o mas campos se deja en blanco.<br />';
		echo '<a href="index.php?act=comments?edit=' . $_POST['id'] . '">Haga clic aqui para volver</a></center>';
	} else {
		yasDB_update("update comments set name = '{$_POST['name']}', comment = '{$_POST['comment']}' where id = '{$_POST['id']}'",false);
		echo '<center>Comentario editado con exito!<br />';
		echo '<a href="index.php?act=comments">Haga clic aqui para continuar</a></center>';
	}
} elseif(!empty($_GET['delete'])) {
	$query = yasDB_select("select id from comments where id = '{$_GET['delete']}'",false);
	if($query->num_rows == 0) {
		echo '<center>No se puede borrar un comentario que no existe!<br />';
		echo '<a href="index.php?act=comments">Haga clic aqui para volver</a></center>';
		$query->close();
		
	} else {
		$query->close();
		
		yasDB_delete("delete from comments where id = '{$_GET['delete']}'",false);
		echo '<center>Comentario eliminado correctamente.<br />';
		echo '<a href="index.php?act=comments">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;?> comentario(s)?");
        if (!response) {
            window.location.href = '<?php echo $setting['siteurl'];?>admin/index.php?act=comments&remove=notta';
            
        }
    </script>
    <?php
    foreach ($_POST['checkbox'] as $box) {
        $query = yasDB_select("SELECT id FROM comments WHERE id = '$box'",false);
        if($query->num_rows == 0) {
            echo '<center>No se puede eliminar este comentario que no existe!<br />';
            echo '<a href="index.php?act=comments">Haga clic aqui para volver</a></center>';
        } else {
            if ($_GET['remove'] != 'notta') {
                $row = $query->fetch_array(MYSQLI_ASSOC);
                $query->close();            
                yasDB_delete("DELETE FROM comments WHERE id = '$box'",false);
                ?>
                <meta http-equiv="refresh" content="0;URL=<?php echo $setting['siteurl'];?>admin/index.php?act=comments" />
                <?php
            }
        } 
	}
} else {

	if (isset($_GET['page'])) {
		$pageno = $_GET['page'];
	} else {
		$pageno = 1;
	} 
	$result = yasDB_select("SELECT count(id) FROM comments",false);
	$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 ($pageno > $lastpage) {
	   $pageno = $lastpage;
	} 
	if ($pageno < 1) {
	   $pageno = 1;
	} 
	$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
	$query = yasDB_select("select * from comments $limit",false);
	if($query->num_rows == 0) {
		echo '<center>No hay comentarios en la base de datos!</center>';
	} else {
		?>
		<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="40px">ID</th>
						<th style="width:50px" >ID del juego</th>
						<th style="width:80px;">Nombre</th>
						<th>Comentario</th>
						<th style="width:100px;">direccion de Ip</th>
						<th width="30px">Editar</th>
						<th class="last" style="width:50px">Eliminar</th>
					</tr>
		<?php
		while($row = $query->fetch_array(MYSQLI_ASSOC)) {
			?><form name="deleteform" method="post" action="">			
	        <tr>
			<td class="first style1"><?php echo $row['id'];?></td>
			<td><?php echo $row['gameid'];?></td>
			<td style="overflow:hidden;"><?php echo $row['name'];?></td>
			<td style="overflow:hidden;"><?php echo $row['comment'];?></td>
			<td><?php echo $row['ipaddress'];?></td>
			<td><a href="index.php?act=comments&edit=<?php echo $row['id'];?>"><font color="#cc0000">Editar</font></a></td>
			<td class="last"><a href="index.php?act=comments&delete=<?php echo $row['id'];?>" onclick="return confirm(\'Esta seguro que desea eliminar este comentario?\')"><font color="#cc0000">Eliminar</font></a><br/><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['id'];?>"></td>
			</tr>
			</table>
			</div>
			<?php
		}
		$query->close();
		
		?>
		<div style="text-align:right;padding-right:20px;padding-top:10px;">
        <input name="deletechecked" class="button" type="submit" 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=comments&page=1">PRIMERA</a> ';
			$prevpage = $pageno-1;
			echo ' <a href="index.php?act=comments&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=comments&page=' . $nextpage . '">SIGUIENTE</a> ';
			echo ' <a href="index.php?act=comments&page=' . $lastpage . '">ULTIMA</a> ';
		} ?> 
	</div>
	<?php
	}
}
?>
</div>