';
$('#products-table-container').html(messageHtml);
}
// Event handlers
$(document)
.on('change', '.quote-quantity', function() {
var productId = $(this).data('product-id');
var quantity = parseInt($(this).val()) || 1;
// Update the item in the array
var itemIndex = quoteItems.findIndex(item => item.id == productId);
if (itemIndex >= 0) {
quoteItems[itemIndex].quantity = quantity;
localStorage.setItem('quoteItems', JSON.stringify(quoteItems));
updateQuoteDisplay();
}
})
.on('change', '.target-price-input', function() {
var productId = $(this).data('product-id');
var targetPrice = $(this).val();
// Update the item in the array
var itemIndex = quoteItems.findIndex(item => item.id == productId);
if (itemIndex >= 0) {
quoteItems[itemIndex].targetPrice = targetPrice;
localStorage.setItem('quoteItems', JSON.stringify(quoteItems));
}
})
.on('click', '.remove-quote-item', function() {
var productId = $(this).data('product-id');
quoteItems = quoteItems.filter(item => item.id != productId);
localStorage.setItem('quoteItems', JSON.stringify(quoteItems));
updateQuoteDisplay();
});
$('#clear-all-quote, #remove-all-btn').click(function() {
if (confirm('Are you sure you want to remove all items from your quote?')) {
quoteItems = [];
localStorage.setItem('quoteItems', JSON.stringify(quoteItems));
updateQuoteDisplay();
}
});
// Form submission
$('#quote-request-form-element').on('submit', function(e) {
e.preventDefault();
if (quoteItems.length === 0 && !uploadedBOMFile) {
alert('Please add some products to your quote or upload a BOM file first.');
return;
}
var formData = {
action: 'submit_quote_request',
nonce: quote_ajax.nonce,
contact_name: $('input[name="contact_name"]').val(),
company_name: $('input[name="company_name"]').val(),
business_email: $('input[name="business_email"]').val(),
telephone: $('input[name="telephone"]').val(),
country: $('input[name="country"]').val(),
comment: $('textarea[name="comment"]').val(),
quote_items: JSON.stringify(quoteItems)
};
// Add BOM file info if uploaded
if (uploadedBOMFile) {
formData.bom_filename = uploadedBOMFile.filename;
}
var submitButton = $(this).find('button[type="submit"]');
submitButton.text('Submitting...').prop('disabled', true);
console.log('Submitting quote request:', formData);
$.ajax({
url: quote_ajax.ajax_url,
type: 'POST',
data: formData,
success: function(response) {
console.log('Submit response:', response);
if (response.success) {
alert('Quote request submitted successfully! We will contact you within 24 hours.');
// Clear localStorage and uploaded file
localStorage.removeItem('quoteItems');
uploadedBOMFile = null;
// Redirect to home or success page
window.location.href = '/';
} else {
alert('Error: ' + response.data);
}
submitButton.text('Submit RFQ').prop('disabled', false);
},
error: function(xhr, status, error) {
console.log('Submit error:', xhr, status, error);
alert('Error submitting quote request. Please try again.');
submitButton.text('Submit RFQ').prop('disabled', false);
}
});
});
function showNotification(message, isError) {
var bgColor = isError ? '#ff4444' : '#28a745';
var notification = $('
' + message + '
');
$('body').append(notification);
setTimeout(function() {
notification.fadeOut(function() {
notification.remove();
});
}, 3000);
}
});