<?php
/**
 * Showing the page
 */

//Defining
$template	= new Template( $page );

//Definindo a página
$meta		= ( defined( 'METADATA_CENTRAL_DE_RELACIONAMENTO' ) ) ? METADATA_CENTRAL_DE_RELACIONAMENTO : null;

try{

	//Checking
	if( isset( $meta ) ){

		//Defining
		$object							= SearchController::byId( 'metadata', $meta );

		//Defining
		$template->METADATA_TITLE		= $object->title;
		$template->METADATA_DESCRIPTION	= $object->description;
		$template->METADATA_KEYWORDS	= $object->keyword;

	}

}catch( Exception $objEx ){}

try{

	//Defining
	$destination	= ( isset( $_GET[ 'd' ] ) && $_GET[ 'd' ] != '' ) ? (int) $_GET[ 'd' ] : null;
	$id				= ( isset( $_GET[ 'i' ] ) && $_GET[ 'i' ] != '' ) ? (int) $_GET[ 'i' ] : null;
	$token			= ( isset( $_GET[ 't' ] ) && $_GET[ 't' ] != '' ) ? (string) $_GET[ 't' ] : null;

	//Defining the object
	$object			= SearchController::byIdByDestination( $id, $destination );

	//Defining
	$hash			= md5( HASH . $object->id . $object->email );

	//Checking
	if( $hash == $token ){

		//Defining the object to template
		$template->OBJ	= $object;

		//Formating the date
		$date			= Util::formatDate( $object->date, 'separate' );

		//Defining the dates to template
		$template->DATE	= $date->day . '/' . $date->month;
		$template->HOUR	= $date->hour . ':' . $date->minute;

		//Defining the message to template
		$template->MESSAGE			= ( isset( $object->message ) ) ? nl2br( $object->message ) : 'Sem mensagem inicial';

		//Checking the answers
		if( !is_null( $object->answers ) ){

			//Listing
			foreach( $object->answers as $answer ){

				//Formating the date
				$date					= Util::formatDate( $answer->date, 'separate' );

				//Defining the date to template
				$template->ANSWER_DATE	= $date->day . '/' . $date->month;
				$template->ANSWER_HOUR	= $date->hour . ':' . $date->minute;

				//Checking
				if( $answer->type == ANSWER_CLIENT ){

					//Defining
					$template->CLASS	= 'pergunta';

					//Defining the answer to template
					$template->ANSWER	= '<p>' . stripslashes( htmlentities( $answer->message, ENT_QUOTES, 'UTF-8' ) ) . '</p>';

					//Showing the block to tempalte
					$template->block( 'USER' );

				}else{

					//Defining the class
					$template->CLASS	= 'resposta';

					//Defining the answer to template
					$template->ANSWER	= $answer->message;

					//Showing the block
					$template->block( 'CLIENT' );

				}

				//Showing the block to template
				$template->block( 'ANSWERS' );

			}

		}

		//Defining some variables to template
		$template->FORM				= $id;
		$template->DESTINATION		= $destination;
		$template->TOKEN			= $token;

		//Showing the block to template
		$template->block( 'NO_ERROR' );

	}else
		//Throwing the exception
		throw new Exception( 'Você não permissão para acessar essa página. Por favor, tente novamente mais tarde.' );

}catch( Exception $e ){}

// //Defining some variabels to template
$template->BASE_URL		= BASE_URL;
$template->BASE_URL_CSS	= BASE_URL_CSS;
$template->BASE_URL_IMG	= BASE_URL_IMG;
$template->BASE_URL_JS	= BASE_URL_JS;
$template->PROJECT_NAME	= PROJECT_NAME;

//Showing the page
$template->show();