<?php
/**
 * @package    Pwtseo
 *
 * @author     Perfect Web Team <extensions@perfectwebteam.com>
 * @copyright  Copyright (C) 2016 - 2025 Perfect Web Team. All rights reserved.
 * @license    GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 * @link       https://extensions.perfectwebteam.com
 */

use Joomla\CMS\Factory;
use Joomla\CMS\Form\FormField;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Session\Session;
use Joomla\CMS\Version;

defined('_JEXEC') or die;

/**
 * PWT SEO field - Structured Data
 * https://developers.google.com/search/docs/guides/intro-structured-data
 *
 * @since  1.3.0
 */
class PWTSeoFormFieldStructuredData extends FormField
{
	/**
	 * The form field type.
	 *
	 * @var    string
	 * @since  1.3.0
	 */
	public $type = 'structureddata';

	/**
	 * Method to get the field input markup.
	 *
	 * @return  string  The field input markup.
	 *
	 * @since   1.3.0
	 * @throws  Exception
	 */
	public function getInput()
	{
        $input   = Factory::getApplication()->input;
        $id      = $input->getInt('id', 0);
        $context = $input->getCmd('option') . '.' . $input->getCmd('view');

        /** @var Joomla\CMS\WebAsset\WebAssetManager $wa */
        $wa = Factory::getDocument()->getWebAssetManager();
        $wa->useScript('core')
            ->addInlineScript("  window.addEventListener('DOMContentLoaded', function () {
			    var versionsModal = document.getElementById('structureddataModal');

			    if (versionsModal) {
			      document.body.appendChild(versionsModal);
			    }
        });");

        echo HTMLHelper::_(
            'bootstrap.renderModal',
            'structureddataModal',
            [
                'url'    => 'index.php?' . http_build_query(
                        [
                            'option' => 'com_pwtseo',
                            'view' => 'structureddata',
                            'layout' => 'modal',
                            'tmpl' => 'component',
                            'context' => $context,
                            'context_id' => $id,
                            Session::getFormToken() => 1
                        ]
                    ),
                'title'      => Text::_('PLG_SYSTEM_PWTSEO_FORM_STRUCTUREDDATA_LABEL'),
                'height' => '100%',
                'width'  => '100%',
                'modalWidth' => '80',
                'bodyHeight' => '70',
                'footer'     => '<button type="button" class="btn btn-cancel" data-bs-dismiss="modal" aria-hidden="true">'
                    . JText::_('JLIB_HTML_BEHAVIOR_CLOSE') . '</button>'
                    . '<button type="button" class="btn btn-success" aria-hidden="true" onclick="jQuery(\'#structureddataModal iframe\').contents().find(\'#applyBtn\').click(); return false;">'
                    . JText::_("JAPPLY") . '</button>'
            ]
        );

        return '<button type="button" data-toggle="modal" data-bs-toggle="modal" data-bs-target="#structureddataModal" class="btn btn-primary">
			<span class="icon-list" aria-hidden="true"></span>' . Text::_('PLG_SYSTEM_PWTSEO_FORM_DATALAYERS_CUSTOMIZE_LABEL') . '</button>';
	}
}
