<?php

    /**
     * @version    CVS: 1.0.0
     * @package    Com_Dvtr_employees
     * @author     Pawel Potoniec <pawel@devatri.pl>
     * @copyright  2025 Pawel Potoniec
     */

    namespace Dvtremployees\Component\Dvtr_employees\Site\View\Employee;
    // No direct access
    defined( '_JEXEC' ) or die;

    use Exception;
    use JFactory;
    use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
    use Joomla\CMS\Factory;
    use Joomla\CMS\Language\Text;

    /**
     * View class for a list of Dvtr_employees.
     *
     * @since  1.0.0
     */
    class HtmlView extends BaseHtmlView {
        protected $state;

        protected $item;

        protected $form;

        protected $params;

        /**
         * Display the view
         *
         * @param string $tpl Template name
         *
         * @return void
         *
         * @throws Exception
         */
        public function display( $tpl = NULL ) {
            $app  = Factory::getApplication();
            $user = $app->getIdentity();

            $this->state  = $this->get( 'State' );
            $this->item   = $this->get( 'Item' );
            $this->params = $app->getParams( 'com_dvtr_employees' );

            $doc = JFactory::getDocument();
            $doc->addStylesheet( 'components/com_dvtr_employees/assets/grid.css' );
            $doc->addStylesheet( 'components/com_dvtr_employees/assets/style.css' );

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

            if ( $this->_layout == 'edit' ) {
                $authorised = $user->authorise( 'core.create', 'com_dvtr_employees' );

                if ( $authorised !== TRUE ) {
                    throw new Exception( Text::_( 'JERROR_ALERTNOAUTHOR' ) );
                }
            }

            $this->_prepareDocument();

            parent::display( $tpl );
        }

        /**
         * Prepares the document
         *
         * @return void
         *
         * @throws Exception
         */
        protected function _prepareDocument() {
            $app   = Factory::getApplication();
            $menus = $app->getMenu();
            $title = NULL;

            // Because the application sets a default page title,
            // We need to get it from the menu item itself
            $menu = $menus->getActive();

            if ( $menu ) {
                $this->params->def( 'page_heading', $this->params->get( 'page_title', $menu->title ) );
            } else {
                $this->params->def( 'page_heading', Text::_( 'COM_DVTR_EMPLOYEES_DEFAULT_PAGE_TITLE' ) );
            }

            $title = $this->item->title ?? $this->params->get( 'page_title', '' );

            if ( empty( $title ) ) {
                $title = $app->get( 'sitename' );
            } elseif ( $app->get( 'sitename_pagetitles', 0 ) == 1 ) {
                $title = Text::sprintf( 'JPAGETITLE', $app->get( 'sitename' ), $title );
            } elseif ( $app->get( 'sitename_pagetitles', 0 ) == 2 ) {
                $title = Text::sprintf( 'JPAGETITLE', $title, $app->get( 'sitename' ) );
            }

            $this->document->setTitle( $title );

            if ( $this->params->get( 'menu-meta_description' ) ) {
                $this->document->setDescription( $this->params->get( 'menu-meta_description' ) );
            }

            if ( $this->params->get( 'menu-meta_keywords' ) ) {
                $this->document->setMetadata( 'keywords', $this->params->get( 'menu-meta_keywords' ) );
            }

            if ( $this->params->get( 'robots' ) ) {
                $this->document->setMetadata( 'robots', $this->params->get( 'robots' ) );
            }

            // Add Breadcrumbs
            $pathway        = $app->getPathway();
            $breadcrumbList = Text::_( 'COM_DVTR_EMPLOYEES_TITLE_EMPLOYEES' );

            if ( ! in_array( $breadcrumbList, $pathway->getPathwayNames() ) ) {
                $pathway->addItem( $breadcrumbList, "index.php?option=com_dvtr_employees&view=employees" );
            }
            $breadcrumbTitle = Text::_( 'COM_DVTR_EMPLOYEES_TITLE_EMPLOYEE' );

            if ( ! in_array( $breadcrumbTitle, $pathway->getPathwayNames() ) ) {
                $pathway->addItem( $breadcrumbTitle );
            }
        }
    }
