<?php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
    <?php wp_head(); ?>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
    <style>
        .individual-tours {
            padding: 4rem 0;
            background-color: #f9f9f9;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 1rem;
        }

        .individual-tours .container {
            max-width: 100%;
            padding: 0 2rem;
        }

        .individual-tours h2 {
            text-align: center;
            margin-bottom: 2rem;
            color: #333;
            font-size: 2.5rem;
            max-width: 1200px;
            margin-left: auto;
            margin-right: auto;
        }

        .tours-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, 350px);
            gap: 2rem;
            padding: 1rem;
            justify-content: center;
            width: 100%;
            max-width: 1800px;
            margin: 0 auto;
        }

        .tour-card {
            width: 350px;
            background: white;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease;
            height: 420px;
            display: flex;
            flex-direction: column;
        }

        .tour-card:hover {
            transform: translateY(-5px);
        }

        .tour-card .tour-image {
            width: 100%;
            height: 200px;
            position: relative;
            overflow: hidden;
        }

        .tour-card .tour-image img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            transition: transform 0.3s ease;
        }

        .tour-card:hover .tour-image img {
            transform: scale(1.1);
        }

        .tour-card .tour-info {
            padding: 1.5rem;
            display: flex;
            flex-direction: column;
            flex: 1;
            justify-content: space-between;
        }

        .tour-card h3 {
            margin: 0 0 1rem;
            color: #333;
            font-size: 1.25rem;
            line-height: 1.4;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }

        .tour-card p {
            color: #666;
            margin-bottom: 1rem;
            display: -webkit-box;
            -webkit-line-clamp: 3;
            -webkit-box-orient: vertical;
            overflow: hidden;
            line-height: 1.6;
        }

        .tour-card .tour-details {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 1rem;
            margin-top: auto;
        }

        .tour-card .price {
            font-weight: bold;
            color: #2c3e50;
            font-size: 1.2rem;
        }

        .tour-card .duration {
            color: #666;
        }

        .tour-card .btn-view {
            display: block;
            text-align: center;
            padding: 0.8rem;
            background-color: var(--accent);
            color: white;
            text-decoration: none;
            border-radius: 4px;
            transition: background-color 0.3s ease;
            margin-top: auto;
        }

        .tour-card .btn-view:hover {
            background-color: var(--accent-dark);
        }

        .no-tours {
            text-align: center;
            padding: 2rem;
            margin: 1rem 0;
            border-radius: 8px;
            font-size: 1.1rem;
            background-color: #f8f9fa;
            color: #6c757d;
            border: 1px solid #dee2e6;
            grid-column: 1 / -1;
        }

        @media (max-width: 768px) {
            .individual-tours h2 {
                font-size: 2rem;
            }

            .tours-grid {
                grid-template-columns: 1fr;
            }

            .tour-card {
                width: 100%;
                height: auto;
                min-height: 450px;
            }
        }
    </style>
</head>
<body <?php body_class(); ?>>

<?php get_header(); ?>

<!-- Individual Tours -->
<section class="individual-tours">
    <div class="container">
        <h2><?php 
            $page_title = get_post_meta(get_the_ID(), '_page_title', true);
            echo esc_html($page_title ? $page_title : get_the_title()); 
        ?></h2>
        <div class="tours-grid">
            <?php
            $journey_id = get_the_ID();
            
            // Pridobi shranjen vrstni red
            $saved_order = get_option('individual_tour_order_' . $journey_id, '');
            $order_array = !empty($saved_order) ? explode(',', $saved_order) : array();
            
            // Definiraj query argumente
            $args = array(
                'post_type' => 'individual_tour',
                'posts_per_page' => -1,
                'meta_key' => '_experience_journey',
                'meta_value' => $journey_id,
                'meta_compare' => '='
            );
            
            // Če imamo shranjen vrstni red, uporabi post__in in orderby
            if (!empty($order_array)) {
                $args['post__in'] = $order_array;
                $args['orderby'] = 'post__in';
            }
            
            $tours = new WP_Query($args);
            
            // Debug v WordPress log
            error_log('=== EXPERIENCE JOURNEY DEBUG ===');
            error_log('Journey ID: ' . $journey_id);
            error_log('Found posts: ' . $tours->found_posts);
            error_log('Post count: ' . $tours->post_count);
            error_log('Max num pages: ' . $tours->max_num_pages);
            error_log('Query SQL: ' . $tours->request);
            error_log('Saved order array: ' . print_r($order_array, true));
            error_log('=== END DEBUG ===');
            
            // Dodajmo tudi vizualni debug za administratorje
            if (current_user_can('administrator')) {
                echo '<div style="background: #f1f1f1; padding: 20px; margin: 20px; border: 1px solid #ddd;">';
                echo '<h3>Debug Info (visible only to admins):</h3>';
                echo '<p>Found posts: ' . $tours->found_posts . '</p>';
                echo '<p>Post count: ' . $tours->post_count . '</p>';
                echo '<p>Journey ID: ' . $journey_id . '</p>';
                echo '</div>';
            }
            
            if ($tours->have_posts()) :
                while ($tours->have_posts()) : $tours->the_post();
                    // Pridobi hero sliko
                    $hero_image_id = get_post_meta(get_the_ID(), '_hero_image', true);
                    $image = wp_get_attachment_image_url($hero_image_id, 'full');
                    // Če hero slika ne obstaja, uporabi featured image
                    if (!$image) {
                        $image = get_the_post_thumbnail_url(get_the_ID(), 'large');
                    }
                    // Če ni nobene slike, uporabi placeholder
                    if (!$image) {
                        $image = get_theme_file_uri('images/placeholder.jpg');
                    }
                    $price = get_post_meta(get_the_ID(), '_tour_price', true);
                    $duration = get_post_meta(get_the_ID(), '_tour_duration', true);
            ?>
                <div class="tour-card">
                    <div class="tour-image">
                        <img src="<?php echo esc_url($image); ?>" 
                             alt="<?php echo esc_attr(get_the_title()); ?>" 
                             loading="lazy">
                    </div>
                    <div class="tour-info">
                        <div>
                            <h3><?php echo esc_html(get_the_title()); ?></h3>
                            <p><?php echo esc_html(get_the_excerpt()); ?></p>
                        </div>
                        <div>
                            <div class="tour-details">
                                <?php if ($price) : ?>
                                    <span class="price">€<?php echo esc_html($price); ?></span>
                                <?php endif; ?>
                                <?php if ($duration) : ?>
                                    <span class="duration"><?php echo esc_html($duration); ?> days</span>
                                <?php endif; ?>
                            </div>
                            <a href="<?php echo esc_url(get_permalink()); ?>" class="btn-view">View Details</a>
                        </div>
                    </div>
                </div>
            <?php
                endwhile;
                wp_reset_postdata();
            else :
            ?>
                <p class="no-tours">Currently no individual tours in this category.</p>
            <?php endif; ?>
        </div>
    </div>
</section>

<?php get_footer(); ?>

</body>
</html> 