<?php
namespace App\Mvc\View\Listing;

class Simple extends GeneralAbstract
{
	/**
	 * The total number of records.
	 * @var Int
	 */
	protected $recordsCountTotal = 	0;

	/**
	 * The available number of results per page.
	 * @var array
	 */
	protected $resultsPerPageList = [5, 10, 15, 20];

	public function __construct(Array $records = [])
	{
		parent::__construct($records);

		$this->dataTemplate = 		'listingTable.tpl';
		$this->headerTemplate = 	\Theme::getInstance()->getPath().'templates/smartyExtends/content/list/table/simple/header.tpl';
		$this->containerTemplate = 	\Theme::getInstance()->getPath().'templates/smartyExtends/content/list/table/simple/index.tpl';
		$this->paginationTemplate = \Theme::getInstance()->getPath().'templates/smartyExtends/content/list/table/simple/pagination.tpl';
		$this->footerTemplate = 	\Theme::getInstance()->getPath().'templates/smartyExtends/content/list/table/simple/footer.tpl';
	}


	/**
	 * Sets the total number of records for the listing.
	 * @param Int $totalRecordsCount
	 */
	public function setRecordsCountTotal($totalRecordsCount)
	{
		$this->recordsCountTotal=$totalRecordsCount;
	}

	/**
	 * Returns the total number of records.
	 * @return Int
	 */
	public function getRecordsCountTotal()
	{
		return $this->recordsCountTotal;
	}


	/**
	 * Sets the results per page list of values.
	 * @param Array $rppl
	 */
	public function setResultsPerPageList(Array $rppl)
	{
		$this->resultsPerPageList=$rppl;
	}

	/**
	 * Returns the list of results per page values.
	 * @return Array.
	 */
	public function getResultsPerPageList()
	{
		return $this->resultsPerPageList;
	}

	public function renderPagination()
	{
		$pg = new \App\Mvc\View\Listing\Members\Pagination();

		if (!$this->getResultsPerPage()) {
			\Log::getInstance()->error(sprintf('[%s:%s] Results per page is not set.', __METHOD__, __LINE__));
		}

		$pg ->setCurrentPage($this->getCurrentPage())
			->setItemsPerPage($this->getResultsPerPage())
			->setTotalItems($this->getRecordsCountTotal())
			->setTotalItemsIsCached($this->getRecordsCountTotalIsCached())
			->setItemsPerPageList(array_combine($this->getResultsPerPageList(), $this->getResultsPerPageList()));

		$smarty = \Engine\Smarty\SmartyMultiton::getInstance();

		$smarty->assign('pagination', $pg);
		$output = $this->smarty_renderTemplate($this->paginationTemplate);
		$smarty->clearAssign('pagination');

		return $output;
	}

	public function renderJsonPagination()
	{
		$pg = new \App\Mvc\View\Listing\Members\Pagination();

		$pg ->setCurrentPage($this->getCurrentPage())
			->setItemsPerPage($this->getResultsPerPage())
			->setTotalItems($this->getRecordsCountTotal())
			->setTotalItemsIsCached($this->getRecordsCountTotalIsCached())
			->setItemsPerPageList(array_combine($this->getResultsPerPageList(), $this->getResultsPerPageList()));


		$jsonMessages = new \stdClass();
		$jsonMessages->currentPage = 			$this->getCurrentPage();
		$jsonMessages->resultsPerPage = 		$this->getResultsPerPage();
		$jsonMessages->totalResults = 			$this->getRecordsCountTotal();
		$jsonMessages->totalResultsForDisplay = $pg->getTotalItemsForDisplay();
		$jsonMessages->totalResultsIsCached = 	$this->getRecordsCountTotalIsCached();

		return $jsonMessages;
	}
}
