<?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_score_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_score_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"]);
}

$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"]));

	
	//	tipo_nucleo - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("tipo_nucleo", $data, $keylink);
			$row["tipo_nucleo_value"] = $value;
	//	ano - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("ano", $data, $keylink);
			$row["ano_value"] = $value;
	//	mes - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("mes", $data, $keylink);
			$row["mes_value"] = $value;
	//	coordenadores - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("coordenadores", $data, $keylink);
			$row["coordenadores_value"] = $value;
	//	professores - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("professores", $data, $keylink);
			$row["professores_value"] = $value;
	//	alunos - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("alunos", $data, $keylink);
			$row["alunos_value"] = $value;
	//	meta_alunos - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("meta_alunos", $data, $keylink);
			$row["meta_alunos_value"] = $value;
	//	dias_funcionamento - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("dias_funcionamento", $data, $keylink);
			$row["dias_funcionamento_value"] = $value;
	//	meta_funcionamento - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("meta_funcionamento", $data, $keylink);
			$row["meta_funcionamento_value"] = $value;
	//	alunos_por_coordenador - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("alunos_por_coordenador", $data, $keylink);
			$row["alunos_por_coordenador_value"] = $value;
	//	meta_alunos_por_coordenador - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("meta_alunos_por_coordenador", $data, $keylink);
			$row["meta_alunos_por_coordenador_value"] = $value;
	//	freq_media_coordenador - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("freq_media_coordenador", $data, $keylink);
			$row["freq_media_coordenador_value"] = $value;
	//	min_freq_coordenador - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("min_freq_coordenador", $data, $keylink);
			$row["min_freq_coordenador_value"] = $value;
	//	freq_media_aluno - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("freq_media_aluno", $data, $keylink);
			$row["freq_media_aluno_value"] = $value;
	//	min_freq_aluno - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("min_freq_aluno", $data, $keylink);
			$row["min_freq_aluno_value"] = $value;
	//	freq_media_professor - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("freq_media_professor", $data, $keylink);
			$row["freq_media_professor_value"] = $value;
	//	min_freq_professor - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("min_freq_professor", $data, $keylink);
			$row["min_freq_professor_value"] = $value;
	//	meta_atividades - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("meta_atividades", $data, $keylink);
			$row["meta_atividades_value"] = $value;
	//	atividades_elaboradas - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("atividades_elaboradas", $data, $keylink);
			$row["atividades_elaboradas_value"] = $value;
	//	atividades_realizadas - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("atividades_realizadas", $data, $keylink);
			$row["atividades_realizadas_value"] = $value;
	//	score - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("score", $data, $keylink);
			$row["score_value"] = $value;
	//	operador - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("operador", $data, $keylink);
			$row["operador_value"] = $value;
	//	data_insercao - Short Date
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("data_insercao", $data, $keylink);
			$row["data_insercao_value"] = $value;
		$rowinfo[] = $row;
		$data = $cipherer->DecryptFetchedArray($rs);
	}
	$xt->assign_loopsection("details_row",$rowinfo);
}
$returnJSON = array("success" => true);
$xt->load_template("senuc_score_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>";
?>