<?php
namespace My\Component\GrafikKeuangan\Administrator\View\Items;

defined('_JEXEC') or die;

use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
use Joomla\CMS\Toolbar\ToolbarHelper;

class HtmlView extends BaseHtmlView
{
	protected $items;
	protected $pagination;
	protected $state;
	public $filterForm;
	public $activeFilters;

	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');

		// Check for errors.
		$errors = $this->get('Errors');
		if (!empty($errors) && is_array($errors) && count($errors)) {
			throw new \Exception(implode("\n", $errors), 500);
		}

		$this->addToolbar();

		return parent::display($tpl);
	}

	protected function addToolbar()
	{
		$canDo = \Joomla\CMS\Helper\ContentHelper::getActions('com_grafik_keuangan');

		ToolbarHelper::title('COM_GRAFIK_KEUANGAN_ITEMS_TITLE', 'bars');

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

		if ($canDo && $canDo->get('core.edit.state')) {
			ToolbarHelper::publish('items.publish', 'JTOOLBAR_PUBLISH', true);
			ToolbarHelper::unpublish('items.unpublish', 'JTOOLBAR_UNPUBLISH', true);
			// ToolbarHelper::archive('items.archive'); // Deprecated in J4/J5
			ToolbarHelper::checkin('items.checkin');
		}

		$state = $this->get('State');
		if ($state && $state->get('filter.state') == -2 && $canDo && $canDo->get('core.delete')) {
			ToolbarHelper::deleteList('JGLOBAL_CONFIRM_DELETE', 'items.delete', 'JTOOLBAR_EMPTY_TRASH');
		} elseif ($canDo && $canDo->get('core.edit.state')) {
			ToolbarHelper::trash('items.trash');
		}

		// Options button.
		if ($canDo && ($canDo->get('core.admin') || $canDo->get('core.options'))) {
			ToolbarHelper::preferences('com_grafik_keuangan');
		}
	}
}
