<?php
/**
 * @package     VikRentItems
 * @subpackage  com_vikrentitems
 * @author      Alessio Gaggii - e4j - Extensionsforjoomla.com
 * @copyright   Copyright (C) 2018 e4j - Extensionsforjoomla.com. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE
 * @link        https://vikwp.com
 */

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

class VikRentItemsHelper
{
	public static function printHeader($highlight = "")
	{
		$app = JFactory::getApplication();
		$cookie = $app->input->cookie;
		$tmpl = VikRequest::getVar('tmpl');
		$view = VikRequest::getVar('view');

		if ($tmpl == 'component') {
			return;
		}

		if (VRIPlatformDetection::isWordPress()) {
			/**
			 * @wponly Hide menu for Pro-update views
			 */
			if (in_array($view, ['getpro'])) {
				return;
			}
		}

		$session   = JFactory::getSession();
		$adim_user = JFactory::getUser();

		$backlogo            = VikRentItems::getBackendLogo();
		$vri_auth_global     = $adim_user->authorise('core.vri.global', 'com_vikrentitems');
		$vri_auth_items      = $adim_user->authorise('core.vri.items', 'com_vikrentitems');
		$vri_auth_prices     = $adim_user->authorise('core.vri.prices', 'com_vikrentitems');
		$vri_auth_orders     = $adim_user->authorise('core.vri.orders', 'com_vikrentitems');
		$vri_auth_gsettings  = $adim_user->authorise('core.vri.gsettings', 'com_vikrentitems');
		$vri_auth_management = $adim_user->authorise('core.vri.management', 'com_vikrentitems');
		
		// check for stored quick actions only once
		$admin_menu_actions_checked = $session->get('admin_menu.actions.check', null, 'vikrentitems');
		if (!$admin_menu_actions_checked) 
		{
			$session->set('admin_menu.actions.check', 1, 'vikrentitems');
		}
		?>
		
		<div class="vri-menu-container<?php echo $view == 'dashboard' ? ' vri-menu-container-closer' : ''; ?>">
			<div class="vri-menu-left">
				<a href="index.php?option=com_vikrentitems"><img src="<?php echo VRI_ADMIN_URI . (!empty($backlogo) ? 'resources/'.$backlogo : 'vikrentitems.png'); ?>" alt="VikRentItems Logo" /></a>
			</div>
			<div class="vri-menu-right">
				<ul class="vri-menu-ul">
					<?php
					if ($vri_auth_prices || $vri_auth_gsettings) 
					{
					?><li class="vri-menu-parent-li">
						<span><?php VikRentItemsIcons::e('truck-moving'); ?> <a href="javascript: void(0);"><?php echo JText::translate('VRMENUONE'); ?><?php VikrentitemsIcons::e('chevron-down', 'vri-submenu-chevron'); ?></a></span>
						<div class="vri-submenu-wrap">
							<ul class="vri-submenu-ul" data-menu-scope="rental">
								<li>
									<div class="<?php echo ($highlight == "2" ? "vmenulinkactive" : "vmenulink"); ?>">
										<a href="index.php?option=com_vikrentitems&amp;task=iva">
											<?php VikrentitemsIcons::e('percent'); ?>
											<span class="vri-submenu-item-txt">
												<span class="vri-submenu-item-txt"><?php echo JText::translate('VRMENUNINE'); ?></span>
											</span>
										</a>
									</div>
								</li>
								<li>
									<div class="<?php echo ($highlight == "1" ? "vmenulinkactive" : "vmenulink"); ?>">
										<a href="index.php?option=com_vikrentitems&amp;task=prices">
											<?php VikrentitemsIcons::e('tags'); ?>
											<span class="vri-submenu-item">
												<span class="vri-submenu-item-txt"><?php echo JText::translate('VRMENUFIVE'); ?></span>
											</span>
										</a>
									</div>
								</li>
								<li>
									<div class="<?php echo ($highlight=="3" ? "vmenulinkactive" : "vmenulink"); ?>">
										<a href="index.php?option=com_vikrentitems&amp;task=places">
											<?php VikrentitemsIcons::e('map-marker'); ?>
											<span class="vri-submenu-item">
												<span class="vri-submenu-item-txt"><?php echo JText::translate('VRMENUTENTHREE'); ?></span>
											</span>
										</a>
									</div>
								</li>
								<li>
									<div class="<?php echo ($highlight=="timeslots" ? "vmenulinkactive" : "vmenulink"); ?>">
										<a href="index.php?option=com_vikrentitems&amp;task=timeslots">
											<?php VikrentitemsIcons::e('clock'); ?>
											<span class="vri-submenu-item">
												<span class="vri-submenu-item-txt"><?php echo JText::translate('VRMENUTIMESLOTS'); ?></span>
											</span>
										</a>
									</div>
								</li>
								<li>
									<div class="<?php echo ($highlight=="restrictions" ? "vmenulinkactive" : "vmenulink"); ?>">
										<a href="index.php?option=com_vikrentitems&amp;task=restrictions">
											<?php VikrentitemsIcons::e('hand-paper'); ?>
											<span class="vri-submenu-item">
												<span class="vri-submenu-item-txt"><?php echo JText::translate('VRMENURESTRICTIONS'); ?></span>
											</span>
										</a>
									</div>
								</li>
							</ul>
						</div>
					</li><?php
					}
					if ($vri_auth_items) 
					{
					?>
					<li class="vri-menu-parent-li">
						<span><?php VikRentItemsIcons::e('layer-group'); ?> <a href="javascript: void(0);"><?php echo JText::translate('VRMENUTWO'); ?><?php VikrentitemsIcons::e('chevron-down', 'vri-submenu-chevron'); ?></a></span>
						<div class="vri-submenu-wrap">
							<ul class="vri-submenu-ul" data-menu-scope="items">
								<li>
									<div class="<?php echo ($highlight=="4" ? "vmenulinkactive" : "vmenulink"); ?>">
										<a href="index.php?option=com_vikrentitems&amp;task=categories">
											<?php VikrentitemsIcons::e('filter'); ?>
											<span class="vri-submenu-item">
												<span class="vri-submenu-item-txt"><?php echo JText::translate('VRMENUSIX'); ?></span>
											</span>
										</a>
									</div>
								</li>
								<li>
									<div class="<?php echo ($highlight=="6" ? "vmenulinkactive" : "vmenulink"); ?>">
										<a href="index.php?option=com_vikrentitems&amp;task=optionals">
											<?php VikrentitemsIcons::e('couch'); ?>
											<span class="vri-submenu-item">
												<span class="vri-submenu-item-txt"><?php echo JText::translate('VRMENUTENFIVE'); ?></span>
											</span>
										</a>
									</div>
								</li>
								<li>
									<div class="<?php echo ($highlight=="5" ? "vmenulinkactive" : "vmenulink"); ?>">
										<a href="index.php?option=com_vikrentitems&amp;task=carat">
											<?php VikrentitemsIcons::e('icons'); ?>
											<span class="vri-submenu-item">
												<span class="vri-submenu-item-txt"><?php echo JText::translate('VRMENUTENFOUR'); ?></span>
											</span>
										</a>
									</div>
								</li>
									<!-- TO FIND THE RIGHT ICONS -->
								<li>
									<div class="<?php echo ($highlight=="7" ? "vmenulinkactive" : "vmenulink"); ?>">
										<a href="index.php?option=com_vikrentitems&amp;task=items">
											<?php VikrentitemsIcons::e('cube'); ?>
											<span class="vri-submenu-item">
												<span class="vri-submenu-item-txt"><?php echo JText::translate('VRMENUTEN'); ?></span>
											</span>
										</a>
									</div>
								</li>
									<!-- TO FIND THE RIGHT ICONS -->
								<li>
									<div class="<?php echo ($highlight=="relations" ? "vmenulinkactive" : "vmenulink"); ?>">
										<a href="index.php?option=com_vikrentitems&amp;task=relations">
											<?php VikrentitemsIcons::e('sync'); ?>
											<span class="vri-submenu-item">
												<span class="vri-submenu-item-txt"><?php echo JText::translate('VRMENURELATIONS'); ?></span>
											</span>
										</a>
									</div>
								</li>
							</ul>
						</div>
					</li><?php
					}
					if ($vri_auth_prices) 
					{
					?><li class="vri-menu-parent-li">
						<span><?php VikRentItemsIcons::e('calculator'); ?> <a href="javascript: void(0);"><?php echo JText::translate('VRIMENUFARES'); ?><?php VikrentitemsIcons::e('chevron-down', 'vri-submenu-chevron'); ?></a></span>
						<div class="vri-submenu-wrap">
							<ul class="vri-submenu-ul" data-menu-scope="pricing">
								<li>
									<div class="<?php echo ($highlight=="fares" ? "vmenulinkactive" : "vmenulink"); ?>">
										<a href="index.php?option=com_vikrentitems&amp;task=tariffs">
											<?php VikrentitemsIcons::e('toolbox'); ?>
											<span class="vri-submenu-item">
												<span class="vri-submenu-item-txt"><?php echo JText::translate('VRIMENUPRICESTABLE'); ?></span>
											</span>
										</a>
									</div>
								</li>
								<li>
									<div class="<?php echo ($highlight=="13" ? "vmenulinkactive" : "vmenulink"); ?>">
										<a href="index.php?option=com_vikrentitems&amp;task=seasons">
											<?php VikrentitemsIcons::e('seedling'); ?>
											<span class="vri-submenu-item">
												<span class="vri-submenu-item-txt"><?php echo JText::translate('VRMENUTENSEVEN'); ?></span>
											</span>
										</a>
									</div>
								</li>
								<li>
									<div class="<?php echo ($highlight=="12" ? "vmenulinkactive" : "vmenulink"); ?>">
										<a href="index.php?option=com_vikrentitems&amp;task=locfees">
											<?php VikrentitemsIcons::e('globe'); ?>
											<span class="vri-submenu-item">
												<span class="vri-submenu-item-txt"><?php echo JText::translate('VRMENUTENSIX'); ?></span>
											</span>
										</a>
									</div>
								</li>
									<!-- TO FIND THE RIGHT ICONS -->
								<li>
									<div class="<?php echo ($highlight=="discounts" ? "vmenulinkactive" : "vmenulink"); ?>">
										<a href="index.php?option=com_vikrentitems&amp;task=discounts">
											<?php VikrentitemsIcons::e('tag'); ?>
											<span class="vri-submenu-item">
												<span class="vri-submenu-item-txt"><?php echo JText::translate('VRIMENUDISCOUNTS'); ?></span>
											</span>
										</a>
									</div>
								</li>
									<!-- TO FIND THE RIGHT ICONS -->
								<li>
									<div class="<?php echo ($highlight=="ratesoverv" ? "vmenulinkactive" : "vmenulink"); ?>">
										<a href="index.php?option=com_vikrentitems&amp;task=ratesoverv">
											<?php VikrentitemsIcons::e('calculator'); ?>
											<span class="vri-submenu-item">
												<span class="vri-submenu-item-txt"><?php echo JText::translate('VRIMENURATESOVERVIEW'); ?></span>
											</span>
										</a>
									</div>
								</li>
							</ul>
						</div>
					</li><?php
					}
					if ($vri_auth_orders) {
					?><li class="vri-menu-parent-li">
						<span><?php VikRentItemsIcons::e('calendar-check'); ?> <a href="javascript: void(0);"><?php echo JText::translate('VRMENUTHREE'); ?><?php VikrentitemsIcons::e('chevron-down', 'vri-submenu-chevron'); ?></a></span>
						<div class="vri-submenu-wrap">
						<ul class="vri-submenu-ul" data-menu-scope="orders">
								<li>
									<div class="<?php echo ($highlight=="8" ? "vmenulinkactive" : "vmenulink"); ?>">
										<a href="index.php?option=com_vikrentitems&amp;task=orders">
											<?php VikrentitemsIcons::e('clipboard-list'); ?>
											<span class="vri-submenu-item">
												<span class="vri-submenu-item-txt"><?php echo JText::translate('VRMENUSEVEN'); ?></span>
											</span>
										</a>
									</div>
								</li>
								<li>
									<div class="<?php echo ($highlight=="19" ? "vmenulinkactive" : "vmenulink"); ?>">
										<a href="index.php?option=com_vikrentitems&amp;task=calendar">
											<?php VikrentitemsIcons::e('calendar'); ?>
											<span class="vri-submenu-item">
												<span class="vri-submenu-item-txt"><?php echo JText::translate('VRIMENUQUICKRES'); ?></span>
											</span>
										</a>
									</div>
								</li>
								<li>
									<div class="<?php echo ($highlight=="15" ? "vmenulinkactive" : "vmenulink"); ?>">
										<a href="index.php?option=com_vikrentitems&amp;task=overv">
											<?php VikrentitemsIcons::e('calendar-check'); ?>
											<span class="vri-submenu-item">
												<span class="vri-submenu-item-txt"><?php echo JText::translate('VRMENUTENNINE'); ?></span>
											</span>
										</a>
									</div>
								</li>
								<li>
									<div class="<?php echo ($highlight=="18" ? "vmenulinkactive" : "vmenulink"); ?>">
										<a href="index.php?option=com_vikrentitems">
											<?php VikrentitemsIcons::e('concierge-bell'); ?>
											<span class="vri-submenu-item">
												<span class="vri-submenu-item-txt"><?php echo JText::translate('VRIMENUDASHBOARD'); ?></span>
											</span>
										</a>
									</div>
								</li>
							</ul>
						</div>
					</li><?php
					}
					if ($vri_auth_management)
					{
					?><li class="vri-menu-parent-li">
						<span><?php VikRentItemsIcons::e('chart-line'); ?> <a href="javascript: void(0);"><?php echo JText::translate('VRIMENUMANAGEMENT'); ?><?php VikrentitemsIcons::e('chevron-down', 'vri-submenu-chevron'); ?></a></span>
						<div class="vri-submenu-wrap">
						<ul class="vri-submenu-ul" data-menu-scope="management">
								<li>
									<div class="<?php echo ($highlight=="customers" ? "vmenulinkactive" : "vmenulink"); ?>">
										<a href="index.php?option=com_vikrentitems&amp;task=customers">
											<?php VikrentitemsIcons::e('users'); ?>
											<span class="vri-submenu-item">
												<span class="vri-submenu-item-txt"><?php echo JText::translate('VRIMENUCUSTOMERS'); ?></span>
											</span>
										</a>
									</div>
								</li>
								<li>
									<div class="<?php echo ($highlight=="17" ? "vmenulinkactive" : "vmenulink"); ?>">
										<a href="index.php?option=com_vikrentitems&amp;task=coupons">
											<?php VikrentitemsIcons::e('user-tag'); ?>
											<span class="vri-submenu-item">
												<span class="vri-submenu-item-txt"><?php echo JText::translate('VRIMENUCOUPONS'); ?></span>
											</span>
										</a>
									</div>
								</li>
								<li>
									<div class="<?php echo ($highlight=="10" ? "vmenulinkactive" : "vmenulink"); ?>">
										<a href="index.php?option=com_vikrentitems&amp;task=stats">
											<?php VikrentitemsIcons::e('chart-line'); ?>
											<span class="vri-submenu-item">
												<span class="vri-submenu-item-txt"><?php echo JText::translate('VRMENUEIGHT'); ?></span>
											</span>
										</a>
									</div>
								</li>
								<li>
									<div class="<?php echo (in_array($highlight, ["crons", "managecron"]) ? "vmenulinkactive" : "vmenulink"); ?>">
										<a href="index.php?option=com_vikrentitems&amp;view=crons">
											<?php VikrentitemsIcons::e('stopwatch'); ?>
											<span class="vri-submenu-item">
												<span class="vri-submenu-item-txt"><?php echo JText::translate('VRIMENUCRONS'); ?></span>
											</span>
										</a>
									</div>
								</li>
							</ul>
						</div>
					</li><?php
					}
					if ($vri_auth_gsettings) {
					?><li class="vri-menu-parent-li">
						<span><?php VikRentItemsIcons::e('cogs'); ?> <a href="javascript: void(0);"><?php echo JText::translate('VRMENUFOUR'); ?><?php VikrentitemsIcons::e('chevron-down', 'vri-submenu-chevron'); ?></a></span>
						<div class="vri-submenu-wrap">
						<ul class="vri-submenu-ul" data-menu-scope="global">
								<li>
									<div class="<?php echo ($highlight=="11" ? "vmenulinkactive" : "vmenulink"); ?>">
										<a href="index.php?option=com_vikrentitems&amp;task=config">
											<?php VikrentitemsIcons::e('cogs'); ?>
											<span class="vri-submenu-item">
												<span class="vri-submenu-item-txt"><?php echo JText::translate('VRMENUTWELVE'); ?></span>
											</span>
										</a>
									</div>
								</li>
								<li>
									<div class="<?php echo ($highlight=="20" ? "vmenulinkactive" : "vmenulink"); ?>">
										<a href="index.php?option=com_vikrentitems&amp;task=translations">
											<?php VikrentitemsIcons::e('language'); ?>
											<span class="vri-submenu-item">
												<span class="vri-submenu-item-txt"><?php echo JText::translate('VRMENUTRANSLATIONS'); ?></span>
											</span>
										</a>
									</div>
								</li>
								<li>
									<div class="<?php echo ($highlight=="14" ? "vmenulinkactive" : "vmenulink"); ?>">
										<a href="index.php?option=com_vikrentitems&amp;task=payments">
											<?php VikrentitemsIcons::e('credit-card'); ?>
											<span class="vri-submenu-item">
												<span class="vri-submenu-item-txt"><?php echo JText::translate('VRMENUTENEIGHT'); ?></span>
											</span>
										</a>
									</div>
								</li>
								<li>
									<div class="<?php echo ($highlight=="16" ? "vmenulinkactive" : "vmenulink"); ?>">
										<a href="index.php?option=com_vikrentitems&amp;task=customf">
											<?php VikrentitemsIcons::e('address-card'); ?>
											<span class="vri-submenu-item">
												<span class="vri-submenu-item-txt"><?php echo JText::translate('VRMENUTENTEN'); ?></span>
											</span>
										</a>
									</div>
								</li>
							</ul>
						</div>
					</li><?php
					}
					?>
				</ul>
				<div class="vri-menu-updates">
				<?php
			if (VRIPlatformDetection::isWordPress()) {
				/**
				 * @wponly PRO Version
				 */
				VikrentitemsLoader::import('update.license');
				if (!VikrentitemsLicense::isPro()) {
					?>
						<button type="button" class="vri-gotopro" title="<?php echo addslashes(JText::translate('VRIGOTOPROBTN')); ?>" onclick="document.location.href='admin.php?option=com_vikrentitems&view=gotopro';">
							<?php VikrentitemsIcons::e('rocket'); ?>
							<span><?php echo JText::translate('VRIGOTOPROBTN'); ?></span>
						</button>
					<?php
				} else {
					?>
						<button type="button" class="vri-alreadypro" title="<?php echo addslashes(JText::translate('VRIISPROBTN')); ?>" onclick="document.location.href='admin.php?option=com_vikrentitems&view=gotopro';">
							<?php VikrentitemsIcons::e('trophy'); ?>
							<span><?php echo JText::translate('VRIISPROBTN'); ?></span>
						</button>
					<?php
				}
			} else {
				/**
				 * @joomlaonly
				 */
				if (($highlight == '18' || $highlight == '11') && method_exists($app, 'triggerEvent')) {
					// VikUpdater
					JPluginHelper::importPlugin('e4j');
					$callable = $app->triggerEvent('onUpdaterSupported');
					if (count($callable) && $callable[0]) {
						//Plugin enabled
						$params = new stdClass;
						$params->version 	= E4J_SOFTWARE_VERSION;
						$params->alias 		= 'com_vikrentitems';
						
						$upd_btn_text = strrev('setadpU kcehC');
						$ready_jsfun = '';
						$result = $app->triggerEvent('onGetVersionContents', array(&$params));
						if (count($result) && $result[0]) {
							$upd_btn_text = $result[0]->response->shortTitle;
						} else {
							$ready_jsfun = 'jQuery("#vik-update-btn").trigger("click");';
						}
						?>
						<button type="button" id="vik-update-btn" onclick="<?php echo count($result) && $result[0] && $result[0]->response->compare == 1 ? 'document.location.href=\'index.php?option=com_vikrentitems&task=updateprogram\'' : 'checkVersion(this);'; ?>">
							<i class="vriicn-cloud"></i> 
							<span><?php echo $upd_btn_text; ?></span>
						</button>
						<script type="text/javascript">
						function checkVersion(button) {
							jQuery(button).find('span').text('Checking...');
							jQuery.ajax({
								type: 'POST',
								url: 'index.php?option=com_vikrentitems&task=checkversion&tmpl=component',
								data: {}
							}).done(function(resp) {
								var obj = typeof resp === 'string' ? JSON.parse(resp) : resp;
								console.log(obj);
								if (obj.status == 1 && obj.response.status == 1) {
									jQuery(button).find('span').text(obj.response.shortTitle);
									if (obj.response.compare == 1) {
										jQuery(button).attr('onclick', 'document.location.href="index.php?option=com_vikrentitems&task=updateprogram"');
									}
								}
							}).fail(function(resp) {
								console.log(resp);
							});
						}
						jQuery(function() {
							<?php echo $ready_jsfun; ?>
						});
						</script>
						<?php
					} else {
						/**
						 * When Vik Updater is not available or disabled, we now
						 * render a modal for the automated installation of the plugin.
						 * 
						 * @since 	1.5.1 (J - - 1.0.0 (WP))
						 */

						$data = [
							'hn'  => getenv('HTTP_HOST'),
							'sn'  => getenv('SERVER_NAME'),
							'app' => CREATIVIKAPP,
							'ver' => VIKRENTITEMS_SOFTWARE_VERSION,
						];

						$vikupdater_url = 'https://extensionsforjoomla.com/vikcheck/vikupdater.php?' . http_build_query($data);

						echo JHtml::fetch(
							'bootstrap.renderModal',
							'jmodal-version-check',
							array(
								'title'       => 'Install VikUpdater',
								'closeButton' => true,
								'keyboard'    => true,
								'bodyHeight'  => 80,
								'url'         => $vikupdater_url,
								'footer'      => '<button type="button" class="btn btn-success" id="version-check-install">' . JText::translate('JTOOLBAR_INSTALL') . '</button>',
							)
						);
						?>
						<button type="button" id="vik-update-btn">
							<i class="vriicn-cloud"></i> 
							<span></span>
						</button>

						<?php echo Vikrentitems::getVriApplication()->getJmodalScript(); ?>

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

								$(function() {
									$('#vik-update-btn').on('click', () => {
										vriOpenJModal('version-check');
									});

									$('#version-check-install').on('click', () => {
										const form = $('<form action="index.php?option=com_installer&task=install.install" method="post"></form>');

										form.append('<input type="hidden" name="installtype" value="url" />');
										form.append('<input type="hidden" name="install_url" value="https://extensionsforjoomla.com/vikapi/?task=products.freedownload&sku=vup" />');
										form.append('<input type="hidden" name="return" value="<?php echo base64_encode(JUri::getInstance()); ?>" />');
										form.append('<?php echo JHtml::fetch('form.token'); ?>');

										$('body').append(form);

										form.submit();
									});
								});
							})(jQuery);
						</script>
						<?php
					}
				}
			}
		?>

				</div>
			</div>
		</div>
		<script type="text/javascript">
		jQuery(function() {
			jQuery('.vri-menu-parent-li').hover(
				function() {
					jQuery(this).addClass('vri-menu-parent-li-opened');
					jQuery(this).find('.vri-submenu-wrap').addClass('vri-submenu-wrap-active');
				},function() {
					jQuery(this).removeClass('vri-menu-parent-li-opened');
					jQuery(this).find('.vri-submenu-wrap').removeClass('vri-submenu-wrap-active');
				}
			);

			if (jQuery('.vmenulinkactive').length) {
				// set active class to current menu block
				jQuery('.vmenulinkactive').closest('.vri-submenu-wrap').parent('li').addClass('vri-menu-parent-li-active');
			}

			// handle quick actions storage
			jQuery('.vri-menu-right').find('.vri-submenu-ul').find('a').on('click', function(e) {
				if (!jQuery(this).find('.vri-submenu-item-txt').length) {
					// nothing to do
					return true;
				}

				// handle the clicked menu entry
				e.preventDefault();

				try {
					// register clicked page
					VRICore.registerAdminMenuAction({
						name: jQuery(this).find('.vri-submenu-item-txt').text(),
						href: jQuery(this).attr('href'),
					});
				} catch(e) {
					console.error(e);
				}

				if (e.metaKey) {
					// open link on new window
					window.open(jQuery(this).attr('href'), '_blank').focus();
				} else {
					// proceed with the navigation
					window.location.href = jQuery(this).attr('href');
				}

				return true;
			});

			// register event to populate quick actions sub-menu helpers
			document.addEventListener('vri-adminmenu-quickactions-create', () => {
				// load the action from the "widgets" scope to be used as fallback
				let menu_widgets_actions = VRICore.getAdminMenuActions('widgets');

				// scan all menu scopes
				jQuery('.vri-submenu-ul[data-menu-scope]').each(function() {
					let menu_ul = jQuery(this);
					let scope = menu_ul.attr('data-menu-scope');
					if (!scope) {
						return;
					}

					let orig_scope = scope;
					let wrapper = menu_ul.closest('.vri-submenu-wrap');
					if (!wrapper || !wrapper.length || wrapper.hasClass('vri-submenu-wrap-multi') || wrapper.find('.vri-submenu-helper-ul').length) {
						return;
					}

					let menu_scope_actions = VRICore.getAdminMenuActions(scope);
					if (!Array.isArray(menu_scope_actions) || !menu_scope_actions.length) {
						// check if we can fallback on the "widgets" scope
						if (Array.isArray(menu_widgets_actions) && menu_widgets_actions.length) {
							// overwrite scope values
							scope 			   = 'widgets';
							menu_scope_actions = menu_widgets_actions;
						} else {
							// do not proceed with this scope
							return;
						}
					}

					// check if menu actions should be merged with the fallback scope
					if (scope != 'widgets' && menu_scope_actions[0].hasOwnProperty('fallback')) {
						let before_fill_length = menu_scope_actions.length;
						menu_scope_actions = VRICore.fillAdminMenuActions(menu_scope_actions, menu_widgets_actions, scope);
						let after_fill_length = menu_scope_actions.length;
						if (before_fill_length && after_fill_length > before_fill_length) {
							// update scope for the filled entries
							VRICore.storageSetItem(VRICore.getStorageScopeName(scope), menu_scope_actions);
						}
					}

					wrapper.addClass('vri-submenu-wrap-multi');
					let quick_actions = jQuery('<ul></ul>').addClass('vri-submenu-helper-ul');
					quick_actions.append('<li class="vri-submenu-helper-lbl-li"><span class="vri-submenu-helper-lbl-txt">' + Joomla.JText._('VRI_QUICK_ACTIONS') + '</span></li>');

					// scan all menu actions
					menu_scope_actions.forEach((action, index) => {
						let is_pinned = action.hasOwnProperty('pinned') && action['pinned'];
						let quick_actions_entry = jQuery('<li></li>').addClass((is_pinned ? 'vri-submenu-item-helper-pinned' : 'vri-submenu-item-helper-unpinned'));
						let quick_actions_div = jQuery('<div></div>').addClass('vmenulink');
						let quick_action_link = jQuery('<a></a>').attr('href', action['href']).addClass('vri-submenu-item-helper-link');

						if (action.hasOwnProperty('target') && action['target']) {
							quick_action_link.attr('target', action['target']);
						}

						if (action.hasOwnProperty('img') && action['img']) {
							let quick_action_img = jQuery('<span></span>').addClass('vri-submenu-item-helper-avatar');
							quick_action_img.append('<img src="' + action['img'] + '" />');
							quick_action_link.append(quick_action_img);
						} else if (action.hasOwnProperty('icon') && action['icon']) {
							let quick_action_icon = jQuery('<span></span>').addClass('vri-submenu-item-helper-avatar');
							quick_action_icon.append(action['icon']);
							quick_action_link.append(quick_action_icon);
						}

						let quick_action_name = jQuery('<span></span>').addClass('vri-submenu-item-helper-txt').text(action['name']);
						quick_action_link.append(quick_action_name);
						quick_actions_div.append(quick_action_link);

						let quick_action_pin = jQuery('<span></span>').addClass('vri-submenu-item-helper-setpin').on('click', function() {
							// toggle pinned status and update admin menu action
							if (!action.hasOwnProperty('pinned')) {
								action['pinned'] = !is_pinned;
							} else {
								action['pinned'] = !action['pinned'];
							}
							try {
								// register/unregister pin
								if (orig_scope != scope && !VRICore.getAdminMenuActions(orig_scope).length) {
									// set fallbacked scope
									action['fallback'] = scope;

									// register scope on local storage with the fallbacked scope
									VRICore.registerAdminMenuAction(action, orig_scope);
								} else {
									// update local storage regularly on this scope
									VRICore.updateAdminMenuAction(action, scope);
								}
								
								// trigger event
								VRICore.emitEvent('vri-adminmenu-quickactions-update');
							} catch(e) {
								console.error(e);
							}
							// update action status
							if (action['pinned']) {
								jQuery(this).closest('li').removeClass('vri-submenu-item-helper-unpinned').addClass('vri-submenu-item-helper-pinned');
							} else {
								jQuery(this).closest('li').removeClass('vri-submenu-item-helper-pinned').addClass('vri-submenu-item-helper-unpinned');
							}
						});

						quick_action_pin.html('<?php VikrentitemsIcons::e('thumbtack'); ?>');
						quick_actions_div.append(quick_action_pin);
						quick_actions_entry.append(quick_actions_div);
						quick_actions.append(quick_actions_entry);
					});

					// append to DOM element
					wrapper.append(quick_actions);
				});
			});

			// register event to update the pinned quick actions
			document.addEventListener('vri-adminmenu-quickactions-update', VRICore.debounceEvent(() => {
				let menu_scopes = [];
				jQuery('.vri-submenu-ul[data-menu-scope]').each(function() {
					let menu_ul = jQuery(this);
					let scope = menu_ul.attr('data-menu-scope');
					if (!scope) {
						scope = '';
					}
					if (menu_scopes.indexOf(scope) < 0) {
						menu_scopes.push(scope);
					}
				});
				let admin_menu_actions = [];
				menu_scopes.forEach((scope) => {
					let menu_actions = VRICore.getAdminMenuActions(scope);
					admin_menu_actions.push({
						scope: scope,
						actions: menu_actions,
					});
				});
				VRICore.doAjax(
					"<?php echo Vikrentitems::ajaxUrl('index.php?option=com_vikrentitems&task=menuactions.update'); ?>",
					{
						actions: admin_menu_actions,
					},
					(resp) => {
						// do nothing
					},
					(err) => {
						// log the error
						console.error(err.responseText);
					}
				);
			}, 300));

			// populate quick actions sub-menu helpers on page load
			VRICore.emitEvent('vri-adminmenu-quickactions-create');

			// check (only once) if the quick actions should be imported from the db
			if (<?php echo !$admin_menu_actions_checked ? 'true' : 'false'; ?>) {
				setTimeout(() => {
					// count admin menu actions populated from local storage
					let tot_admin_menu_actions = 0;
					jQuery('.vri-submenu-ul[data-menu-scope]').each(function() {
						let menu_ul = jQuery(this);
						let scope = menu_ul.attr('data-menu-scope');
						if (!scope) {
							return;
						}
						let menu_scope_actions = VRICore.getAdminMenuActions(scope);
						if (!Array.isArray(menu_scope_actions) || !menu_scope_actions.length) {
							return;
						}
						tot_admin_menu_actions++;
						return false;
					});

					if (tot_admin_menu_actions) {
						return;
					}

					// request for any previously stored quick actions for this admin
					VRICore.doAjax(
						"<?php echo Vikrentitems::ajaxUrl('index.php?option=com_vikrentitems&task=menuactions.retrieve'); ?>",
						{},
						(resp) => {
							// store to local storage the quick actions just retrieved
							let obj_res = typeof resp === 'string' ? JSON.parse(resp) : resp;
							if (Array.isArray(obj_res) && obj_res.length) {
								obj_res.forEach((menu_actions) => {
									let storage_scope_name = VRICore.options.admin_menu_actions_nm;
									if (menu_actions['scope']) {
										storage_scope_name += '.' + menu_actions['scope'];
									}
									VRICore.storageSetItem(storage_scope_name, menu_actions['actions']);
								});
							}
							// trigger the event to populate the quick actions
							VRICore.emitEvent('vri-adminmenu-quickactions-create');
						},
						(err) => {
							// log the error
							console.error(err.responseText);
						}
					);
				}, 300);
			}

			// declare window resize handler for setting the position of sub-menus
			const subMenuStylesPositioning = () => {
				// get window width
				let windowWidth = window.innerWidth;

				// scan all sub-menu entries
				document.querySelectorAll('.vri-submenu-wrap').forEach((submenu) => {
					// remove the class to style the sub-menu "to left"
					submenu.classList.remove('vri-submenu-wrap-toleft');

					// get sub-menu width and offset left position values
					let elemWidth = submenu.offsetWidth;
					let elemOffLeft = submenu.getBoundingClientRect().left;

					if (elemWidth) {
						// sub-menu visibility is hidden, but width has been computed
						if ((elemWidth + elemOffLeft) > windowWidth) {
							// add "to left" positioning class or the sub-menu would exceed the window
							submenu.classList.add('vri-submenu-wrap-toleft');
						}
					} else {
						// on small screens, the sub-menu may be hidden, hence with no width
						let parentmenu = submenu.closest('.vri-menu-parent-li');

						// register listener for mouseover event
						parentmenu.addEventListener('mouseover', (e) => {
							setTimeout(() => {
								let now_submenu = e.target.querySelector('.vri-submenu-wrap');
								if (!now_submenu) {
									return;
								}
								if ((now_submenu.offsetWidth + now_submenu.getBoundingClientRect().left) > window.innerWidth) {
									// add "to left" positioning class or the sub-menu would exceed the window
									now_submenu.classList.add('vri-submenu-wrap-toleft');
								}
							}, 0);
						});

						// register listener for touchstart event
						parentmenu.addEventListener('touchstart', (e) => {
							let limenu = e.target;
							if (!limenu.matches('.vri-menu-parent-li')) {
								limenu = limenu.closest('.vri-menu-parent-li');
							}
							// propagate the mouseover event
							limenu.dispatchEvent(new Event('mouseover'));
						});
					}
				});
			};

			// register listener for window resize event
			window.addEventListener('resize', subMenuStylesPositioning);

			// trigger sub-menus styles positioning function on page load
			setTimeout(() => {
				subMenuStylesPositioning();
			}, 0);

		});
		</script>
		<?php 
	}
	
	public static function printFooter()
	{
		echo '<br clear="all" />' . '<div id="hmfooter">' . JText::sprintf('VRIVERSION', VIKRENTITEMS_SOFTWARE_VERSION) . ' <a href="https://extensionsforjoomla.com/">e4j - Extensionsforjoomla.com</a></div>';
	}
	
	/**
	 * Returns a BS-compatible dropdown menu that submits
	 * the form whenever a value is selected.
	 * 
	 * @param 	array		$arr_values
	 * @param 	string 		$current_key
	 * @param 	string 		$empty_value
	 * @param 	string 		$default
	 * @param 	string 		$input_name
	 *
	 * @return 	string
	 */
	public static function getDropDown($arr_values, $current_key, $empty_value, $default, $input_name)
	{
		$dropdown = '';
		$x = rand(1, 999);
		if (defined('JVERSION') && version_compare(JVERSION, '2.6.0') < 0) {
			//Joomla 2.5
			$dropdown .= '<select name="'.$input_name.'" onchange="document.adminForm.submit();">'."\n";
			$dropdown .= '<option value="">'.$default.'</option>'."\n";
			$list = "\n";
			foreach ($arr_values as $k => $v) {
				$dropdown .= '<option value="'.$k.'"'.($k == $current_key ? ' selected="selected"' : '').'>'.$v.'</option>'."\n";
			}
			$dropdown .= '</select>'."\n";
		} else {
			//Joomla 3.x
			$dropdown .= '<script type="text/javascript">'."\n";
			$dropdown .= 'function dropDownChange'.$x.'(setval) {'."\n";
			$dropdown .= '	document.getElementById("dropdownval'.$x.'").value = setval;'."\n";
			$dropdown .= '	document.adminForm.submit();'."\n";
			$dropdown .= '}'."\n";
			$dropdown .= '</script>'."\n";
			$dropdown .= '<input type="hidden" name="'.$input_name.'" value="'.$current_key.'" id="dropdownval'.$x.'"/>'."\n";
			$list = "\n";
			foreach ($arr_values as $k => $v) {
				if($k == $current_key) {
					$default = $v;
				}
				$list .= '<li><a href="javascript: void(0);" onclick="dropDownChange'.$x.'(\''.$k.'\');">'.$v.'</a></li>'."\n";
			}
			$list .= '<li class="divider"></li>'."\n".'<li><a href="javascript: void(0);" onclick="dropDownChange'.$x.'(\'\');">'.$empty_value.'</a></li>'."\n";
			$dropdown .= '<div class="btn-group">
		<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-expanded="true">'.$default.' <span class="caret"></span></button>
		<ul class="dropdown-menu" role="menu">'.
			$list.
		'</ul>
	</div>';
		}

		return $dropdown;
	}

	public static function pUpdateProgram($version)
	{
		/**
		 * @wponly 	do nothing
		 */
	}

	/**
	 * Method to add parameters to the update extra query.
	 * 
	 * @joomlaonly 	this class is automatically loaded by Joomla
	 * 				to invoke this method when updating the component.
	 *
	 * @param   Update  &$update  An update definition
	 * @param   JTable  &$table   The update instance from the database
	 *
	 * @return  void
	 *
	 * @since 	1.7.3 (J) - 1.1.5 (WP)
	 */
	public static function prepareUpdate(&$update, &$table)
	{
		// get current domain
		$server = JFactory::getApplication()->input->server;

		// build query array
		$query = [
			'domain' => base64_encode($server->getString('HTTP_HOST')),
			'ip' 	 => $server->getString('REMOTE_ADDR'),
		];

		// always refresh the extra query before an update
		$update->set('extra_query', http_build_query($query, '', '&amp;'));
	}
}
