<?php
declare(strict_types=1);

namespace Joomla\Component\AgendaPimpinan\Administrator\View\Items;

\defined('_JEXEC') or die;

use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Toolbar\Toolbar;
use Joomla\CMS\Toolbar\ToolbarHelper;

class HtmlView extends BaseHtmlView
{
    protected $items = [];

    public function display($tpl = null)
    {
        $this->items = $this->get('Items') ?: [];

        $this->addToolbar();

        return parent::display($tpl);
    }

    protected function addToolbar(): void
    {
        ToolbarHelper::title(Text::_('COM_AGENDA_PIMPINAN'), 'calendar');

        // FIX: Jangan pakai ToolbarHelper::addNew('item.add')
        // Karena pada kasus Anda, Joomla membentuk URL option salah (com_agendapimpinan).
        // Kita paksa jadi link eksplisit yang benar.
        $toolbar = Toolbar::getInstance('toolbar');
        $toolbar->appendButton(
            'Link',
            'new',
            Text::_('JNEW'),
            Route::_('index.php?option=com_agenda_pimpinan&view=item&layout=edit'),
            false
        );
    }
}
