<?php
@ini_set("display_errors","1");
@ini_set("display_startup_errors","1");

include("include/dbcommon.php");
header("Expires: Thu, 01 Jan 1970 00:00:01 GMT"); 

include("include/senuc_professores_variables.php");

$mode = postvalue("mode");

if(!isLogged())
{ 
	return;
}
if(!CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Search"))
{
	return;
}

$cipherer = new RunnerCipherer($strTableName);


include('include/xtempl.php');
$xt = new Xtempl();

$layout = new TLayout("detailspreview","BreezeAqua1","MobileAqua1");
$layout->blocks["bare"] = array();
$layout->containers["dcount"] = array();

$layout->containers["dcount"][] = array("name"=>"detailspreviewheader","block"=>"","substyle"=>1);


$layout->containers["dcount"][] = array("name"=>"detailspreviewdetailsfount","block"=>"","substyle"=>1);


$layout->containers["dcount"][] = array("name"=>"detailspreviewdispfirst","block"=>"display_first","substyle"=>1);


$layout->skins["dcount"] = "empty";
$layout->blocks["bare"][] = "dcount";
$layout->containers["detailspreviewgrid"] = array();

$layout->containers["detailspreviewgrid"][] = array("name"=>"detailspreviewfields","block"=>"details_data","substyle"=>1);


$layout->skins["detailspreviewgrid"] = "grid";
$layout->blocks["bare"][] = "detailspreviewgrid";$page_layouts["senuc_professores_detailspreview"] = $layout;


$recordsCounter = 0;

//	process masterkey value
$mastertable = postvalue("mastertable");
$masterKeys = my_json_decode(postvalue("masterKeys"));
if($mastertable != "")
{
	$_SESSION[$strTableName."_mastertable"]=$mastertable;
//	copy keys to session
	$i = 1;
	if(is_array($masterKeys) && count($masterKeys) > 0)
	{
		while(array_key_exists ("masterkey".$i, $masterKeys))
		{
			$_SESSION[$strTableName."_masterkey".$i] = $masterKeys["masterkey".$i];
			$i++;
		}
	}
	if(isset($_SESSION[$strTableName."_masterkey".$i]))
		unset($_SESSION[$strTableName."_masterkey".$i]);
}
else
	$mastertable = $_SESSION[$strTableName."_mastertable"];

//$strSQL = $gstrSQL;

if($mastertable == "senuc_nucleo")
{
	$where = "";
		$where .= GetFullFieldName("idnucleo", $strTableName, false)."=".make_db_value("idnucleo",$_SESSION[$strTableName."_masterkey1"]);
}
if($mastertable == "nucleos_presenciais")
{
	$where = "";
		$where .= GetFullFieldName("idnucleo", $strTableName, false)."=".make_db_value("idnucleo",$_SESSION[$strTableName."_masterkey1"]);
}
if($mastertable == "nucleos_virtuais")
{
	$where = "";
		$where .= GetFullFieldName("idnucleo", $strTableName, false)."=".make_db_value("idnucleo",$_SESSION[$strTableName."_masterkey1"]);
}

$str = SecuritySQL("Search");
if(strlen($str))
	$where.=" and ".$str;
$strSQL = $gQuery->gSQLWhere($where);

$strSQL.=" ".$gstrOrderBy;

$rowcount = $gQuery->gSQLRowCount($where);

$xt->assign("row_count",$rowcount);
if($rowcount) {
	$xt->assign("details_data",true);
	$rs = db_query($strSQL,$conn);

	$display_count = 10;
	if($mode == "inline")
		$display_count*=2;
	if($rowcount>$display_count+2)
	{
		$xt->assign("display_first",true);
		$xt->assign("display_count",$display_count);
	}
	else
		$display_count = $rowcount;

	$rowinfo = array();
	
	$data = $cipherer->DecryptFetchedArray($rs);
	require_once getabspath('classes/controls/ViewControlsContainer.php');
	$pSet = new ProjectSettings($strTableName, PAGE_LIST);
	$viewContainer = new ViewControlsContainer($pSet, PAGE_LIST);
	while($data && $recordsCounter<$display_count) {
		$recordsCounter++;
		$row = array();
		$keylink = "";
		$keylink.="&key1=".htmlspecialchars(rawurlencode(@$data["id"]));

	
	//	nome - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("nome", $data, $keylink);
			$row["nome_value"] = $value;
	//	email - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("email", $data, $keylink);
			$row["email_value"] = $value;
	//	cpf - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("cpf", $data, $keylink);
			$row["cpf_value"] = $value;
	//	telefone - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("telefone", $data, $keylink);
			$row["telefone_value"] = $value;
	//	celular - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("celular", $data, $keylink);
			$row["celular_value"] = $value;
	//	operadora - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("operadora", $data, $keylink);
			$row["operadora_value"] = $value;
	//	nascimento - Short Date
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("nascimento", $data, $keylink);
			$row["nascimento_value"] = $value;
	//	etnia - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("etnia", $data, $keylink);
			$row["etnia_value"] = $value;
	//	renda - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("renda", $data, $keylink);
			$row["renda_value"] = $value;
	//	estado - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("estado", $data, $keylink);
			$row["estado_value"] = $value;
	//	cidade - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("cidade", $data, $keylink);
			$row["cidade_value"] = $value;
	//	escolaridade - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("escolaridade", $data, $keylink);
			$row["escolaridade_value"] = $value;
	//	curso - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("curso", $data, $keylink);
			$row["curso_value"] = $value;
	//	materias - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("materias", $data, $keylink);
			$row["materias_value"] = $value;
	//	ingresso_nucleo - Short Date
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("ingresso_nucleo", $data, $keylink);
			$row["ingresso_nucleo_value"] = $value;
	//	bolsista - Checkbox
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("bolsista", $data, $keylink);
			$row["bolsista_value"] = $value;
	//	motivo - Custom
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("motivo", $data, $keylink);
			$row["motivo_value"] = $value;
	//	ativo - Checkbox
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("ativo", $data, $keylink);
			$row["ativo_value"] = $value;
	//	ultimo_score - Custom
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("ultimo_score", $data, $keylink);
			$row["ultimo_score_value"] = $value;
	//	data_ultimo_score - Custom
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("data_ultimo_score", $data, $keylink);
			$row["data_ultimo_score_value"] = $value;
	//	tipo - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("tipo", $data, $keylink);
			$row["tipo_value"] = $value;
		$rowinfo[] = $row;
		$data = $cipherer->DecryptFetchedArray($rs);
	}
	$xt->assign_loopsection("details_row",$rowinfo);
}
$returnJSON = array("success" => true);
$xt->load_template("senuc_professores_detailspreview.htm");
$returnJSON["body"] = $xt->fetch_loaded();

if($mode!="inline"){
	$returnJSON["counter"] = postvalue("counter");
	$layout = GetPageLayout(GoodFieldName($strTableName), 'detailspreview');
	if($layout)
	{
		$rtl = $xt->getReadingOrder() == 'RTL' ? 'RTL' : '';
		$returnJSON["style"] = "styles/".$layout->style."/style".$rtl.".css";
		$returnJSON["pageStyle"] = "pagestyles/".$layout->name.$rtl.".css";
		$returnJSON["layout"] = $layout->style." page-".$layout->name.".css";
	}	
}	

echo "<textarea>".htmlspecialchars(my_json_encode($returnJSON))."</textarea>";
?>