<?php
namespace ModulesAbstract\ElectronicInvoice\EntityQueueListing;

abstract class Controller extends \App\Mvc\Controller\Window\PaginatedListing\ListingAbstract
{
	// set list of columns with support ordering
	protected $listOrderingColumns = ['addTime', 'lastUpdateTime', 'status', ];

	protected $orderColumn = 'addTime';

	/**
	 * Define if the entity execution use or not a transaction
	 * @var Boolean
	 */
	protected $useSqlTransaction = true;

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing::initialize()
	 */
	protected function initialize()
	{
		parent::initialize();

		$this->actionsCfg['delete'] = [];
		$this->actionsCfg['openServerLogs'] =    	['checkSection' => true, 'checkOption' => false,];
		$this->actionsCfg['openCronjobLogs'] =    	['checkSection' => true, 'checkOption' => false,];
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\ListingAbstract::optionDefineParams()
	 */
	protected function optionDefineParams(\App\Mvc\View\Utils\RowOptions\Option\Option $option, $selection)
	{
		switch ($option->getId()) {
			case 'delete':
			break;

			// these actions are always available
			case 'openServerLogs':
			break;

			case 'openCronjobLogs':
			break;


			default:
				return false;
		}

		return true;
	}


	/**
	 * Get the instantiated DB entity
	 * @param Int $id
	 * @return \App\Entity\SelectAbstract
	 */
	abstract protected function selectEntity($id);

	/**
	 * Get the list of statuses
	 *
	 * @return array
	 */
	abstract public function getAllStatuses();

	/**
	 * Open server logs.
	 *
	 * @param array $R The input array.
	 * @return \Engine\Mvc\Response\HtmlRedirect The HTML redirect response.
	 */
	protected function actionOpenServerLogs(array $R)
	{
		$queueItem = $this->selectEntity($R['id']);

		$filters = $this->actionOpenServerLogsGenerateFilters($queueItem);
		$filters['listing[paramsSlot]'] =  \Engine\RandomGenerator::randomString(4);
		return new \Engine\Mvc\Response\HtmlRedirect(\Engine\Mvc\Dispatcher::getInstance()->getAppUrl('Modules/Admin/Server/Logs', 'index', $filters));
	}


	abstract protected function actionOpenServerLogsGenerateFilters(\ModulesAbstract\ElectronicInvoice\EntityQueue\QueueItem $queueItem);


	/**
	 * Open cronjob logs.
	 *
	 * @param array $R The input array.
	 * @return \Engine\Mvc\Response\HtmlRedirect The HTML redirect response.
	 */
	protected function actionOpenCronjobLogs(array $R)
	{
		$queueItem = $this->selectEntity($R['id']);

		$filters = $this->actionOpenCronjobLogsGenerateFilters($queueItem);
		$filters['listing[paramsSlot]'] =  \Engine\RandomGenerator::randomString(4);
		return new \Engine\Mvc\Response\HtmlRedirect(\Engine\Mvc\Dispatcher::getInstance()->getAppUrl('Modules/Admin/CronJobs/Logs', 'index', $filters));
	}

	abstract protected function actionOpenCronjobLogsGenerateFilters(\ModulesAbstract\ElectronicInvoice\EntityQueue\QueueItem $queueItem);

	abstract protected function bulkActionDelete(array $recordsIds, \App\MessagesCollector $msgs, array $R);

	/**
	 * Entity name
	 * @param Int $id
	 */
	protected function optionGetEntityName($id)
	{
		return $id;
	}
}