<?php
/** 
 * @package     VikRestaurants
 * @subpackage  core
 * @author      E4J s.r.l.
 * @copyright   Copyright (C) 2023 E4J s.r.l. All Rights Reserved.
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link        https://vikwp.com
 */

// No direct access
defined('ABSPATH') or die('No script kiddies please!');

$vik = VREApplication::getInstance();

?>

<style>
	.cron-buttons-tags button {
		margin: 4px 0;
	}
</style>

<!-- PARAMETERS -->

<div class="vikpayparamdiv">
	<?php
	if ($this->config)
	{
		// display cron job driver parameters
		echo JLayoutHelper::render('form.fields', [
			'fields' => $this->config,
			'params' => $this->cron->params,
			'prefix' => 'cronform_',
		]);
	}
	else
	{
		echo $vik->alert(JText::translate('VRMANAGEPAYMENT9'));
	}
	?>
</div>

<script>
	(function($) {
		'use strict';

		$(function() {
			// render select
			$('.vikpayparamdiv select').each(function() {
				let option = $(this).find('option').first();

				let data = {
					// disable search for select with 3 or lower options
					minimumResultsForSearch: $(this).find('option').length > 3 ? 0 : -1,
					// allow clear selection in case the value of the first option is empty
					allowClear: option.val() || $(this).hasClass('required') ? false : true,
					// take the whole space
					width: '90%',
				};

				if (!option.val()) {
					// set placeholder by using the option text
					data.placeholder = option.text() || Joomla.JText._('JGLOBAL_SELECT_AN_OPTION');
					// unset the text from the option for a correct rendering
					option.text('');
				}

				$(this).select2(data);
			});

			// insert tags when clicking the related placeholders
			$(document).on('click', '.cron-buttons-tags button[data-target]', function() {
				const target = $(this).data('target');

				// extract tag to insert from button text
				let tag = $(this).text().trim();

				const area = Joomla.editors.instances['cronform_' + target];

				if (!area) {
					return false;
				}

				// wrap the tag into a span to support a sort of highlighting,
				// but only if TinyMCE is supported
				if (typeof tinymce !== 'undefined') {
					tag = '<span class="highlight-tag">' + tag + '</span>'
				}

				// in case of no selected text, auto append a white-space
				if (!area.getSelection()) {
					tag += ' ';
				}

				area.replaceSelection(tag);
			});
		});
	})(jQuery);
</script>