<?php
namespace My\Component\GrafikKeuangan\Administrator\Controller;

defined('_JEXEC') or die;

use Joomla\CMS\MVC\Controller\AdminController;

class ItemsController extends AdminController
{
	public function getModel($name = 'Item', $prefix = 'Administrator', $config = ['ignore_request' => true])
	{
		if (strtolower($name) === 'item') {
			return new \My\Component\GrafikKeuangan\Administrator\Model\ItemModel($config);
		}
		return parent::getModel($name, $prefix, $config);
	}

	public function trash()
	{
		// Manual handling since AdminController::delete might be hard deleting or misconfigured
		$ids = $this->input->get('cid', [], 'array');

		if (empty($ids)) {
			// No items selected
			$this->setRedirect(\Joomla\CMS\Router\Route::_('index.php?option=com_grafik_keuangan&view=items', false), \Joomla\CMS\Language\Text::_('JERROR_NO_ITEMS_SELECTED'), 'warning');
			return;
		}

		$model = $this->getModel();

		// -2 is the standard 'Trashed' state in Joomla
		if (!$model->publish($ids, -2)) {
			$this->setRedirect(\Joomla\CMS\Router\Route::_('index.php?option=com_grafik_keuangan&view=items', false), $model->getError(), 'error');
		} else {
			$this->setRedirect(\Joomla\CMS\Router\Route::_('index.php?option=com_grafik_keuangan&view=items', false), \Joomla\CMS\Language\Text::plural('JN_ITEMS_TRASHED', count($ids)));
		}
	}
}
