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

session_name('portal_absa');
session_start();

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'] = str_replace("\\","",$_SESSION['query']);
$SQL_1="select COUNT(*) as count from (".$_SESSION['query'].") as temp";
//$SQL_1="select COUNT(*) as count from (select rfcemisor,tipodocto,serie,folio from doce) 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']." order by $sidx $sord LIMIT $start , $limit";
//$SQL = "select rfcemisor,tipodocto,serie,folio from doce order by $sidx $sord LIMIT $start , $limit";

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

$responce=new stdClass();
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
$i=0;


while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
    $responce->rows[$i]['id']=$row['username'];
			if($row['activado'] == 0){
				$img="msn16.png";
			}else if($row['activado'] == 1){
				$img="watch16.png";
			}else if($row['activado'] == 2){
				$img="accept16.png";
			}

			$iconos = "<center><img src='Imagenes/$img' title='Usuarios'/></center>";
                        $_SESSION['datosGrid1'].=" \t".$row['username']."\t".$row['nombre']."\t".$row['puesto']."\t".$row['rfc_cliente']."\t".$row['esadmin']."\t".$row['todoRFC']."\t".$row['esCliente']."\t".$row['esProv']."\t".$row['fechacreacion']."\t".$row['fechaacceso']."\t".$row['taxid']."\t".$row['email']."\t".$row['telefono']."\t".$row['contactoC']."\n";
			$responce->rows[$i]['cell']=array($iconos,$row['username'],$row['nombre'],$row['puesto'],$row['rfc_cliente'],$row['esadmin'],$row['todoRFC'],$row['esCliente'],$row['esProv'],$row['fechacreacion'],$row['fechaacceso'],$row['taxid'],$row['email'],$row['telefono'],$row['contactoC'],$row['ref_rfc_receptor']);
			$i++;
}

echo json_encode($responce);

?>