jQuery(document).ready(function($) { var currentStep = 1; var calculatedPrice = 0; var bookingData = {}; // Navegação entre etapas $(document).on('click', '.btn-next', function() { var nextStep = $(this).data('next'); showStep(nextStep); }); $(document).on('click', '.btn-prev', function() { var prevStep = $(this).data('prev'); showStep(prevStep); }); function showStep(step) { $('.booking-step').hide(); $('.booking-step[data-step="' + step + '"]').fadeIn(300); currentStep = step; $('html, body').animate({ scrollTop: $('.booking-petshop-wrapper').offset().top - 50 }, 300); } // Cálculo dinâmico de preço function calculatePrice() { var productId = $('.booking-petshop-wrapper').data('product-id'); var attributes = { 'Logistica': $('select[name="Logistica"]').val(), 'Modalidade': $('select[name="Modalidade"]').val(), 'Serviço': $('select[name="Serviço"]').val(), 'Porte': $('select[name="Porte"]').val() }; $.ajax({ url: bookingData.ajaxUrl, type: 'POST', data: { action: 'booking_calculate_price', nonce: bookingData.nonce, product_id: productId, attributes: JSON.stringify(attributes) }, success: function(response) { if (response.success) { calculatedPrice = response.data.price; $('#booking-price').text(formatCurrency(response.data.price)); $('#booking-price-value').val(response.data.price); } else { $('#booking-price').text('Combinação indisponível'); } } }); } // Recalcular ao mudar qualquer atributo $('select[name="Logistica"], select[name="Modalidade"], select[name="Serviço"], select[name="Porte"]').on('change', function() { calculatePrice(); }); // Calcular preço inicial calculatePrice(); // Formatar moeda function formatCurrency(value) { return 'R$ ' + parseFloat(value).toFixed(2).replace('.', ',').replace(/\B(?=(\d{3})+(?!\d))/g, '.'); } // Coletar dados do booking function collectBookingData() { return { product_id: $('.booking-petshop-wrapper').data('product-id'), customer_name: $('input[name="customer_name"]').val(), customer_email: $('input[name="customer_email"]').val(), customer_phone: $('input[name="customer_phone"]').val(), pet_name: $('input[name="pet_name"]').val(), pet_breed: $('input[name="pet_breed"]').val(), service: $('select[name="Serviço"]').val(), porte: $('select[name="Porte"]').val(), modalidade: $('select[name="Modalidade"]').val(), logistica: $('select[name="Logistica"]').val(), reservation_date: $('input[name="data_atendimento"]').val(), reservation_time: $('select[name="horario_atendimento"]').val(), professional: $('select[name="profissional"]').val(), observations: $('textarea[name="observacoes"]').val(), price: calculatedPrice }; } // Submissão do formulário - Integração com Checkout $('#booking-form').on('submit', function(e) { e.preventDefault(); bookingData = collectBookingData(); var $submitBtn = $('.btn-submit'); $submitBtn.prop('disabled', true).text('Processando...'); // Verificar se estamos em uma página de checkout WooCommerce if ($('.woocommerce-checkout').length > 0) { // Adicionar dados ao carrinho e prosseguir para checkout addToCartAndCheckout(bookingData); } else { // Criar reserva diretamente createReservationDirect(bookingData); } }); // Adicionar ao carrinho WooCommerce function addToCartAndCheckout(data) { var productId = $('.booking-petshop-wrapper').data('product-id'); // Aqui você precisa mapear o product_id do booking para o product_id do WooCommerce // Exemplo: var wcProductId = getWooCommerceProductId(productId); $.ajax({ url: wc_add_to_cart_params.wc_ajax_url.replace('%%endpoint%%', 'add_to_cart'), type: 'POST', data: { product_id: productId, // Substituir pelo ID do produto WooCommerce quantity: 1, booking_data: JSON.stringify(data) }, success: function(response) { if (response.error && response.product_url) { alert('Erro ao adicionar ao carrinho'); } else { // Redirecionar para checkout window.location.href = wc_checkout_params.checkout_url; } }, error: function() { alert('Erro de conexão'); $submitBtn.prop('disabled', false).text('✓ Confirmar Agendamento'); } }); } // Criar reserva diretamente (sem WooCommerce) function createReservationDirect(data) { $.ajax({ url: bookingData.ajaxUrl, type: 'POST', data: { action: 'booking_create_reservation', nonce: bookingData.nonce, ...data }, success: function(response) { if (response.success) { $('#booking-protocol').text('#' + response.data.reservation_id); showStep(4); // Disparar evento para sistemas externos $(document).trigger('booking_completed', [response.data]); } else { alert(response.data.message || 'Erro ao criar agendamento'); $submitBtn.prop('disabled', false).text('✓ Confirmar Agendamento'); } }, error: function() { alert('Erro de conexão. Tente novamente.'); $submitBtn.prop('disabled', false).text('✓ Confirmar Agendamento'); } }); } // Integração com sistema de checkout externo window.integrateWithExternalCheckout = function(checkoutData) { var data = collectBookingData(); return $.ajax({ url: bookingData.apiUrl + 'reservations', type: 'POST', contentType: 'application/json', data: JSON.stringify({ booking_data: data, checkout_data: checkoutData }) }); }; // Verificar disponibilidade via API window.checkAvailability = function(professional, date, timeSlot) { return $.ajax({ url: bookingData.apiUrl + 'availability', type: 'GET', data: { professional: professional, date: date, time_slot: timeSlot } }); }; });