<?php
/** 
 * @package     VikRestaurants
 * @subpackage  core
 * @author      E4J s.r.l.
 * @copyright   Copyright (C) 2023 E4J s.r.l. All Rights Reserved.
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link        https://vikwp.com
 */

// No direct access
defined('ABSPATH') or die('No script kiddies please!');

/**
 * VikRestaurants room closures view.
 *
 * @since 1.5
 */
class VikRestaurantsViewroomclosures extends JViewVRE
{
	/**
	 * VikRestaurants view display method.
	 *
	 * @return 	void
	 */
	function display($tpl = null)
	{
		$app = JFactory::getApplication();
		$dbo = JFactory::getDbo();

		// set the toolbar
		$this->addToolBar();

		$filters = array();
		$filters['id_room'] = $app->getUserStateFromRequest($this->getPoolName() . '.id_room', 'id_room', 0, 'uint');
		$filters['repeat']  = $app->getUserStateFromRequest($this->getPoolName() . '.repeat', 'repeat', '', 'string');
		$filters['date']    = $app->getUserStateFromRequest($this->getPoolName() . '.date', 'date', '', 'string');

		$this->filters = $filters;

		$this->ordering = $app->getUserStateFromRequest($this->getPoolName() . '.ordering', 'filter_order', 'c.id', 'string');
		$this->orderDir = $app->getUserStateFromRequest($this->getPoolName() . '.orderdir', 'filter_order_Dir', 'DESC', 'string');

		$lim 	= $app->getUserStateFromRequest($this->getPoolName() . '.limit', 'limit', $app->get('list_limit'), 'int');
		$lim0 	= $this->getListLimitStart($filters + ['limit' => $lim]);
		$navbut	= "";

		$rows = array();

		$q = $dbo->getQuery(true)
			->select('SQL_CALC_FOUND_ROWS `c`.*')
			->select($dbo->qn('r.name'))
			->from($dbo->qn('#__vikrestaurants_room', 'r'))
			->rightjoin($dbo->qn('#__vikrestaurants_room_closure', 'c') . ' ON ' . $dbo->qn('r.id') . ' = ' . $dbo->qn('c.id_room'))
			->where(1)
			->order($dbo->qn($this->ordering) . ' ' . $this->orderDir);

		if ($filters['id_room'])
		{
			$q->where($dbo->qn('r.id') . ' = ' . $filters['id_room']);
		}

		if ($filters['repeat'])
		{
			$q->where($dbo->qn('c.repeat') . ' = ' . $dbo->q($filters['repeat']));
		}

		/**
		 * It is possible to lean on the "onBeforeListQueryRoomclosures" plugin event
		 * to manipulate the query used to load the list of records.
		 *
		 * @since 1.9
		 */
		$this->onBeforeListQuery($q);

		if (!E4J\VikRestaurants\Helpers\DateHelper::isNull($filters['date']))
		{
			$collection = E4J\VikRestaurants\Room\Closure\ClosuresCollection::getInstance();

			if ($filters['id_room'])
			{
				$collection = $collection->filter(new E4J\VikRestaurants\Room\Closure\Filters\RoomFilter($filters['id_room']));
			}

			$collection = $collection->filter(new E4J\VikRestaurants\Room\Closure\Filters\DateFilter($filters['date']));

			$ids = [];

			foreach ($collection as $closure)
			{
				// register all the matching closures
				$ids[] = $closure->get('id');
			}

			if ($ids)
			{
				// fetch all the matching closures by ID
				$q->andWhere($dbo->qn('c.id') . ' IN (' . implode(',', $ids) . ')');
			}
			else
			{
				// no matching closure, force to failure
				$q->andWhere(0);
			}
		}

		$dbo->setQuery($q, $lim0, $lim);
		$dbo->execute();

		// assert limit used for list query
		$this->assertListQuery($lim0, $lim);

		if ($dbo->getNumRows())
		{
			$rows = $dbo->loadAssocList();
			$dbo->setQuery('SELECT FOUND_ROWS();');
			jimport('joomla.html.pagination');
			$pageNav = new JPagination($dbo->loadResult(), $lim0, $lim);
			$navbut = JLayoutHelper::render('blocks.pagination', ['pageNav' => $pageNav]);
		}
		
		$this->rows   = $rows;
		$this->navbut = $navbut;
		
		// display the template (default.php)
		parent::display($tpl);
	}

	/**
	 * Setting the toolbar.
	 *
	 * @return 	void
	 */
	private function addToolBar()
	{
		// add menu title and some buttons to the page
		JToolbarHelper::title(JText::translate('VRMAINTITLEVIEWROOMCLOSURES'), 'vikrestaurants');

		$user = JFactory::getUser();

		JToolbarHelper::back('JTOOLBAR_BACK', 'index.php?option=com_vikrestaurants&view=rooms');
		
		if ($user->authorise('core.create', 'com_vikrestaurants'))
		{
			JToolbarHelper::addNew('roomclosure.add');
		}

		if ($user->authorise('core.edit', 'com_vikrestaurants'))
		{
			JToolbarHelper::editList('roomclosure.edit');
		}

		if ($user->authorise('core.delete', 'com_vikrestaurants'))
		{
			JToolbarHelper::deleteList(VikRestaurants::getConfirmSystemMessage(), 'roomclosure.delete');
		}
	}

	/**
	 * Checks for advanced filters set in the request.
	 *
	 * @return 	boolean  True if active, otherwise false.
	 *
	 * @since 	1.9
	 */
	protected function hasFilters()
	{
		return $this->filters['id_room'] || $this->filters['repeat'] || $this->filters['date'];
	}

	/**
	 * Detects the status of the current room closure.
	 * 
	 * @param   array   $closure  The room closure.
	 * 
	 * @return  string  The closure status (confirmed, pending, removed).
	 * 
	 * @since   1.10
	 */
	protected function detectStatus(array $closure)
	{
		$now = VikRestaurants::now();

		if ($closure['stop'] > 0 && $closure['stop'] < $now)
		{
			// the closure has been stopped
			return 'removed';
		}

		if (!$closure['repeat'] && $closure['stop'] <= 0 && $closure['end_ts'] < $now)
		{
			// closure has expired
			return 'removed';
		}

		if ($closure['start_ts'] > $now)
		{
			// closure not yet started
			return 'pending';
		}

		// the closure owns an active recurrence or matching the current date
		return 'confirmed';
	}
}
