// Admin page menu
document.addEventListener('DOMContentLoaded', function () {
    // Get all buttons and sections
    const buttons = document.querySelectorAll('#wet-sticky-header .nav-btn');
    const sections = document.querySelectorAll('#wet-page-body > div');

    // Add click event listener to each button
    buttons.forEach(button => {
        button.addEventListener('click', function () {
            // Get the target section from the button's data attribute
            const targetId = this.getAttribute('data-target');

            // Hide all sections
            sections.forEach(section => {
                section.style.display = 'none';
            });

            // Show the targeted section
            const targetSection = document.getElementById(targetId);
            targetSection.style.display = 'block';
        });
    });
});

/* all products sort */
jQuery(function($){
	
	const ITEMS_PER_PAGE = 45;

	let visibleLimit = ITEMS_PER_PAGE;

    const currentCat = $(".atg").data("cat");

	$(".theme-check input").prop("checked",true);
	
	$(".home-sort-buttons #theme-count-total").text($(".wetl-template").length);

	function resetFilters(){

		$(".theme-check input").prop("checked",true);

	}

    function sortThemes(){

        let items=$(".wet-templates-loop .wetl-template").get();

        items.sort(function(a,b){

			let aRelated = parseInt($(a).data("related")) || 0;
			let bRelated = parseInt($(b).data("related")) || 0;

			if(aRelated !== bRelated){
				return bRelated - aRelated;
			}

            if($("#home-sort-popular").hasClass("home-sort-active")){

                return $(b).data("popular")-$(a).data("popular");

            }

			if($("#home-sort-abc").hasClass("home-sort-active")){

				return ($(a).data("abc")+"").localeCompare($(b).data("abc")+"");

			}

            return $(b).data("date")-$(a).data("date");

        });

        let parent = $(".wet-templates-loop");

		$.each(items,function(){
			parent.append(this);
		});

    }

	function applyLoadMore(){

		let visibleItems = $(".wetl-template")
			.not(".hidden-template");

		visibleItems.addClass("loadmore-hidden");

		visibleItems
			.slice(0, visibleLimit)
			.removeClass("loadmore-hidden");

		if(visibleItems.length <= visibleLimit){

			$("#theme-load-more").hide();

		}else{

			$("#theme-load-more").show();

		}

	}
	
    function filterThemes(){

        let styles=[];
        let structures=[];

        $(".theme-filter-list:eq(0) input:checked").each(function(){
            styles.push($(this).val());
        });

        $(".theme-filter-list:eq(1) input:checked").each(function(){
            structures.push($(this).val());
        });

        let count=0;

        $(".wetl-template").each(function(){

            let style=(( $(this).data("style") || "" )+"").split(" ");
			let structure=(( $(this).data("structure") || "" )+"").split(" ");

            let styleMatch=false;
            let structureMatch=false;

            $.each(styles,function(i,v){

                if(style.indexOf(v)>-1){
                    styleMatch=true;
                    return false;
                }

            });

            $.each(structures,function(i,v){

                if(structure.indexOf(v)>-1){
                    structureMatch=true;
                    return false;
                }

            });

            if(styleMatch && structureMatch){

                count+=parseInt($(this).data("count"));

                $(this).removeClass("hidden-template");

            }else{

                $(this).addClass("hidden-template");

            }

        });

        $("#theme-count-current").text(count);
		
		if(count==0){
			$(".theme-no-results").fadeIn(200);
		}else{
			$(".theme-no-results").hide();
		}

    }
	
	function refresh(){
		visibleLimit = ITEMS_PER_PAGE;
		sortThemes();
		filterThemes();
		applyLoadMore();
	}
	
	$("#theme-load-more").on("click", function(){
		visibleLimit += ITEMS_PER_PAGE;
		applyLoadMore();
	});


    $("#home-sort-date").click(function(){
        $(".home-sort-b").removeClass("home-sort-active");
        $(this).addClass("home-sort-active");
        refresh();
    });


    $("#home-sort-popular").click(function(){
        $(".home-sort-b").removeClass("home-sort-active");
        $(this).addClass("home-sort-active");
        refresh();
    });
	
	$("#home-sort-abc").click(function(){
		$(".home-sort-b").removeClass("home-sort-active");
		$(this).addClass("home-sort-active");
		refresh();
	});


    $(".theme-check input").change(function(){
        refresh();
    });
	
	$(".theme-clear-filters").on("click",function(){
		resetFilters();
		refresh();
	});
	
	$(".theme-no-results-reset").click(function(){
		resetFilters();
		refresh();
	});

    refresh();

});
