<?php
/**
 * @package com_spmedical
 * @author JoomShaper http://www.joomshaper.com
 * @copyright Copyright (c) 2010 - 2025 JoomShaper
 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or later
 */

// No Direct Access
defined ('_JEXEC') or die('Restricted Access');

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

class SpmedicalViewDepartment extends HtmlView
{
	protected $form;
	protected $item;
	protected $canDo;
	protected $id;

	public function display($tpl = null)
	{
		// Get the Data
		$model = $this->getModel('Department');
		$this->form = $this->get('Form');
		$this->item = $this->get('Item');
		$this->id = $this->item->id;

		$this->canDo = ContentHelper::getActions('com_spmedical','component',$this->item->id);

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

		$this->addToolBar();
		parent::display($tpl);
	}

	protected function addToolBar()
	{
		$input 	= Factory::getApplication()->input;
		$userId = Factory::getUser()->id;

		// Hide Joomla Administrator Main menu
		$input->set('hidemainmenu', true);
		$isNew = ($this->item->id == 0);

		ToolbarHelper::title(Text::_('COM_SPMEDICAL_DEPARTMENT') . ': '.  ($isNew ? Text::_('COM_SPMEDICAL_NEW') : Text::_('COM_SPMEDICAL_EDIT')), 'pencil');

		if ($isNew)
		{
			// For new records, check the create permission.
			if ($this->canDo->get('core.create'))
			{
				ToolbarHelper::apply('department.apply', 'JTOOLBAR_APPLY');
				ToolbarHelper::save('department.save', 'JTOOLBAR_SAVE');
			}
			ToolbarHelper::cancel('department.cancel', 'JTOOLBAR_CANCEL');
		}
		else
		{
			if ($this->canDo->get('core.edit') || $this->canDo->get('core.edit.own') && $this->item->created_by == $userId )
			{
				// We can save the new record
				ToolbarHelper::apply('department.apply', 'JTOOLBAR_APPLY');
				ToolbarHelper::save('department.save', 'JTOOLBAR_SAVE');
			}
			ToolbarHelper::cancel('department.cancel', 'JTOOLBAR_CLOSE');
		}
	}
}
