{
const {form, profile} = e.detail;
const {fullPayment, partialPayment} = profile.discountPayment;
const message = e.detail.profile.message;
console.log('profile; ', profile)
app.showDiscountPrice(form, profile)
const preorderBtn = form.find('.gPreorderBtn:not(.gPreorderBtnClone)')
preorderBtn.attr('data-default-value', message.preorderText)
preorderBtn.click(function(){
form.find('.gPreorderHidden').trigger('click')
})
if((!partialPayment.enable && fullPayment.enable) || (!fullPayment.enable && partialPayment.enable)){
form.addClass('gHidePaymentOption')
}else{
form.removeClass('gHidePaymentOption')
}
})
document.addEventListener('globo.preorder.form.payment', e => {
const {form} = e.detail;
const preorderBtn = form.find('.gPreorderBtn')
if(form.find('.gPreorderBtnClone').length == 0){
jQuery('').insertAfter('.gPreorderBtn')
form.find('.gPreorderBtn:not(.gPreorderBtnClone)').addClass('gPreorderHidden')
}else{
form.find('.gPreorderBtnClone .loader-button__text').html(form.find('.gPreorderBtn').attr('data-default-value'))
}
if(form.hasClass('gPreorderDiscountForm')){
form.closest('.product__info').find('.price-list').addClass('price-list-discount')
}else{
form.closest('.product__info').find('.price-list').removeClass('price-list-discount')
}
})
document.addEventListener('globo.preorder.form.default', e => {
const form = e.detail.form;
form.find('.gPreorderBtnClone').remove();
form.find('.gPreorderBtn').remove('gPreorderHidden')
})
window.addEventListener('load', function(){
app.appSetInterval(function(){
app.initCollectionPreorderPrice()
}, 1000, 6)
})
},
showDiscountPrice: function(form, profile){
const {fullPayment, partialPayment} = profile.discountPayment;
if((fullPayment.enable && fullPayment.discountValue != '0') || (partialPayment.enable)){
window.Globo.Preorder.appSetInterval(function(){
let priceHTML = form.find(".gPreorderPrice .gPreorderPriceHtml")
if(!priceHTML.length){
priceHTML = form.find(".gPreorderSellingPlanPrice .gPreorderPriceSale")
}
if(priceHTML.length){
let priceDiscount = priceHTML.html().trim();
console.log('priceDiscount: ', priceDiscount)
let priceList = form.closest('.product__info').find('.price-list')
if(priceList.find('.gPreorderDiscount').length){
priceList.find('.gPreorderDiscount').html(priceDiscount)
}else{
let priceDiscountElement = `${priceDiscount}`;
jQuery(priceDiscountElement).insertBefore(priceList.find('.price').first())
}
priceList.addClass('gPreorderDisCountRow')
}
}, 600, 4)
}else{
let priceList = form.closest('.product__info').find('.price-list')
if(priceList.find('.gPreorderDiscount').length){
priceList.find('.gPreorderDiscount').remove()
}
}
},
removeDiscountPrice: function(){
document.addEventListener('globo.preorder.show.addtocart', e => {
const form = e.detail.form;
let priceList = form.closest('.product__info').find('.price-list')
if(priceList.find('.gPreorderDiscount').length){
priceList.find('.gPreorderDiscount').remove()
}
})
},
initCollectionPreorderPrice: function(){
const app = this;
let products = window?.GloboPreorderParams?.products || [];
let productItems = document.querySelectorAll('product-item:not(.loaded)')
if(productItems.length && products.length){
productItems.forEach(productItem => {
let productId = productItem.getAttribute('data-product-id')
if(productId){
let productJson = products.find(product => product.id.toString() == productId.toString())
if(productJson){
let variantId = productJson?.first_available_variant?.id || productJson.variants[0].id;
if(variantId && window.Globo.Preorder){
let profile = window.Globo.Preorder.getProductProfile(productJson, variantId, null)
if(profile?.isPreorder){
app.showPreorderPriceOnCollection(productItem, profile)
productItem.classList.add('loaded')
}
}
}
}
})
}
},
showPreorderPriceOnCollection: function(productItem, profile){
const {fullPayment, partialPayment} = profile.discountPayment;
let moneyFormat = window.Globo.moneyFormat;
if((fullPayment.enable && fullPayment.discountValue != '0') || (partialPayment.enable)){
let discountValue = '0', discountType = '', price = Number(profile.variant.price) / 100;
if(fullPayment.enable){
discountValue = fullPayment.discountValue;
discountType = fullPayment.discountType;
}else{
discountValue = partialPayment.discountValue;
discountType = partialPayment.discountType;
}
console.log('price: ', price)
let discountPrice = discountType == "percentage" ? (price / 100) - (price) / (parseInt(discountValue) * 100) : (price / 100) - (price) - Number(discountValue)
let positionPrice = productItem.querySelector('.price-list')
if(positionPrice && !positionPrice.querySelector('.gPreorderDiscount')){
let priceDiscount = `
${moneyFormat.replace('{' + `{amount_no_decimals}` + '}', discountPrice.toFixed(2).replace('.', ''))}
`;
$(priceDiscount).insertBefore($(positionPrice).find('.price'))
positionPrice.classList.add('gPreorderDisCountRow')
}
}
},
appSetInterval: function(e, t, n) {
e();
var r = 0
, s = window.setInterval((function() {
e(),
++r === n && window.clearInterval(s)
}
), t)
}
}
preorderCustom.render()
