<?php

require_once("../includes/clase_sesion.php");
$sesion = new sesion(); // inicio la sesion
require_once("../includes/academia_constantes.php");
require_once("../includes/funciones_buscador.php");
require_once("../includes/funciones_generales.php");

require_once("../includes/MySQL.php");
$academia = new Servidor_Base_Datos(AC_CONN_HOST, AC_CONN_USER, AC_CONN_PASS, AC_CONN_DB . "" . $sesion->get('anio'));


$examp = $_REQUEST["q"]; //query number
$page = $_REQUEST['page']; // get the requested page
$limit = $_REQUEST['rows']; // get how many rows we want to have into the grid
$sidx = $_REQUEST['sidx']; // get index row - i.e. user click to sort
$sord = $_REQUEST['sord']; // get the direction
$id = $_REQUEST['id'];
if (!$sidx)
    $sidx = 1;
if (!$sord)
    $sord = "asc";
if (!$limit)
    $limit = 10;

// search options
// IMPORTANT NOTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// this type of constructing is not recommendet
// it is only for demonstration
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

$cnf = $sesion->get("configuracion");
$calendario = $cnf['calendario'];
$fec_cal_edad = $calendario == 'A' ? date('Y-10-31') : date('Y-10-31');

//print_r($cnf);
$wh2 = '';
$tb_est = 'aca_estudiantes';

$sql = "SELECT
      cod_estudiante
    , numedocumento_est
    , nombre1_est
    , nombre2_est
    , apellido1_est
    , apellido2_est
    , estado_est
    , matriculado
    , genero_est
	, fechnacimiento_est
    , telefono
    , tipodocumento_est
    ,cod_estudiantes_grupos as cod_est_gr
    ";

$sqlTb = "from pag_estudiantes_adm";

$grupo = $_POST['cdgr'];


if ($grupo != '' && $grupo != '0x') {
    $wh2.=" AND cod_grupo ='$grupo'";
}

$wh = "";

$searchOn = Strip($_REQUEST['_search']);
if ($searchOn == 'true') {
    $searchstr = Strip($_REQUEST['filters']);
    //$wh= constructWhere($searchstr);
    $jsona = json_decode($searchstr, true);
    $wh = " AND " . getStringForGroup($jsona);
    //var_dump($wh);
    //echo $ss;
}

$cons = "SELECT COUNT(*) AS count $sqlTb WHERE 1=1 $wh2 " . $wh;
//		echo $cons;
$academia->instruccion($cons);

$row = $academia->extraer_registro();
$count = $row['count'];
$academia->liberar();
if ($count > 0) {
    $total_pages = ceil($count / $limit);
} else {
    $total_pages = 0;
}
if ($page > $total_pages)
    $page = $total_pages;
$start = $limit * $page - $limit; // do not put $limit*($page - 1)
if ($start < 0)
    $start = 0;

//*************************************
$sidx = $sidx == 'nom_ape' ? 'apellido1_est' : $sidx;
//**************************************		

$SQL = $sql . " " . $sqlTb . " where 1=1 $wh2 " . $wh . " ORDER BY $sidx $sord LIMIT $start , $limit";

//echo $SQL;	
$responce->page = $page;

$responce->total = $total_pages;
$responce->records = $count;
$academia->instruccion($SQL);
$i = 0;
$formato_nombres = $cnf['format_nom'];
while ($row = $academia->extraer_registro()) {
    $responce->rows[$i]['id'] = $row['cod_est_gr'];
    $nom_ape = $row['apellido1_est'] . ' ' . $row['apellido2_est'] . ' ' . $row['nombre1_est'] . ' ' . $row['nombre2_est'];
//    $dtsGrupo = !empty($row['grupo']) ? json_decode($row['grupo'], true) : array();
    $responce->rows[$i]['cell'] = array(
        $row[numedocumento_est],
        formatoPalabras(trim($nom_ape), $formato_nombres),
    );


    $i++;
}
if ($sesion->get('ac_usuario') == 'prointel') {
    $responce->sql = $SQL;
}

echo json_encode($responce);
$academia->cerrar();
?>
