<?php
/**
 * Venustheme
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Venustheme.com license that is
 * available through the world-wide-web at this URL:
 * https://venustheme.com/terms
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category   venustheme
 * @package    Ves_Megamenu
 * @copyright  Copyright (c) 2021 venustheme (https://www.venustheme.com/)
 * @license    https://venustheme.com/terms
 */

// phpcs:disable Magento2.Templates.ThisInTemplate
// phpcs:disable Magento2.Security.XssTemplate.FoundUnescaped
// phpcs:disable Generic.Files.LineLength.TooLong
/**
 * @var $block \Ves\MegaMenu\Block\Menu
 */
?>
<?php if ($menu = $this->getMenu()) : ?>
<?php
$data = $this->helper(\Ves\Megamenu\Helper\Data::class);
$html = $class = '';
$mobileTemplate = $menu->getData("mobile_template");
$mobile_menu 	= false;
if(!$mobileTemplate) $mobileTemplate = 1;
//If active mobile menu alias will replace current desktop menu to mobile menu
if ($mobileTemplate == 2 && $menu->getMobileMenuAlias() && $this->getMobileDetect()->isMobile()) {
	$mobile_menu_alias = $menu->getMobileMenuAlias();
	$old_menu = $menu;
	$menu = $block->getMenuProfile(0, $mobile_menu_alias);
	if($menu) {
		$mobile_menu = true;
	} else {
		$menu = $old_menu;
	}
}

$mobileTemplate = $menu->getData("mobile_template");
if($this->hasData("mobile_template")) $mobileTemplate = $this->getData("mobile_template");
if($mobileTemplate == 0){
	$class = 'offcanvas-menu offcanvas-menu-left ves-megamenu-mobile';
}elseif($mobileTemplate == 3){
	$class = 'ves-drill-down-menu';
}else{
	$class = 'ves-megamenu-mobile';
}

$menuItems = $menu->getData('menuItems');
$structure = json_decode($menu->getStructure(), true);
$categories = [];
foreach ($menuItems as $item) {
	if (isset($item['link_type']) && $item['link_type'] == 'category_link' && isset($item['category']) && !in_array($item['category'], $categories)) {
		$categories[] = $item['category'];
	}
}
$data->setMenuCategories($categories);
if(is_array($structure)){
	foreach ($structure as $k => $v) {
		$itemData = $data->renderMenuItemData($v, [], $menuItems);
		$html .= $data->drawItem($itemData);
	}
}

$checked = true;
$html2 = "";

try{
	$html2          = $data->filter($html);
} catch (\Exception $e) {
	$checked = false;
}

if($checked) {
	$html = $html2;
}

$alias = $menu->getAlias().time().rand();
$disableBellow = (int)$menu->getData('disable_bellow');
$disableAbove = (int)$menu->getData('disable_above');

$desktopTemplate = $menu->getData("desktop_template");
if(!$desktopTemplate) $desktopTemplate = 'horizontal';
if($this->hasData("desktop_template")) $desktopTemplate = $this->getData("desktop_template");

$event = $menu->getData("event");
if(!$event) $event = 'hover';
if($this->hasData("event")) $event = $this->getData("event");

$class .= ' ' . $menu->getData("classes");
$custom_css_class = $this->getConfig("custom_class");
if($custom_css_class){
	$class .=" ".$custom_css_class;
}
$menu_custom_class = "";
$disableItemBlocks = $menu->getData("disable_iblocks");
if($disableItemBlocks)
	$menu_custom_class .= ' hide-blocks';
if($desktopTemplate == "vertical-left" || $desktopTemplate == "vertical-right") {
	$menu_custom_class .= "ves-vertical";
}
?>
<div class="block ves-menu <?= $class ?>" id="<?= $alias ?>-menu">
	<?php if($title = $this->getData("title")) : ?>
	<div class="block-title"><strong><span><?= __($title); ?></span></strong></div>
	<?php endif; ?>
	<nav class="navigation" role="navigation" data-action="navigation">
		<ul id="<?= $alias ?>" class="ves-megamenu <?= 'menu-' . $event ?> ves-<?= $desktopTemplate ?> <?= $menu_custom_class; ?>" <?php if($disableBellow>0){ ?>data-disable-bellow="<?= $disableBellow ?>"<?php } ?> <?php if($disableAbove>0){ ?>data-disable-above="<?= $disableAbove ?>"<?php } ?> data-mage-init='{"menu":{"responsive":false, "expanded":false, "position":{"my":"left top","at":"left bottom"}}}' data-toggle-mobile-nav="false">
			<?= $html; ?>
		</ul>
		<?= /* @escapeNotVerified */ $block->getChildHtml() ?>
	</nav>
	<?php if ($html!='') :?>
	<script type="text/javascript">
		if(typeof(initedToggleMenu) == 'undefined') {
			var initedToggleMenu = false;
		}
		require(['jquery',
			<?php if($menu->getData('scrolltofixed')) : ?>'scrolltofixed',<?php endif; ?>
			'Ves_Megamenu/js/megamenuGeneral'
			],function($){
				$('.price-box.price-final_price', $("#<?= $alias ?>")).each(function(){
					$(this).removeClass('price-box').addClass('price-box1');
					$(this).attr('data-role','priceBox1');
				})
				if(typeof(playMegamenuJs) == 'function') {
					//console.log("\nrun playMegamenuJs function...");
					jQuery(document).ready(function($) {
						playMegamenuJs($, '<?= $alias ?>', <?= $mobileTemplate; ?>, '<?= $event; ?>', <?= $menu->getData('scrolltofixed'); ?>);

					});
				} else {
					jQuery(document).ready(function($) {
						<?php if ($mobileTemplate == 3) { ?>
							$('.ves-drill-down-menu').find('.opener').addClass('ves-click');
							$(window).on('load resize',function(e){
								e.preventDefault();

								var back        	= '<div class="hide-submenu"></div>';
								var subHide     	= $(back);
								var subMenu       	= $('.ves-drill-down-menu .submenu');

								// Add submenu hide bar
								if (subHide.children('hide-submenu').length ==0 && subMenu.find(".hide-submenu").length == 0) {
									subHide.prependTo(subMenu);
								}
								var subHideToggle 	= $('.ves-drill-down-menu .hide-submenu');
								// Hide submenu
								subHideToggle.on("click", function() {
									$(this).parents(".view-submenu").removeClass('view-submenu');
									// $(this).parent().parent().removeClass('view-submenu');
									// $(this).parent().parent().parent().removeClass('view-submenu');
									// $(this).parent().parent().parent().parent().parent().parent().parent().removeClass('view-submenu');
									// $(this).parent().parent().parent().parent().parent().parent().parent().parent().removeClass('view-submenu');
									// $(this).parent().parent().parent().parent().parent().parent().parent().parent().parent().removeClass('view-submenu');
									$(this).parent().hide();
								});

								if ($(window).width() <= 768){

									$('.ves-drill-down-menu').find('.opener').addClass('fa fa-arrow-right').removeClass('opener');
									$('.ves-drill-down-menu').find('.navigation').addClass('navdrilldown').removeClass('navigation');
									$('.ves-drill-down-menu #<?= $alias ?> .ves-click').on('click', function(e) {
										e.preventDefault();
										if ($(window).width() <= 768){

											$(this).removeClass('.item-active');
											$(this).parents('.dropdown-submenu').addClass('view-submenu');
											$(this).parents('.submenu').addClass('view-submenu');
											$(this).parents('ul.ves-megamenu').addClass('view-submenu');
											var a = $(this).parents('li.nav-item').offset().top;
											var b = $(this).parents('ul.ves-megamenu').offset().top;
											var c = $(this).parent().parent().offset().top;

											$(this).parents('li.nav-item').children('.submenu').css('top',b-a+'px');
											$(this).parent().parent().children('.submenu').css('top',b-c+'px');
											$('.submenu.dropdown-menu').hide();
											$(this).parents('.submenu').show();
											$(this).parent().parent().children('.submenu').show();
											return false;

										}
									});
								}else {

									$('.ves-drill-down-menu').find('.fa-arrow-right').addClass('opener').removeClass('fa fa-arrow-right');
									$('.ves-drill-down-menu').find('.navdrilldown').addClass('navigation').removeClass('navdrilldown');
								}
							});
							<?php } ?>
							jQuery('#<?= $alias ?>-menu .ves-megamenu .level0').hover(function() {
								var mParentTop = jQuery(this).parents('.ves-megamenu').offset().top;
								var mParentHeight = $(this).parent().height();
								var mTop =  $(this).height();
								var mHeight = $(this).height();
								var mParent = $(this).parent();
								if (mHeight < mParentHeight) {
									mTop = $(this).offset().top - mParent.offset().top + mHeight;
								}
								$(this).children('.submenu').css({top:mTop});
							});
							<?php if($menu->getData('scrolltofixed')){ ?>

								$('.nav-sections-items > .nav-sections-item-content').scrollToFixed({
									zIndex: 99
								});

								$(window).on("resize load", function(){
									if ($(window).width() < 768){
										$('.nav-sections-items > .nav-sections-item-content').css({position: '', top: '', width: '100%'});
									}
								});
								var menuParentPosition = $('#<?= $alias ?>-menu').parents('.sections.nav-sections').offset().top;
								$(window).scroll(function() {
									var height = $(window).scrollTop();
									if (height<(menuParentPosition) - $('#<?= $alias ?>-menu').outerHeight()) {
										$('.nav-sections-items > .nav-sections-item-content').css({position: '', top: '', width: '100%'});
									}
									$('.section-items.nav-sections-items').find('div').each(function(index, el) {
										if ($(this).html() == '' && $(this).attr('class')=='') {
											$(this).remove();
										}
									});
								});
								<?php } ?>
								jQuery('p').each(function() {
									var $this = $(this);
									if ($this.html().replace(/\s|&nbsp;/g, '').length == 0)
									$this.remove();
								});
							});
					var toggle_nav = $("#<?= $alias; ?>").attr("data-toggle-mobile-nav");
					if(toggle_nav == true || toggle_nav == 'true' || toggle_nav==1){
						if(!initedToggleMenu) {
							var menuToogle = function () {
								if ($('html').hasClass('nav-open')) {
									$('html').removeClass('nav-open');
									setTimeout(function () {
										$('html').removeClass('nav-before-open');
									}, 300);
								} else {
									$('body').append('<div class="ves-overlay ves-overlay<?= $alias ?>"></div>');
									$('html').addClass('nav-before-open');
									setTimeout(function () {
										$('html').addClass('nav-open');
									}, 42);
								}
							}
							$(document).on("click", ".action.nav-toggle", menuToogle);
							initedToggleMenu = true;
						}
						$(document).on("click", ".ves-overlay<?= $alias ?>", function(){
							$('#<?= $alias ?>').css("left","");
							$('html').removeClass('ves-navopen');
							setTimeout(function () {
								$('html').removeClass('ves-nav-before-open');
							}, 300);
							$(this).remove();
							return false;
						});
					}

					$("#<?= $alias ?> .dynamic-items li").hover(function(){
						$(this).parents(".dynamic-items").find("li").removeClass("dynamic-active");
						$(this).addClass("dynamic-active");
						var id = $(this).data("dynamic-id");
						$("#<?= $alias ?> ."+id).parent().find(".dynamic-item").removeClass("dynamic-active");
						$("#<?= $alias ?> ."+id).addClass("dynamic-active");
					});
					var mImg = '';
					$("#<?= $alias ?> img").hover(function(){
						mImg = '';
						mImg = $(this).attr('src');
						if ($(this).data('hoverimg')){
							$(this).attr('src',$(this).data('hoverimg'));
						}
					},function(){
						$(this).attr('src',mImg);
					});
					$("#<?= $alias ?> li a").hover(function(){
						$(this).css({
							"background-color": $(this).data("hover-bgcolor"),
							"color": $(this).data("hover-color")
						});
					}, function(){
						$(this).css({
							"background-color": $(this).data("bgcolor"),
							"color": $(this).data("color")
						});
					});
					$(window).on("resize load", function(){

						if($("#<?= $alias ?>").data("disable-bellow") && $("#<?= $alias ?>").data("disable-above")){

							if (($(window).width() <= $("#<?= $alias ?>").data("disable-bellow")) || ($(window).width() >= $("#<?= $alias ?>").data("disable-above"))){
								$("#<?= $alias ?>-menu").hide();
							}else{
								$("#<?= $alias ?>-menu").show();
							}

							$("#<?= $alias ?>").find("li").each(function(index, element){
								if (($(window).width() <= $(this).data("disable-bellow")) || ($(window).width() >= $(this).data("disable-above"))){
									$(this).addClass("hidden");
								} else if ($(this).hasClass("hidden")){
									$(this).removeClass("hidden");
								}
							});

						} else if($("#<?= $alias ?>").data("disable-bellow") && !$("#<?= $alias ?>").data("disable-above")) {
							if ($(window).width() <= $("#<?= $alias ?>").data("disable-bellow")){
								$("#<?= $alias ?>-menu").hide();
							}else{
								$("#<?= $alias ?>-menu").show();
							}

							$("#<?= $alias ?>").find("li").each(function(index, element){
								if ($(window).width() <= $(this).data("disable-bellow")){
									$(this).addClass("hidden");
								}else if ($(this).hasClass("hidden")){
									$(this).removeClass("hidden");
								}
							});
						} else if($("#<?= $alias ?>").data("disable-above") && !$("#<?= $alias ?>").data("disable-bellow")) {
							if ($(window).width() >= $("#<?= $alias ?>").data("disable-above")){
								$("#<?= $alias ?>-menu").hide();
							}else{
								$("#<?= $alias ?>-menu").show();
							}

							$("#<?= $alias ?>").find("li").each(function(index, element){
								if($(window).width() >= $(this).data("disable-above")) {
									$(this).addClass("hidden");
								} else if ($(this).hasClass("hidden")){
									$(this).removeClass("hidden");
								}
							});
						}

						if ($(window).width() >= 768 && $(window).width() <= 1024){
							$("#<?= $alias ?> .nav-anchor").off().click(function(){
								var iParent = $(this).parent('.nav-item');
								iParent.addClass("clicked");
								if ($(iParent).children('.submenu').length == 1){
									iParent.trigger('hover');
									if (iParent.hasClass('submenu-alignleft') || iParent.hasClass('submenu-alignright')){
										if ((iParent.offset().left + iParent.find('.submenu').eq(0).width()) > $(window).width()){
											iParent.find('.submenu').eq(0).css('max-width','100%');
											iParent.css('position','static');
										}
									}
									return false;
								}
							});
						}else{
							$("#<?= $alias ?>").find('.submenu').css('max-width','');
							$("#<?= $alias ?>").find('.submenu-alignleft').css('position','relative');
						}
						if ($(window).width() <= 768){
							$('.sections.nav-sections').removeAttr( "style" )
							$("#<?= $alias ?>").addClass("nav-mobile");

							/*
							$("#<?= $alias ?> .nav-anchor").off().click(function(){

								$("#<?= $alias ?> .nav-item").removeClass("item-active");
								var parent = $(this).parents(".nav-item").eq(0);
								$(this).toggleClass('item-active');
								$(parent).find(".submenu").eq(0).slideToggle();
								return false;
							});
							*/

						}else{
							$("#<?= $alias ?>").find(".submenu").css({'display':''});
							$("#<?= $alias ?>").find("div").removeClass("mbactive");
							$("#<?= $alias ?>").removeClass("nav-mobile");

							/*
							$("#<?= $alias ?> .nav-anchor").off().click(function(){
								if("#" != $(this).attr("href"))
									window.location.url = $(this).attr("href");
								return true;
							});
							*/

						}
					}).resize();
					//Toggle mobile menu
					$('.ves-megamenu-mobile #<?= $alias ?> .opener').on('click', function(e) {
						e.preventDefault();
						$("#<?= $alias ?> .nav-item").removeClass("item-active");
						var parent = $(this).parents(".nav-item").eq(0);
						$(this).toggleClass('item-active');
						$(parent).find(".submenu").eq(0).slideToggle();
						$(parent).find(".submenu").eq(0).toggleClass('submenu-active');
						return false;
					});


					<?php if($event == 'hover'){ ?>

					<?php } else { ?>
							$(document).mouseup(function(e) {
							    var container = $("#<?= $alias ?> li.dropdown  > .dropdown-menu");

							    // if the target of the click isn't the container nor a descendant of the container
							    if (!container.is(e.target) && container.has(e.target).length === 0)
							    {
							        $(container).stop().hide();
							    }
							});
							var container = $("#<?= $alias ?> li.dropdown > .dropdown-menu");
							container.each(function(){
								$(this).parent().addClass("menu-has-children");
							})
							$("#<?= $alias ?> li.dropdown.menu-has-children > .nav-anchor").on('click', function(e) {

								e.preventDefault();

								var parent = $(this).parents(".nav-item").eq(0);
								$(this).toggleClass('item-active');

								 var $this =  $(parent).find(".dropdown-menu").eq(0);
								$(".dropdown-menu").not($this).hide();
							    $(parent).find(".dropdown-menu").eq(0).stop().toggle();

							});
					<?php } ?>
				}
			});
	</script>
	<?php endif; ?>
</div>
<?php endif; ?>
