<?php
/**
 *------------------------------------------------------------------------------
 * @package       T4 Page Builder for Joomla!
 *------------------------------------------------------------------------------
 * @copyright     Copyright (C) 2004-2020 JoomlArt.com. All Rights Reserved.
 * @license       GNU General Public License version 2 or later; see LICENSE.txt
 * @authors       JoomlArt, JoomlaBamboo, (contribute to this project at github
 *                & Google group to become co-author)
 *------------------------------------------------------------------------------
 */

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri;

// Create shortcuts to some parameters.
$params = $this->params;
$user = Factory::getUser();
$item = $this->item;
$load_legacy = $this->loadlegacy;
$doc = Factory::getDocument();
$app = Factory::getApplication();
$preview = $app->input->get('preview');

?>
<div class="jpb-page" itemscope="itemscope" itemtype="https://schema.org/Article">

	<meta itemprop="inLanguage" content="<?php echo ($this->item->language === '*') ? Factory::getConfig()->get('language') : $this->item->language; ?>" />
	<?php if (!empty($item)): ?>
	<?php

$html = \JPB\Helper\Html::loadShareBlock($item->text);
$html = \JPB\Helper\Html::voidJdoc($html);
$html = \JPB\Helper\Html::renderJdoc($html);

// check load captcha on form
if (preg_match('/t4b-recaptcha/', $html, $matches, PREG_OFFSET_CAPTURE)) {
    if (version_compare(JVERSION, '4', 'ge')) {
        $apiSrc = 'https://www.google.com/recaptcha/api.js?onload=JoomlainitReCaptcha2&render=explicit&hl='
        . Factory::getLanguage()->getTag();

        // Load assets, the callback should be first
        $doc->getWebAssetManager()
            ->registerAndUseScript('plg_captcha_recaptcha', 'plg_captcha_recaptcha/recaptcha.min.js', [], ['defer' => true])
            ->registerAndUseScript('plg_captcha_recaptcha.api', $apiSrc, [], ['defer' => true], ['plg_captcha_recaptcha']);

    } else {
        $doc->addScript('https://www.google.com/recaptcha/api.js', 'text/javascript', true, true);
    }
}
// check load captcha on form
if (preg_match('/role="tablist"/', $html, $matches, PREG_OFFSET_CAPTURE)) {
    if (version_compare(JVERSION, '4', 'ge')) {
        // Load assets, the callback should be first
        $doc->getWebAssetManager()->useScript('bootstrap.tab');
    }
}
?>
	<?php // Content is generated by content plugin event "onContentBeforeDisplay"?>
	<?php echo $this->item->event->beforeDisplayContent; ?>
	<div class="content clearfix">
	<?php
		// Render builder head for front end
		echo $html;
	?>
	</div>
	<?php
// Clean ouput, remove builder metadata
\JPB\Helper\Render::cleanRespond();
?>
	<?php endif?>
	<?php if ($user->authorise('core.create', 'com_t4pagebuilder') && $user->authorise('core.edit', 'com_t4pagebuilder')): ?>
	<a href="#" class="btn btn-frimary t4b-edit-btn"><i class="fas fa-pencil-alt"></i>Live edit page</a>
	<?php endif;?>

	<?php // Content is generated by content plugin event "onContentAfterDisplay"?>
	<?php echo $this->item->event->afterDisplayContent; ?>
</div>
<script>
	var loadLegacy = "<?php echo $load_legacy; ?>";
	if(loadLegacy == '1'){
		jQuery("head").append('<link href="<?php echo Uri::base(true) ?>/media/t4pagebuilder/builder/css/legacy.css" rel="stylesheet" type="text/css">');
	}

	jQuery(document).ready(function($){
		<?php if ($preview): ?>
		if(window.parent.jQuery && window.parent.jQuery('#preview').length){
			var title = document.title;
			var preview_title = window.parent.jQuery('#preview').find('.t4b-preview-page-title');
			if(!window.parent.jQuery('#preview').data('view') && preview_title.length){
				preview_title.html('<i class="fal fa-file-alt"></i>'+title);
			}
			window.parent.jQuery('#preview').data('view',false);
			jQuery('.contentpane.modal').removeClass('modal');
		}
		<?php endif;?>
		var pageid = '<?php echo $item ? $item->id : ""; ?>';
		localStorage.setItem('pageid', pageid);
		$('.t4b-edit-btn').on('click',function(e){
			e.preventDefault();
			localStorage.setItem('editpage', true);
			localStorage.setItem('T4EDIT_VISITED', true);
			localStorage.setItem('T4EDIT_PAGE', window.location.href);
			window.location.href = "<?php echo Uri::base(true) . '/index.php?option=com_t4pagebuilder&view=page&layout=edit&id=' ?>"+pageid+'&inline=1';
		});
		window['t4b-animation'].init({
			easing: 'ease-out-back',
			duration: 1000
		});
	});
	<?php
if (!empty($this->item->js)) {
    echo $this->item->js;
}
?>
</script>