<?php
/**
 * Order bump template for tour extras
 */

defined('ABSPATH') || exit;

// Debug informacije
error_log('Order bump template loaded');

// Pridobi extras iz košarice
$cart_items = WC()->cart->get_cart();
error_log('Cart items: ' . print_r($cart_items, true));

$tour_id = null;
$available_extras = array();

// Poišči tour ID iz prvega izdelka v košarici
foreach ($cart_items as $cart_item) {
    if (isset($cart_item['product_id'])) {
        $product = wc_get_product($cart_item['product_id']);
        if ($product) {
            $sku = $product->get_sku();
            if (strpos($sku, 'tour-') === 0) {
                $tour_id = str_replace('tour-', '', $sku);
                break;
            }
        }
    }
}

// Če smo našli tour ID, pridobimo njegove extras
if ($tour_id) {
    $available_extras = get_post_meta($tour_id, '_available_extras', true);
}

// Če ni extras-ov, ne prikazujemo ničesar
if (empty($available_extras)) {
    return;
}
?>

<div class="order-bump-section">
    <h3>Available Extras</h3>
    <p class="order-bump-description">Enhance your tour experience with these additional services:</p>
    
    <div class="order-bump-items">
        <?php foreach ($available_extras as $index => $extra) : ?>
            <div class="order-bump-item">
                <label class="order-bump-label">
                    <input type="checkbox" 
                           name="tour_extras[]" 
                           value="<?php echo esc_attr($index); ?>"
                           data-price="<?php echo esc_attr($extra['price']); ?>"
                           data-name="<?php echo esc_attr($extra['name']); ?>"
                           class="tour-extra-checkbox">
                    <span class="extra-name"><?php echo esc_html($extra['name']); ?></span>
                    <span class="extra-price">€<?php echo number_format($extra['price'], 2); ?></span>
                </label>
            </div>
        <?php endforeach; ?>
    </div>
</div>

<style>
.order-bump-section {
    margin: 2rem 0;
    padding: 1.5rem;
    background-color: #f8f9fa;
    border-radius: 8px;
    border: 1px solid #e9ecef;
}

.order-bump-section h3 {
    margin-bottom: 1rem;
    color: #2c3e50;
    font-size: 1.2rem;
}

.order-bump-description {
    margin-bottom: 1.5rem;
    color: #6c757d;
    font-size: 0.95rem;
}

.order-bump-items {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.order-bump-item {
    background: white;
    padding: 1rem;
    border-radius: 6px;
    border: 1px solid #dee2e6;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.order-bump-item:hover {
    transform: translateY(-2px);
    box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}

.order-bump-label {
    display: flex;
    align-items: center;
    gap: 1rem;
    cursor: pointer;
}

.tour-extra-checkbox {
    margin: 0;
}

.extra-name {
    flex-grow: 1;
    color: #2c3e50;
}

.extra-price {
    color: var(--accent);
    font-weight: 500;
    white-space: nowrap;
}

@media (max-width: 768px) {
    .order-bump-section {
        padding: 1rem;
    }
    
    .order-bump-item {
        padding: 0.8rem;
    }
}
</style>

<script>
jQuery(document).ready(function($) {
    // Funkcija za posodobitev cene v košarici
    function updateCartWithExtras() {
        var extras = [];
        $('.tour-extra-checkbox:checked').each(function() {
            extras.push({
                index: $(this).val(),
                name: $(this).data('name'),
                price: $(this).data('price')
            });
        });
        
        // Pošlji AJAX zahtevek za posodobitev košarice
        $.ajax({
            url: wc_checkout_params.ajax_url,
            type: 'POST',
            data: {
                action: 'update_cart_extras',
                extras: extras,
                security: wc_checkout_params.update_order_review_nonce
            },
            success: function(response) {
                if (response.success) {
                    // Posodobi checkout
                    $('body').trigger('update_checkout');
                }
            }
        });
    }
    
    // Poslušaj spremembe na checkboxih
    $(document).on('change', '.tour-extra-checkbox', function() {
        updateCartWithExtras();
    });
});
</script> 