<?php
/**
 * @package    pixelcms_articles
 *
 * @author     christophf <your@email.com>
 * @copyright  A copyright
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 * @link       http://your.url.com
 */

use Joomla\CMS\MVC\View\HtmlView;

defined('_JEXEC') or die;

/**
 * Pixelcms_articles view.
 *
 * @package  pixelcms_articles
 * @since    1.0
 */
class pixelcms_articlesViewtracktorpool_products extends HtmlView
{
    protected $ads;
    protected $locale;
    protected $relatedAds;
    protected $params;
    protected $is_module = false;

	function display($tpl = null)
	{
		// Assign data to the view
        $this->ads = $this->get('Data');
        $this->locale = $this->get('Locale');
		$this->params = $this->get('state')->get('params');

		$document = JFactory::getDocument();
		if (gettype($this->ads) === 'array') {
	        $tpl = 'listing';
	        $document->addScript(JURI::base(true) . '/media/com_pixelcms_articles/js/vue.min.js', [
		        'version' => '2.5.16',
	        ]);
        } else {
            $this->relatedAds = $this->get('RelatedAds');

            $ad = $this->ads;
            $title = implode(' ', [$ad->manufacturer->value, $ad->model]);
            $input = JFactory::getApplication()->input;
            $input->set('px-title', $title);
            $input->set('px-art-nr', $ad->dealer_internal_id ?? $ad->id);
        }

        $document->addScript(JURI::base(true) . '/media/com_pixelcms_articles/js/cssua.min.js', [
            'version' => md5_file(JPATH_BASE . '/media/com_pixelcms_articles/js/cssua.min.js'),
        ]);
		$document->addStyleSheet(JURI::base(true) . '/media/com_pixelcms_articles/css/fonts.css', [
            'version' => md5_file(JPATH_BASE . '/media/com_pixelcms_articles/css/fonts.css'),
        ]);
        $document->addStyleSheet(JURI::base(true) . '/media/com_pixelcms_articles/css/machine_details.css', [
            'version' => md5_file(JPATH_BASE . '/media/com_pixelcms_articles/css/machine_details.css'),
        ]);
        $document->addStyleSheet(JURI::base(true) . '/media/com_pixelcms_articles/css/style.css', [
        	'version' => md5_file(JPATH_BASE . '/media/com_pixelcms_articles/css/style.css'),
        ]);
        $document->addScript(JURI::base(true) . '/media/com_pixelcms_articles/js/script.js', [
	        'version' => md5_file(JPATH_BASE . '/media/com_pixelcms_articles/js/script.js'),
        ]);

		// Display the view
		parent::display($tpl);
	}

    protected function _setPath($type, $path)
    {
        $component = 'com_pixelcms_articles';
        $app = JFactory::getApplication();

        // Clear out the prior search dirs
        $this->_path[$type] = array();

        // Actually add the user-specified directories
        $this->_addPath($type, $path);

        // Always add the fallback directories as last resort
        switch (strtolower($type))
        {
            case 'template':
                // Set the alternative template search dir
                if (isset($app))
                {
                    $component = preg_replace('/[^A-Z0-9_\.-]/i', '', $component);
                    $fallback = JPATH_THEMES . '/' . $app->getTemplate() . '/html/' . $component . '/' . $this->getName();
                    $this->_addPath('template', $fallback);
                }
                break;
        }
    }
}
