<?php
add_action( 'wp_enqueue_scripts', function() {
    wp_enqueue_style('woodmart-style', get_template_directory_uri() . '/style.css');
    wp_enqueue_style('woodmart-child-style', get_stylesheet_uri(), array('woodmart-style'));
});

// Webshop header image //
add_action('woocommerce_before_shop_loop', 'custom_shop_header_image', 5);

function custom_shop_header_image() {
    if (is_shop()) {
        $header_url = 'http://www.popmania.nl/wp-content/uploads/2025/07/Shop-Header.jpg'; // URL van je afbeelding

        echo '<div class="shop-header-image" style="
            width: 100%;
            margin-bottom: 30px;
        ">
            <img src="' . esc_url($header_url) . '" alt="Shop Header" style="width:100%; height:auto;">
        </div>';
    }
}

//Images onder de producten webshop //
add_action( 'woocommerce_after_shop_loop', 'popmania_shop_seo_tekst', 20 );
function popmania_shop_seo_tekst() {
    // Controleer of dit de shop of een productcategorie-pagina is
    if ( is_shop() || is_product_category() ) {
        echo '<div class="shop-seo-tekst" style="margin:40px 0 20px 0; padding:24px; background:#f7f7f7; border-radius:12px;">
            <h2>Populaire producten en inspiratie van PopMania</h2>
            <p>Ontdek ons uitgebreide assortiment unieke Funko Pops, exclusieve verzamelobjecten en populaire figuren uit film, serie, sport en games. Elke week voegen wij nieuwe producten toe aan onze collectie. Scroll naar beneden, klik op "Load more products" en laat je inspireren! Op zoek naar iets speciaals? Neem dan gerust <a href="/contact/">contact</a> met ons op.</p>
            <img src="http://www.popmania.nl/wp-content/uploads/2025/07/Shop-Banner.png" alt="Funko Pop Banner" style="display:block; background:none !important; max-width:100%; height:auto;margin:24px auto 0 auto;border-radius:12px;box-shadow:0 2px 16px rgba(0,0,0,0.08);" />
        </div>';
    }
}

add_action( 'woocommerce_after_shop_loop', 'popmania_shop_banners_naast_elkaar', 25 );
function popmania_shop_banners_naast_elkaar() {
    if ( is_shop() || is_product_category() ) {
        echo '
        <div class="popmania-banner-row" style="display:flex; flex-wrap:wrap; gap:24px; justify-content:center; margin:32px 0;">
            <a href="https://www.popmania.nl/product-category/outlet/" style="flex:1; min-width:180px; max-width:400px; text-align:center;">
                <img src="http://www.popmania.nl/wp-content/uploads/2025/07/Outlet_Banner.jpg"
                     alt="Outlet Funko Pop" style="width:100%; height:auto; border-radius:12px; box-shadow:0 2px 16px rgba(0,0,0,0.07);">
            </a>
            <a href="https://www.popmania.nl/categorieen/" style="flex:1; min-width:180px; max-width:400px; text-align:center;">
                <img src="http://www.popmania.nl/wp-content/uploads/2025/07/Categorieen_Banner.jpg"
                     alt="CategorieÃ«n Funko Pop" style="width:100%; height:auto; border-radius:12px; box-shadow:0 2px 16px rgba(0,0,0,0.07);">
            </a>
        </div>
        <style>
            @media (max-width: 800px) {
                .popmania-banner-row { flex-direction: column; gap:16px; }
            }
        </style>
        ';
    }
}

//headers categorie pagina's //
add_action('woocommerce_archive_description', 'custom_category_headers_responsive');

function custom_category_headers_responsive() {
    if (is_product_category()) {
        $current_cat = get_queried_object()->slug;

        $category_headers = array(
            'ad-icons' => 'https://www.popmania.nl/wp-content/uploads/2025/08/ad-icons-cat-header.jpg',
            'animation' => 'https://www.popmania.nl/wp-content/uploads/2025/08/animation-cat-header.jpg',
            'army'    => 'https://www.popmania.nl/wp-content/uploads/2025/07/Disney-Header.jpg',
			'basketball'    => 'https://www.popmania.nl/wp-content/uploads/2025/08/basketball-cat-header.jpg',
			'comics'    => 'https://www.popmania.nl/wp-content/uploads/2025/08/comics-cat-header.jpg',
			'directors'    => 'https://www.popmania.nl/wp-content/uploads/2025/08/directors-cat-header.jpg',
			'disney'    => 'https://www.popmania.nl/wp-content/uploads/2025/08/disney-cat-header.jpg',
			'football'    => 'https://www.popmania.nl/wp-content/uploads/2025/08/football-cat-header.jpg',
			'games'    => 'https://www.popmania.nl/wp-content/uploads/2025/08/games-cat-header.jpg',
			'gpk'    => 'https://www.popmania.nl/wp-content/uploads/2025/08/gpk-cat-header.jpg',
			'heroes'    => 'https://www.popmania.nl/wp-content/uploads/2025/08/heroes-cat-header.jpg',
			'hockey'    => 'https://www.popmania.nl/wp-content/uploads/2025/08/hockey-cat-header.jpg',
			'icons'    => 'https://www.popmania.nl/wp-content/uploads/2025/08/icons-cat-header.jpg',
			'magic'    => 'https://www.popmania.nl/wp-content/uploads/2025/08/magic-cat-header.jpg',
			'marvel'    => 'https://www.popmania.nl/wp-content/uploads/2025/08/marvel-cat-header.jpg',
			'movies'    => 'https://www.popmania.nl/wp-content/uploads/2025/08/movies-cat-header.jpg',
			'retro-toys'    => 'https://www.popmania.nl/wp-content/uploads/2025/08/retrotoys-cat-header.jpg',
			'rocks'    => 'https://www.popmania.nl/wp-content/uploads/2025/08/rocks-cat-header.jpg',
			'star-wars'    => 'https://www.popmania.nl/wp-content/uploads/2025/08/star-wars-cat-header.jpg',
			'television'    => 'https://www.popmania.nl/wp-content/uploads/2025/08/television-cat-header.jpg',
			'trains'    => 'https://www.popmania.nl/wp-content/uploads/2025/08/trains-cat-header.jpg',
			'trolls'    => 'https://www.popmania.nl/wp-content/uploads/2025/08/trolls-cat-header.jpg',
			'army'    => 'https://www.popmania.nl/wp-content/uploads/2025/08/army-cat-header.jpg',
			'wwe'    => 'https://www.popmania.nl/wp-content/uploads/2025/08/wwe-cat-header.jpg',
            // Voeg hier al je categorieën toe
        );

        if (array_key_exists($current_cat, $category_headers)) {
            $header_url = $category_headers[$current_cat];
            echo '<div class="custom-category-header" style="background-image: url(' . esc_url($header_url) . ');"></div>';
        }
    }
}


// Random volgorde per sessie of via URL-parameter (fallback)
add_action( 'woocommerce_product_query', function( $q ) {

    if ( ( is_shop() || is_product_category() || is_product_tag() ) && ! is_admin() ) {

        // Start PHP sessie indien mogelijk
        if ( ! session_id() ) {
            @session_start();
        }

        // 1?? Check of er een seed in de URL staat (fallback methode)
        if ( isset($_GET['rand_seed']) && is_numeric($_GET['rand_seed']) ) {
            $seed = intval($_GET['rand_seed']);
        }
        // 2?? Anders gebruik sessie
        elseif ( ! empty($_SESSION['pm_random_seed']) ) {
            $seed = intval($_SESSION['pm_random_seed']);
        }
        // 3?? Zo niet, maak een nieuwe
        else {
            $seed = rand(0, PHP_INT_MAX);
            $_SESSION['pm_random_seed'] = $seed;
        }

        // Zet query op willekeurige volgorde met onze seed
        $q->set( 'orderby', 'rand' );

        // Pas ORDER BY aan voor consistente volgorde
        add_filter( 'posts_orderby', function( $orderby ) use ( $seed ) {
            if ( strpos( $orderby, 'RAND()' ) !== false ) {
                global $wpdb;
                $orderby = 'RAND(' . $seed . ')';
            }
            return $orderby;
        }, 9999 );

    }
});

// Alles-in-één vertaling voor WoodMart knoppen
add_filter( 'gettext', function( $translated, $text, $domain ) {
    $map = array(
        // Load More knop
        'Load more products' => 'Meer producten',
        'LOAD MORE PRODUCTS' => 'Meer producten',
        'Load more'          => 'Meer producten',
        'LOAD MORE'          => 'Meer producten',

        // AJAX Loading tekst
        'Loading...'         => 'Bezig met laden...',
        'LOADING...'         => 'Bezig met laden...',

        // Buy Now knop
        'Buy now'            => 'Koop nu',
        'BUY NOW'            => 'Koop nu',

        // Add to cart knop
        'Add to cart'        => 'Toevoegen aan winkelwagen',
        'ADD TO CART'        => 'Toevoegen aan winkelwagen',
    );

    if ( isset( $map[ $text ] ) ) {
        return $map[ $text ];
    }

    return $translated;
}, 999, 3 );

// Voeg seed automatisch toe aan Woodmart Load More AJAX URL's (fallback werkt ook bij JS calls)
add_action( 'wp_enqueue_scripts', function() {
    if ( is_shop() || is_product_category() || is_product_tag() ) {
        if ( ! session_id() ) {
            @session_start();
        }
        $seed = ! empty($_SESSION['pm_random_seed']) ? $_SESSION['pm_random_seed'] : rand(0, PHP_INT_MAX);
        $_SESSION['pm_random_seed'] = $seed;

        // Voeg JavaScript toe die alle 'Load More' knoppen aanpast
        wp_add_inline_script( 'jquery-core', "
            jQuery(document).ready(function($) {
                var seed = " . intval($seed) . ";
                $(document).on('click', '.wd-load-more', function(e) {
                    var btn = $(this);
                    var url = btn.data('ajax-url') || '';
                    if(url && url.indexOf('rand_seed') === -1) {
                        url += (url.indexOf('?') !== -1 ? '&' : '?') + 'rand_seed=' + seed;
                        btn.data('ajax-url', url);
                    }
                });
            });
        " );
    }
});

