console.log('SLBD offer management JS loaded');
console.log('Offer tabs found', document.querySelectorAll('[data-offers-tab]').length);
document.addEventListener('click', function(event){
const tab=event.target.closest('[data-offers-tab]');
if(!tab){
return;
}
event.preventDefault();
const target=tab.getAttribute('data-offers-tab');
console.log('Offer tab clicked', target);
document.querySelectorAll('[data-offers-tab]').forEach(function(button){
const isActive=button===tab;
button.classList.toggle('is-active', isActive);
button.setAttribute('aria-selected', isActive ? 'true':'false');
});
document.querySelectorAll('[data-offers-panel]').forEach(function(panel){
const isActive=panel.getAttribute('data-offers-panel')===target;
panel.classList.toggle('is-active', isActive);
panel.hidden = !isActive;
});
});
jQuery(document).ready(function($){
var ajax_nonce=ajax_object.nonce;
var currentCounterIsSeller=false;
function setActionLoading($button, loadingText){
if($button.data('slbdOfferLoading')){
return false;
}
const $relatedButtons=$button.closest('.slbd-offer-management-helper-actions').find('.slbd-offer-management-helper-btn').not($button);
$button.data('slbdOfferLoading', true);
$button.data('slbdOfferOriginalHtml', $button.html());
$button.data('slbdOfferRelatedButtons', $relatedButtons);
$button
.prop('disabled', true)
.attr('aria-busy', 'true')
.addClass('slbd-offer-action-is-loading slbd-offer-action-disabled')
.text(loadingText);
$relatedButtons
.prop('disabled', true)
.addClass('slbd-offer-action-disabled');
return true;
}
function resetActionLoading($button){
const originalHtml=$button.data('slbdOfferOriginalHtml');
const $relatedButtons=$button.data('slbdOfferRelatedButtons');
$button
.prop('disabled', false)
.removeAttr('aria-busy')
.removeClass('slbd-offer-action-is-loading slbd-offer-action-disabled')
.data('slbdOfferLoading', false);
if(originalHtml){
$button.html(originalHtml);
}
if($relatedButtons&&$relatedButtons.length){
$relatedButtons
.prop('disabled', false)
.removeClass('slbd-offer-action-disabled');
}}
$(document).on('click', '.slbd-offer-management-helper-btn.slbd-offer-management-helper-accept-btn', function(e){
e.preventDefault();
const $button=$(this);
if(!setActionLoading($button, 'Accepting...')){
return;
}
let offerId=$(this).data('offer-id');
let productId=$(this).data('product-id');
console.log("Accept button clicked for offer ID:", offerId);
$.ajax({
url: ajax_object.ajax_url,
method: 'POST',
data: {
action: 'handle_offer_action',
offer_id: offerId,
product_id: productId,
offer_action: 'accept',
security: ajax_nonce
},
success: function(response){
console.log("Accept response:", response);
if(response.success){
if(response.data.checkout_url){
window.location.href=response.data.checkout_url;
}else{
location.reload();
}}else{
resetActionLoading($button);
alert(response.data.message||"Failed to accept offer.");
}},
error: function(){
resetActionLoading($button);
alert("An error occurred. Please try again.");
}});
});
$(document).on('click', '.slbd-offer-management-helper-btn.slbd-offer-management-helper-decline-btn', function(e){
e.preventDefault();
const $button=$(this);
if(!setActionLoading($button, 'Declining...')){
return;
}
let offerId=$(this).data('offer-id');
let productId=$(this).data('product-id');
console.log("Decline button clicked for offer ID:", offerId);
$.post(ajax_object.ajax_url, {
action: 'handle_offer_action',
offer_id: offerId,
product_id: productId,
offer_action: 'decline',
security: ajax_nonce
}, function(response){
console.log("Decline response:", response);
if(response.success){
location.reload();
}else{
resetActionLoading($button);
alert(response.data.message||"Failed to decline offer.");
}}).fail(function(){
resetActionLoading($button);
alert("An error occurred. Please try again.");
});
});
$(document).on('click', '.slbd-offer-management-helper-btn.slbd-offer-management-helper-counter-btn', function(e){
e.preventDefault();
if($(this).data('slbdOfferLoading')){
return;
}
const offerId=$(this).data('offer-id');
const productId=$(this).data('product-id');
const isSeller=String($(this).attr('data-is-seller'))==='1';
const expirationDate=$(this).attr('data-expiration-date')||'';
console.log("Counter button clicked for offer ID:", offerId, "and product ID:", productId);
currentCounterIsSeller=isSeller;
$('#offer-id').val(offerId);
$('#product-id').val(productId);
$('#offer-expiration-date').val(expirationDate);
$('.slbd-offer-expiration-field').css('display', isSeller ? 'block':'none');
$('#counterOfferModal').modal('show');
});
$('#submit-counter').on('click', function(e){
e.preventDefault();
const $button=$(this);
const offerId=$('#offer-id').val();
const counterAmount=$('#counter-amount').val();
const productId=$('#product-id').val();
const expirationDate=currentCounterIsSeller ? $('#offer-expiration-date').val():'';
console.log("Submitting counter offer for offer ID:", offerId, "with amount:", counterAmount);
if(counterAmount){
if(!setActionLoading($button, 'Sending...')){
return;
}
$.post(ajax_object.ajax_url, {
action: 'handle_offer_action',
offer_id: offerId,
offer_action: 'counter',
counter_amount: counterAmount,
product_id: productId,
offer_expiration_date: expirationDate,
security: ajax_nonce
}, function(response){
console.log("Counter offer response:", response);
if(response.success){
$('#counterOfferModal').modal('hide');
location.reload();
}else{
resetActionLoading($button);
alert(response.data.message||"Failed to counter offer.");
}}).fail(function(){
resetActionLoading($button);
alert("An error occurred. Please try again.");
});
}else{
alert("Please enter a valid counter amount.");
}});
$(document).on('click', '.slbd-offer-management-helper-btn.slbd-offer-management-helper-pay-now-btn', function(e){
e.preventDefault();
let offerId=$(this).data('offer-id');
let productId=$(this).data('product-id');
let $button=$(this);
if(!setActionLoading($button, 'Loading payment...')){
return;
}
$.post(ajax_object.ajax_url, {
action: 'handle_offer_action',
offer_id: offerId,
product_id: productId,
offer_action: 'pay_now',
security: ajax_nonce
}, function(response){
if(response.success){
window.location.href=response.data.checkout_url;
}else{
alert(response.data.message||'An error occurred. Please try again.');
resetActionLoading($button);
}}).fail(function(){
alert("An error occurred. Please try again.");
resetActionLoading($button);
});
});
$('#counterOfferModal').on('hidden.bs.modal', function (){
$('#counter-amount').val('');
$('#offer-expiration-date').val('');
resetActionLoading($('#submit-counter'));
currentCounterIsSeller=false;
$('.slbd-offer-expiration-field').css('display', 'none');
$('body').removeClass('modal-open');
$('.modal-backdrop').remove();
});
$('#counterOfferModal .cancel-btn').on('click', function (e){
e.preventDefault();
$('#counterOfferModal').modal('hide');
});
function getMakeOfferNumber($field){
if(!$field.length){
return 0;
}
try {
if($.fn.autoNumerics&&$field.data('autoNumeric')){
return Number($field.autoNumerics('get'))||0;
}} catch (error){
}
return Number(String($field.val()||'').replace(/[^0-9.]/g, ''))||0;
}
function setMakeOfferMessage(type, message){
let $message=$('.slbd-make-offer-message');
if(!$message.length){
$message=$('<div class="slbd-make-offer-message" role="status" aria-live="polite"></div>');
$('#woocommerce-make-offer-form').prepend($message);
}
$message
.removeClass('slbd-make-offer-message--error slbd-make-offer-message--success')
.addClass('slbd-make-offer-message--' + type)
.html(message)
.show();
}
function resetMakeOfferSubmit($form){
const $submit=$form.find(':submit').first();
const originalHtml=$submit.data('slbdMakeOfferOriginalHtml');
$form.data('slbdMakeOfferSubmitting', false);
$submit
.prop('disabled', false)
.removeAttr('aria-busy')
.removeClass('slbd-make-offer-submit-is-loading');
if(originalHtml){
$submit.html(originalHtml);
}}
function setMakeOfferSubmitFinal($form){
const $submit=$form.find(':submit').first();
$form.data('slbdMakeOfferSubmitting', false);
$form.data('slbdMakeOfferSucceeded', true);
$submit
.prop('disabled', true)
.removeAttr('aria-busy')
.removeClass('slbd-make-offer-submit-is-loading')
.text('Offer submitted');
}
function escapeMakeOfferText(value){
return $('<div>').text(value||'').html();
}
function closeMakeOfferModal($form){
$('#lightbox_custom_ofwc_offer_form')
.removeClass('active')
.hide();
$('#lightbox_custom_ofwc_offer_form_close_btn').hide();
resetMakeOfferSubmit($form);
}
function getMakeOfferContext(){
return window.slbd_make_offer_context||{};}
function getMakeOfferFieldWrapper($input){
if(!$input.length){
return $();
}
let $field=$input.closest('p, .form-row, .woocommerce-make-offer-form-section, .form-group').first();
if(!$field.length){
$field=$input.parent();
}
const hasOtherInputs=$field.find(':input').not($input).length > 0;
if(($field.is('form, fieldset')||hasOtherInputs)&&$input.parent().length&&!$input.parent().is('form')){
$field=$input.parent();
}
return $field;
}
function hideMakeOfferField($input, $form){
const $field=getMakeOfferFieldWrapper($input);
if(!$field.length){
return;
}
if($field.is('form, fieldset')||$field.find(':input').not($input).length > 0){
const inputId=$input.attr('id');
if(inputId){
$form.find('label[for="' + inputId + '"]').addClass('slbd-make-offer-field-hidden');
}
$input.addClass('slbd-make-offer-field-hidden');
return;
}
$field.addClass('slbd-make-offer-field-hidden');
}
function initializeMakeOfferAmountInput($input){
if(!$input.length||!$.fn.autoNumerics){
return;
}
try {
$input.autoNumerics('init', {
aForm: false,
aSep: window.offers_for_woocommerce_js_params ? offers_for_woocommerce_js_params.ofw_public_js_thousand_separator:',',
aDec: window.offers_for_woocommerce_js_params ? offers_for_woocommerce_js_params.ofw_public_js_decimal_separator:'.',
vMin: '0.00',
lZero: 'allow',
wEmpty: 'sign',
mDec: window.offers_for_woocommerce_js_params ? offers_for_woocommerce_js_params.ofw_public_js_number_of_decimals:'2',
aSign: ''
});
} catch (error){
}}
function ensureMakeOfferAmountField($form){
let $offerAmount=$form.find('[name="offer_price_each"]').first();
if($offerAmount.length){
return $offerAmount;
}
const amountHtml='' +
'<p class="form-row slbd-make-offer-field slbd-make-offer-amount-field slbd-make-offer-restored-amount-field">' +
'<label for="woocommerce-make-offer-form-price-each">Your offer</label>' +
'<span class="slbd-make-offer-money-field">' +
'<span class="slbd-make-offer-money-field__prefix">$</span>' +
'<input type="text" id="woocommerce-make-offer-form-price-each" name="offer_price_each" value="" required min="0.01" step="0.01" inputmode="decimal" autocomplete="off">' +
'<span class="slbd-make-offer-money-field__suffix">AUD</span>' +
'</span>' +
'</p>';
const $name=$form.find('[name="offer_name"]').first();
const $actions=$form.find('.slbd-make-offer-actions, :submit').first();
if($name.length){
getMakeOfferFieldWrapper($name).before(amountHtml);
}else if($actions.length){
const $actionsWrapper=$actions.closest('.slbd-make-offer-actions');
if($actionsWrapper.length){
$actionsWrapper.before(amountHtml);
}else{
$actions.before(amountHtml);
}}else{
$form.append(amountHtml);
}
$offerAmount=$form.find('[name="offer_price_each"]').first();
initializeMakeOfferAmountInput($offerAmount);
return $offerAmount;
}
function makeOfferLog(){
if(window.slbdMakeOfferDebug&&window.console&&typeof window.console.debug==='function'){
window.console.debug.apply(window.console, arguments);
}}
function isMakeOfferAjaxRequest(settings){
if(!settings||!settings.data){
return false;
}
if(typeof settings.data==='string'){
return settings.data.indexOf('action=new_offer_form_submit')!==-1;
}
return settings.data.action==='new_offer_form_submit';
}
function parseMakeOfferAjaxResponse(jqXHR){
if(jqXHR&&jqXHR.responseJSON){
return jqXHR.responseJSON;
}
const raw=jqXHR ? String(jqXHR.responseText||'').trim():'';
if(!raw){
return {};}
try {
return JSON.parse(raw);
} catch (error){
return {
raw: raw
};}}
function getMakeOfferResponseError(parsed){
if(!parsed){
return '';
}
const status=String(parsed.statusmsg||parsed.result||parsed.status||'').toLowerCase();
const message=parsed.statusmsgDetail||parsed.message||'';
if(parsed.success===false||status.indexOf('failed')!==-1||status==='error'){
return message||'We could not submit your offer. Please check the form and try again.';
}
return '';
}
function showMakeOfferSuccess($form){
const context=getMakeOfferContext();
const offersUrl=context.offers_url||'/my-account/manage-my-offers/';
const successHtml='' +
'<div class="slbd-make-offer-success" role="status" aria-live="polite">' +
'<strong>Your offer has been submitted.</strong>' +
'<p>You can track it from My Account &rarr; Offers.</p>' +
'<div class="slbd-make-offer-success-actions">' +
'<a class="slbd-make-offer-success-link" href="' + escapeMakeOfferText(offersUrl) + '">View my offers</a>' +
'<button type="button" class="slbd-make-offer-success-close">Close</button>' +
'</div>' +
'</div>';
$form.find('.slbd-make-offer-success').remove();
$('#offer-submit-loader, #tab_custom_ofwc_offer_tab_alt_message_2, #tab_custom_ofwc_offer_tab_alt_message_custom, #tab_custom_ofwc_offer_tab_alt_message_success, #aeofwc-popup-counter-box').hide();
$form.find('fieldset, .slbd-make-offer-field, .slbd-make-offer-identity-card, .slbd-make-offer-admin-note, .slbd-make-offer-actions').hide();
$form.append(successHtml);
setMakeOfferSubmitFinal($form);
}
function showMakeOfferError($form, message){
resetMakeOfferSubmit($form);
setMakeOfferMessage('error', message||'We could not submit your offer. Please try again.');
}
function handleMakeOfferAjaxComplete(jqXHR, settings){
if(!isMakeOfferAjaxRequest(settings)){
return;
}
const $form=$('#woocommerce-make-offer-form');
if(!$form.length||!$form.data('slbdMakeOfferSubmitting')||$form.data('slbdMakeOfferSucceeded')){
return;
}
const parsed=parseMakeOfferAjaxResponse(jqXHR);
const contentType=jqXHR&&typeof jqXHR.getResponseHeader==='function' ? jqXHR.getResponseHeader('content-type'):'';
const errorMessage=getMakeOfferResponseError(parsed);
const statusCode=jqXHR ? Number(jqXHR.status||0):0;
makeOfferLog('Make Offer response status', statusCode);
makeOfferLog('Make Offer response content type', contentType||'');
makeOfferLog('Make Offer response summary', parsed&&parsed.raw ? parsed.raw.slice(0, 180):parsed);
if(errorMessage||statusCode >=400||statusCode===0){
makeOfferLog('Make Offer error detected');
showMakeOfferError($form, errorMessage);
return;
}
makeOfferLog('Make Offer success detected');
showMakeOfferSuccess($form);
}
function validateMakeOfferForm($form, context){
const $quantity=$form.find('[name="offer_quantity"]');
let $offerAmount=$form.find('[name="offer_price_each"]').first();
const $name=$form.find('[name="offer_name"]:visible');
const $email=$form.find('[name="offer_email"]:visible');
const quantity=getMakeOfferNumber($quantity);
const offerAmount=getMakeOfferNumber($offerAmount);
const maxQuantity=Number(context.max_quantity||0);
const emailValue=String($email.val()||'').trim();
if(context.is_self_offer){
return context.self_offer_text||'You cannot make an offer on your own listing.';
}
if(!quantity||quantity < 1){
return 'Please enter a quantity of at least 1.';
}
if(maxQuantity&&quantity > maxQuantity){
return 'Please enter a quantity no higher than the available stock.';
}
if(!offerAmount||offerAmount <=0){
return 'Please enter a valid offer amount.';
}
if($name.length&&!String($name.val()||'').trim()){
return 'Please enter your name.';
}
if($email.length&&(!emailValue||!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(emailValue))){
return 'Please enter a valid email address.';
}
return '';
}
function syncMakeOfferSubmitState($form, context){
const $submit=$form.find(':submit').first();
if(!$submit.length||$form.data('slbdMakeOfferSubmitting')||$form.data('slbdMakeOfferSucceeded')){
return;
}
$submit.prop('disabled', !!validateMakeOfferForm($form, context));
}
function addMakeOfferHeader(){
const $card=$('#tab_custom_ofwc_offer_tab_inner');
if(!$card.length){
return;
}
$card.children('.slbd-make-offer-header').remove();
$card.prepend('<div class="slbd-make-offer-header">' +
'<button type="button" class="slbd-make-offer-close" aria-label="Close make an offer modal">×</button>' +
'</div>'
);
$card
.find('h1, h2, h3, h4, legend')
.not('.slbd-make-offer-title')
.addClass('slbd-make-offer-native-title');
}
function addMakeOfferHeading(){
const $card=$('#tab_custom_ofwc_offer_tab_inner');
if(!$card.length){
return;
}
$card.children('.slbd-make-offer-heading').remove();
const headingHtml='' +
'<div class="slbd-make-offer-heading">' +
'<h2 class="slbd-make-offer-title">Make an Offer</h2>' +
'<p class="slbd-make-offer-intro">Complete the form below to submit your offer.</p>' +
'</div>';
const $summary=$card.children('.slbd-make-offer-summary').first();
if($summary.length){
$summary.after(headingHtml);
}else{
$card.children('.slbd-make-offer-header').first().after(headingHtml);
}}
function addMakeOfferProductContext(context){
const $form=$('#woocommerce-make-offer-form');
const $card=$('#tab_custom_ofwc_offer_tab_inner');
if(!$form.length||!$card.length){
return;
}
$('#lightbox_custom_ofwc_offer_form .slbd-make-offer-product').remove();
const image=context.image_url ? '<img src="' + escapeMakeOfferText(context.image_url) + '" alt="">':'';
const stock=context.stock_text ? '<span class="slbd-make-offer-product__stock">' + escapeMakeOfferText(context.stock_text) + '</span>':'';
const price=context.price_html ? '<span class="slbd-make-offer-product__price">' + context.price_html + '</span>':'';
const productHtml='' +
'<div class="slbd-make-offer-product slbd-make-offer-summary">' +
'<div class="slbd-make-offer-product__image">' + image + '</div>' +
'<div class="slbd-make-offer-product__body">' +
'<strong class="slbd-make-offer-product__title">' + escapeMakeOfferText(context.title) + '</strong>' +
'<div class="slbd-make-offer-product__meta">' + price + stock + '</div>' +
'</div>' +
'</div>';
const $header=$card.children('.slbd-make-offer-header').first();
if($header.length){
$header.after(productHtml);
}else{
$card.prepend(productHtml);
}}
function hideNativeMakeOfferCopy(){
const $card=$('#tab_custom_ofwc_offer_tab_inner');
$card.children().each(function(){
const $child=$(this);
if($child.is('form')||$child.find(':input, button, select, textarea').length){
return;
}
const text=String($(this).text()||'').replace(/\s+/g, ' ').trim().toLowerCase();
if(text.indexOf('to make an offer please complete the form below')!==-1){
$child.addClass('slbd-make-offer-native-copy');
}});
$card
.find('p, div, span')
.filter(function(){
const $element=$(this);
const text=String($element.text()||'').replace(/\s+/g, ' ').trim().toLowerCase();
return !$element.find(':input, button, select, textarea').length &&
text==='to make an offer please complete the form below:';
})
.addClass('slbd-make-offer-native-copy');
}
function normalizeMakeOfferAmountLabel($form, $offerAmount){
if(!$offerAmount.length){
return;
}
const amountInputId=$offerAmount.attr('id')||'woocommerce-make-offer-form-price-each';
const $moneyField=$offerAmount.closest('.slbd-make-offer-money-field, .angelleye-input-group');
let $field=$offerAmount.closest('p, .form-row, .woocommerce-make-offer-form-section').first();
if(!$field.length){
$field=$moneyField.length ? $moneyField.parent():$offerAmount.parent();
}
$field.addClass('slbd-make-offer-amount-field');
$field.find('.slbd-make-offer-clean-label').remove();
let keptLabel=false;
$field.find('label').each(function(){
const $label=$(this);
const text=String($label.text()||'').replace(/\s+/g, ' ').trim().toLowerCase();
const isAmountLabel=$label.attr('for')===amountInputId||text.indexOf('your offer')!==-1;
if(!isAmountLabel){
return;
}
if(!keptLabel){
keptLabel=true;
$label
.removeClass('slbd-make-offer-native-amount-label slbd-make-offer-label-duplicate')
.addClass('slbd-make-offer-clean-label')
.removeAttr('aria-hidden')
.text('Your offer');
return;
}
$label.addClass('slbd-make-offer-native-amount-label').attr('aria-hidden', 'true');
});
if(!keptLabel){
const labelHtml='<label class="slbd-make-offer-clean-label" for="' + escapeMakeOfferText(amountInputId) + '">Your offer</label>';
if($moneyField.length){
$moneyField.before(labelHtml);
}else{
$offerAmount.before(labelHtml);
}
$offerAmount.attr('aria-label', 'Your offer');
}}
function normalizeMakeOfferIdentity(context, $form, $name, $email){
const userName=String(context.user_name||$name.val()||'').trim();
const userEmail=String(context.user_email||$email.val()||'').trim();
$form.find('.slbd-make-offer-identity-card, .slbd-make-offer-admin-note').remove();
if(userName&&$name.length&&!$name.val()){
$name.val(userName);
}
if(userEmail&&$email.length&&!$email.val()){
$email.val(userEmail);
}
if(!context.is_logged_in){
return;
}
if(context.can_edit_identity){
getMakeOfferFieldWrapper($name).before('<div class="slbd-make-offer-admin-note">' +
'<strong>Customer details</strong>' +
'<span>As an admin, you can submit this offer on behalf of a customer.</span>' +
'</div>'
);
$form.find('label[for="' + ($name.attr('id')||'') + '"]').text('Customer name');
$form.find('label[for="' + ($email.attr('id')||'') + '"]').text('Customer email');
return;
}
$name.val(userName).attr('type', 'hidden');
$email.val(userEmail).attr('type', 'hidden');
hideMakeOfferField($name, $form);
hideMakeOfferField($email, $form);
const identityHtml='' +
'<div class="slbd-make-offer-identity-card">' +
'<span class="slbd-make-offer-identity-label">Offer submitted as</span>' +
'<strong>' + escapeMakeOfferText(userName) + '</strong>' +
'<span>' + escapeMakeOfferText(userEmail) + '</span>' +
'</div>';
const $actions=$form.find('.slbd-make-offer-actions').first();
if($actions.length){
$actions.before(identityHtml);
}else{
$form.append(identityHtml);
}}
function enhanceMakeOfferFields(context){
const $form=$('#woocommerce-make-offer-form');
const $quantity=$form.find('[name="offer_quantity"]');
let $offerAmount=$form.find('[name="offer_price_each"]').first();
const $offerTotal=$form.find('[name="offer_total"]');
const $name=$form.find('[name="offer_name"]');
const $email=$form.find('[name="offer_email"]');
const maxQuantity=Number(context.max_quantity||0);
$form.addClass('slbd-make-offer-form');
$('body').addClass('slbd-make-offer-enhanced');
$('#lightbox_custom_ofwc_offer_form').addClass('slbd-make-offer-modal');
$('#tab_custom_ofwc_offer_tab_inner').addClass('slbd-make-offer-card');
addMakeOfferHeader();
hideNativeMakeOfferCopy();
$form.find('label[for="woocommerce-make-offer-form-quantity"]').text('Quantity');
$form.find('label[for="woocommerce-make-offer-form-total"]').text('Offer total');
$offerAmount=ensureMakeOfferAmountField($form);
if($quantity.length){
if(!getMakeOfferNumber($quantity)){
$quantity.val('1');
}
$quantity.attr({ min: '1', inputmode: 'numeric', pattern: '[0-9]*' });
if(maxQuantity){
$quantity.attr('max', String(maxQuantity));
}
if(maxQuantity===1){
$quantity.val('1');
hideMakeOfferField($quantity, $form);
}}
if($offerAmount.length){
$offerAmount.attr({ min: '0', step: '0.01', inputmode: 'decimal' });
const $existingMoneyField=$offerAmount.closest('.angelleye-input-group');
if($existingMoneyField.length){
$existingMoneyField.addClass('slbd-make-offer-money-field');
if(!$existingMoneyField.find('.slbd-make-offer-money-field__suffix').length){
$existingMoneyField.append('<span class="slbd-make-offer-money-field__suffix">AUD</span>');
}}else if(!$offerAmount.closest('.slbd-make-offer-money-field').length){
$offerAmount.wrap('<span class="slbd-make-offer-money-field"></span>');
$offerAmount.before('<span class="slbd-make-offer-money-field__prefix">$</span>');
$offerAmount.after('<span class="slbd-make-offer-money-field__suffix">AUD</span>');
}}
if($offerTotal.length){
hideMakeOfferField($offerTotal, $form);
}
$form.find('label[for]').each(function(){
const labelFor=$(this).attr('for');
const $matchingLabels=$form.find('label[for="' + labelFor + '"]');
if($matchingLabels.length > 1){
$matchingLabels.slice(1).addClass('slbd-make-offer-label-duplicate').attr('aria-hidden', 'true');
}});
$form.find(':input').each(function(){
getMakeOfferFieldWrapper($(this)).addClass('slbd-make-offer-field');
});
getMakeOfferFieldWrapper($offerAmount).removeClass('slbd-make-offer-field-hidden');
normalizeMakeOfferAmountLabel($form, $offerAmount);
const $submit=$form.find(':submit').first();
if($submit.length){
$submit.addClass('slbd-make-offer-submit');
if(!$submit.closest('.slbd-make-offer-actions').length){
$submit.wrap('<div class="slbd-make-offer-actions"></div>');
$submit.before('<button type="button" class="slbd-make-offer-cancel">Cancel</button>');
}}
normalizeMakeOfferIdentity(context, $form, $name, $email);
}
function handleSelfOffer(context){
if(!context.is_self_offer){
return;
}
const $buttons=$('.offers-for-woocommerce-make-offer-button-single-product');
$buttons
.attr('aria-disabled', 'true')
.addClass('slbd-make-offer-button-disabled')
.hide();
$('.offers-for-woocommerce-add-to-cart-wrap').each(function(){
const $wrap=$(this);
if(!$wrap.find('.slbd-make-offer-self-message').length){
$wrap.append('<div class="slbd-make-offer-self-message">' + escapeMakeOfferText(context.self_offer_text) + '</div>');
}});
}
function initMakeOfferEnhancements(){
const context=window.slbd_make_offer_context||{};
const $form=$('#woocommerce-make-offer-form');
if(!context.is_product_page||!$form.length){
return;
}
handleSelfOffer(context);
enhanceMakeOfferFields(context);
addMakeOfferProductContext(context);
addMakeOfferHeading();
$(document).off('click.slbdMakeOfferSelf').on('click.slbdMakeOfferSelf', '.slbd-make-offer-button-disabled', function(event){
event.preventDefault();
setMakeOfferMessage('error', context.self_offer_text||'You cannot make an offer on your own listing.');
});
$form.off('submit.slbdMakeOffer').on('submit.slbdMakeOffer', function(event){
const $currentForm=$(this);
if($currentForm.data('slbdMakeOfferSubmitting')||$currentForm.data('slbdMakeOfferSucceeded')){
event.preventDefault();
return false;
}
const error=validateMakeOfferForm($currentForm, context);
if(error){
event.preventDefault();
setMakeOfferMessage('error', error);
return false;
}
const $submit=$currentForm.find(':submit').first();
$currentForm.data('slbdMakeOfferSubmitting', true);
$submit.data('slbdMakeOfferOriginalHtml', $submit.html());
makeOfferLog('Make Offer submit started');
$submit
.prop('disabled', true)
.attr('aria-busy', 'true')
.addClass('slbd-make-offer-submit-is-loading')
.text('Submitting offer...');
});
$form
.off('input.slbdMakeOfferValidation change.slbdMakeOfferValidation')
.on('input.slbdMakeOfferValidation change.slbdMakeOfferValidation', '[name="offer_price_each"], [name="offer_quantity"], [name="offer_name"], [name="offer_email"]', function(){
syncMakeOfferSubmitState($form, context);
});
syncMakeOfferSubmitState($form, context);
$(document).off('click.slbdMakeOfferClose').on('click.slbdMakeOfferClose', '#lightbox_custom_ofwc_offer_form_close_btn, #aeofwc-close-lightbox-link, .slbd-make-offer-close, .slbd-make-offer-cancel, .slbd-make-offer-success-close', function(event){
event.preventDefault();
closeMakeOfferModal($form);
});
$(document).off('ajaxComplete.slbdMakeOffer').on('ajaxComplete.slbdMakeOffer', function(event, jqXHR, settings){
handleMakeOfferAjaxComplete(jqXHR, settings);
});
$(document).off('ajaxError.slbdMakeOffer').on('ajaxError.slbdMakeOffer', function(event, jqXHR, settings){
handleMakeOfferAjaxComplete(jqXHR, settings);
});
$(document).off('click.slbdMakeOfferOverlay').on('click.slbdMakeOfferOverlay', '#lightbox_custom_ofwc_offer_form.slbd-make-offer-modal', function(event){
if(event.target===this){
closeMakeOfferModal($form);
}});
$(document).off('keydown.slbdMakeOfferEsc').on('keydown.slbdMakeOfferEsc', function(event){
if(event.key==='Escape'&&$('#lightbox_custom_ofwc_offer_form.slbd-make-offer-modal.active:visible').length){
closeMakeOfferModal($form);
}});
const observerTarget=document.getElementById('tab_custom_ofwc_offer_tab_inner')||document.getElementById('lightbox_custom_ofwc_offer_form');
if(observerTarget&&!$(observerTarget).data('slbdMakeOfferObserverBound')){
$(observerTarget).data('slbdMakeOfferObserverBound', true);
const observer=new MutationObserver(function(){
const errorVisible=$('#tab_custom_ofwc_offer_tab_alt_message_2:visible, #tab_custom_ofwc_offer_tab_alt_message_custom:visible').length > 0;
const successVisible=$('#tab_custom_ofwc_offer_tab_alt_message_success:visible').length > 0;
if(errorVisible){
resetMakeOfferSubmit($form);
}
if(successVisible){
const link='<a href="' + escapeMakeOfferText(context.offers_url) + '">My Account &rarr; Offers</a>';
setMakeOfferMessage('success', 'Your offer has been submitted. You can track it from ' + link + '.');
}});
observer.observe(observerTarget, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });
}}
initMakeOfferEnhancements();
window.setTimeout(initMakeOfferEnhancements, 500);
window.setTimeout(initMakeOfferEnhancements, 1500);
});