/******* Do not edit this file *******
Woody Code Snippets CSS and JS
Saved: May 29 2025 | 19:28:27 */
jQuery(document).ready(function($) {
    var currentOrderData = {};

    $('#order-popup').on('click', function(e) {
        e.stopPropagation();
    });

    $('.open-order-popup').on('click', function(e) {
        e.preventDefault();
        var orderId = $(this).data('order-id');
        fetchOrderDetails(orderId);
    });

    function fetchOrderDetails(orderId) {
        $.ajax({
            url: orderPopupAjax.ajaxurl,
            type: 'POST',
            data: {
                action: 'get_order_details',
                order_id: orderId,
                nonce: orderPopupAjax.nonce
            },
            success: function(response) {
                if (response.success) {
                    currentOrderData = response.data;
                    currentOrderData.order_id = orderId;
                    updateOrderDetails();
                } else {
                    alert(response.data.message || 'Virhe tilaustietojen haussa');
                }
            },
            error: function(xhr, status, error) {
                console.error('AJAX Error:', error);
                alert('Virhe tilaustietojen haussa. Yritä uudelleen.');
            }
        });
    }

    function updateOrderDetails() {
        var html = '<div class="order-columns">';
        html += '<div class="order-column">';
        html += '<h3>Asiakas</h3>';
        html += '<p><strong>Nimi:</strong> ' + currentOrderData.customer.name + '</p>';
        html += '<p><strong>Sähköposti:</strong> ' + currentOrderData.customer.email + '</p>';
        html += '<p><strong>Puhelin:</strong> ' + currentOrderData.customer.phone + '</p>';
        html += '<p><strong>Laskutusosoite:</strong> ' + currentOrderData.customer.billing_address + '</p>';
        html += '</div>';
        html += '<div class="order-column">';
        html += '<h3>Toimitustiedot</h3>';
        html += '<p><strong>Toimitusosoite:</strong> ' + currentOrderData.customer.shipping_address + '</p>';
        html += '<p><strong>Maksutapa:</strong> ' + currentOrderData.payment_method + '</p>';
        html += '<p><strong>Toimitustapa:</strong> ' + currentOrderData.shipping_method + '</p>';
        html += '</div>';
        html += '</div>';
        html += '<h3>Tilatut tuotteet</h3>';
        $.each(currentOrderData.items, function(index, item) {
            html += '<p><strong>' + item.name + '</strong> (' + item.quantity + ' kpl) - ' + item.total + '</p>';
            if (item.addons && item.addons.length) {
                $.each(item.addons, function(key, addon) {
                    html += '<p style="margin-left:20px;">+ ' + addon.name + ': ' + addon.value + '</p>';
                });
            }
            if (item.extra && item.extra.trim() !== '') {
                html += '<div>' + item.extra + '</div>';
            }
        });
        html += '<h3>Tilauksen yhteenveto</h3>';
        html += '<p><strong>Kohteiden välisumma:</strong> ' + currentOrderData.subtotal + '</p>';
        html += '<p><strong>Toimitus:</strong> ' + currentOrderData.shipping_total + '</p>';
        html += '<p><strong>ALV:</strong> ' + currentOrderData.tax_total + '</p>';
        html += '<p><strong>Tilaus yhteensä:</strong> ' + currentOrderData.total + '</p>';
        html += '<h3 id="order-status-title">Tilauksen tila</h3>';
        html += '<select id="order-status-select">';
        currentOrderData.status_options.forEach(function(status) {
            var selected = (currentOrderData.status === status.value) ? 'selected' : '';
            html += '<option value="' + status.value + '" ' + selected + '>' + status.label + '</option>';
        });
        html += '</select>';
        html += '<span id="order-status-message" style="margin-left:10px;color:green;"></span>';
        setTimeout(function() {
            $('#order-status-select').off('change').on('change', function() {
                var newStatus = $(this).val();
                $('#order-status-message').text('Päivitetään...');
                updateOrderStatus(newStatus);
            });
        }, 100);

        $('#order-details').html(html).show();
        openPopup();
    }

    function updateOrderStatus(newStatus) {
        $.ajax({
            url: orderPopupAjax.ajaxurl,
            type: 'POST',
            data: {
                action: 'update_order_status',
                order_id: currentOrderData.order_id,
                new_status: newStatus,
                nonce: orderPopupAjax.nonce
            },
            success: function(resp) {
                if (resp.success) {
                    $('#order-status-message').text('Tila päivitetty!');
                    currentOrderData.status = newStatus;
                } else {
                    $('#order-status-message')
                        .css('color','red')
                        .text('Virhe: ' + (resp.data && resp.data.message ? resp.data.message : 'Tuntematon virhe'));
                }
                setTimeout(function(){ 
                    $('#order-status-message').text('').css('color','green'); 
                }, 3000);
            },
            error: function(xhr, status, error) {
                console.error('AJAX Error:', error);
                $('#order-status-message')
                    .css('color','red')
                    .text('Virhe tilan päivityksessä. Yritä uudelleen.');
            }
        });
    }

    function openPopup() {
        $('#order-overlay').fadeIn();
        $('#order-popup').css("opacity", "1").fadeIn();
    }

    function closePopup() {
        $('#order-popup').fadeOut(function() {
            $(this).css("opacity", "0");
        });
        $('#order-overlay').fadeOut();
    }

    $('#close-popup, #order-overlay').on('click', function(e) {
        e.preventDefault();
        closePopup();
    });

    $('#print-order').on('click', function(e) {
        e.preventDefault();
        window.print();
    });
});