<?
/*
Autor: Lic. Carlos Alberto Vaca Coronado
Version: 3.2.0
*/

session_name('portal_absa');session_start();if(!isset($_SESSION['usuario_p_prov_absa']) || trim($_SESSION['usuario_p_prov_absa']) == "" || trim(strtoupper($_SESSION["URL_PORTAL"]."admin.php")) != trim(strtoupper($_SERVER['HTTP_REFERER']))){?><script>window.location.href="<?=$_SESSION["URL_PORTAL"]."index.php"?>"</script><?exit;}

require "../conexion/conectar.php";

$conexion = new CONDB();
$conexion->conectar();

$page = $_GET['page']; // get the requested page
$limit = $_GET['rows']; // get how many rows we want to have into the grid
$sidx = $_GET['sidx']; // get index row - i.e. user click to sort
$sord = $_GET['sord']; // get the direction

if(!$sidx) $sidx =1;

$totalrows = isset($_REQUEST['totalrows']) ? $_REQUEST['totalrows']: false;
if($totalrows) {
	$limit = (integer) $totalrows;
}


$_SESSION['query_historial'] = str_replace("\\","",$_SESSION['query_historial']);
$SQL_1="select COUNT(*) as count from (".$_SESSION['query_historial'].") as temp";
$result = mysqli_query($conexion->CONEXION,$SQL_1);

$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$count = (integer) $row['count'];

if( $count >0 ) {
	$total_pages = (integer) ceil($count/$limit);
} else {
	$total_pages = 0;
}
if ($page > $total_pages) $page=$total_pages;
if ($limit<0) $limit = 0;
$start = (integer) $limit*$page - $limit;
if ($start<0) $start = 0;

$SQL = $_SESSION['query_historial']." order by $sidx $sord LIMIT $start , $limit";

$result = mysqli_query($conexion->CONEXION, $SQL );

$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
$i=0;


$ocultos = explode(",",$_GET['ocultos']);
for($j = 0; $j < count($ocultos); $j++){
	$ocultos[$j] = trim($ocultos[$j]);
}
while ($property = mysqli_fetch_field($result)) {
	$columnas[$j]=$property->name;
}

$_SESSION['datosGrid2']="";
unset($_SESSION['datosGrid2']);

while($row = mysqli_fetch_array($result,MYSQL_ASSOC)){

    //$responce->rows[$i]['id']=$row[$_GET['key']];
	$r=0;
	for($j=0; $j < count($columnas); $j++){
		
		$pos = array_keys($ocultos,$columnas[$j]);
		
		if($ocultos[$pos[0]] == ""){
                        $_SESSION['datosGrid2'].=$row[$columnas[$j]]."\t";
			$arrDat[$r] = $row[$columnas[$j]];
			$r++;
		}
	}
        $_SESSION['datosGrid2'].="\n";
        
	$responce->rows[$i]['cell']=$arrDat;
	$i++;
}
echo json_encode($responce);

?>