<?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 gift card code list.
 *
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @since       1.0.0
 */
class CMGiftCardViewCodes extends HtmlView
{
	/**
	 * An array of items.
	 *
	 * @var  array
	 */
	protected $items;

	/**
	 * The pagination object.
	 *
	 * @var  JPagination
	 */
	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 a JError 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('codes');
			$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_CODES'), 'copy');

		if ($canDo->get('core.create'))
		{
			$toolbar->addNew('code.add');
		}

		if ($canDo->get('core.edit.state') || $canDo->get('core.delete'))
		{
			$dropdown = $toolbar->dropdownButton('status-group')
				->text('JTOOLBAR_CHANGE_STATUS')
				->toggleSplit(false)
				->icon('fas fa-ellipsis-h')
				->buttonClass('btn btn-action')
				->listCheck(true);

			$childBar = $dropdown->getChildToolbar();

			if ($canDo->get('core.edit.state'))
			{
				$childBar
					->publish('codes.publish', 'JTOOLBAR_PUBLISH', true)
					->listCheck(true);

				$childBar
					->unpublish('codes.unpublish', 'JTOOLBAR_UNPUBLISH', true)
					->listCheck(true);

				$childBar
					->checkin('codes.checkin')
					->listCheck(true);
			}

			if ($canDo->get('core.delete'))
			{
				$childBar->delete('codes.delete')
					->message('JGLOBAL_CONFIRM_DELETE')
					->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_CODES'), 'copy');

		if ($canDo->get('core.create'))
		{
			ToolbarHelper::addNew('code.add');
		}

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

		if ($canDo->get('core.edit.state'))
		{
			ToolbarHelper::publish('codes.publish', 'JTOOLBAR_PUBLISH', true);
			ToolbarHelper::unpublish('codes.unpublish', 'JTOOLBAR_UNPUBLISH', true);
			ToolbarHelper::checkin('codes.checkin');
		}

		if ($canDo->get('core.delete'))
		{
			ToolbarHelper::deleteList('JGLOBAL_CONFIRM_DELETE', 'codes.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')
		);
	}
}
