<?php
/**
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @copyright   Copyright (C) 2016-2024 CMExension
 * @license     GNU General Public License version 2 or later
 */

defined('_JEXEC') or die;

use Joomla\CMS\Language\Text;
use Joomla\CMS\Toolbar\Toolbar;
use Joomla\CMS\Toolbar\ToolbarHelper;
use Joomla\CMS\Helper\ContentHelper;
use Joomla\CMS\MVC\View\HtmlView;

/**
 * View class for order list.
 *
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @since       1.0.0
 */
class CMGiftCardViewOrders extends HtmlView
{
	/**
	 * An array of items.
	 *
	 * @var  array
	 */
	protected $items;

	/**
	 * The pagination object.
	 *
	 * @var  Pagination
	 */
	protected $pagination;

	/**
	 * The model state.
	 *
	 * @var  object
	 */
	protected $state;

	/**
	 * Method to display the view.
	 *
	 * @param   string  $tpl  A template file to load. [optional]
	 *
	 * @return  mixed  A string if successful, otherwise an Error object.
	 *
	 * @since   1.0.0
	 */
	public function display($tpl = null)
	{
		$this->items			= $this->get('Items');
		$this->pagination		= $this->get('Pagination');
		$this->state			= $this->get('State');
		$this->filterForm		= $this->get('FilterForm');
		$this->activeFilters	= $this->get('ActiveFilters');
		$this->params			= $this->state->get('params');

		if (count($errors = $this->get('Errors')))
		{
			return CMGiftCardHelper::throwException(implode("\n", $errors), 500);
		}

		if (version_compare(JVERSION, '4.0.0-alpha1', '<'))
		{
			CMGiftCardHelper::addSubmenu('orders');
			$this->sidebar = \JHtmlSidebar::render();
			$this->setLayout('default3');
			$this->addToolbar3();
		}
		else
		{
			$this->addToolbar();
		}

		return parent::display($tpl);
	}

	/**
	 * Add the page title and toolbar.
	 *
	 * @return  void
	 *
	 * @since   1.0.0
	 */
	protected function addToolbar()
	{
		$canDo = ContentHelper::getActions('com_cmgiftcard');

		$toolbar = Toolbar::getInstance('toolbar');
		ToolbarHelper::title(Text::_('COM_CMGIFTCARD_MANAGER_ORDERS'), 'copy');

		if ($canDo->get('core.edit.state'))
		{
			$toolbar->checkin('orders.checkin')->listCheck(true);
		}

		if ($canDo->get('core.delete'))
		{
			$toolbar->delete('orders.delete')
				->message('COM_CMGIFTCARD_CONFIRM_DELETE_ORDER')
				->listCheck(true);
		}

		if ($canDo->get('core.admin') || $canDo->get('core.options'))
		{
			ToolbarHelper::preferences('com_cmgiftcard');
		}
	}

	/**
	 * Add the page title and toolbar for Joomla 3.
	 *
	 * @return  void
	 *
	 * @since   2.0.0-alpha1
	 */
	protected function addToolbar3()
	{
		$canDo = ContentHelper::getActions('com_cmgiftcard');

		ToolbarHelper::title(Text::_('COM_CMGIFTCARD_MANAGER_ORDERS'), 'copy');

		if ($canDo->get('core.edit'))
		{
			ToolbarHelper::editList('order.edit');
		}

		if ($canDo->get('core.edit.state'))
		{
			ToolbarHelper::checkin('orders.checkin');
		}

		if ($canDo->get('core.delete'))
		{
			ToolbarHelper::deleteList('COM_CMGIFTCARD_CONFIRM_DELETE_ORDER', 'orders.delete');
		}

		if ($canDo->get('core.admin') || $canDo->get('core.options'))
		{
			ToolbarHelper::preferences('com_cmgiftcard');
		}
	}

	/**
	 * Returns an array of fields the table can be sorted by
	 *
	 * @return  array  Array containing the field name to sort by as the key and display text as value
	 *
	 * @since   1.0.0
	 */
	protected function getSortFields()
	{
		return array(
			'a.ordering'	=> Text::_('JGRID_HEADING_ORDERING'),
			'a.published'	=> Text::_('JSTATUS'),
			'a.title'		=> Text::_('JGRID_HEADING_TITLE'),
			'a.id'			=> Text::_('JGRID_HEADING_ID')
		);
	}
}
