"use strict";
jQuery(document).ready(function($){
$(".single_variation_wrap").on("show_variation", function(b, c){
$('.woo-loder-wrap').show();
jQuery(".woo-vou-fields-wrapper-variation").hide();
jQuery(".woo-vou-preview-pdf-wrap").hide();
jQuery("#woo-vou-fields-wrapper-"+c.variation_id).show();
jQuery("#woo-vou-preview-pdf-wrap-"+c.variation_id).show();
var data={
'action':'woo_vou_variation_start_enddate',
'variation_id':c.variation_id
};
jQuery.post(WooVouPublic.ajaxurl, data, function(response){
if(response!=''){
if(response.vou_max_date!=''){
$("input#recipient_giftdate-"+c.variation_id).datepicker("destroy");
$("input#recipient_giftdate-"+c.variation_id).datepicker({
dateFormat: format,
minDate: response.vou_min_date,
maxDate: response.vou_max_date
});
}else if(response.vou_min_date!=''){
$("input#recipient_giftdate-"+c.variation_id).datepicker("destroy");
$("input#recipient_giftdate-"+c.variation_id).datepicker({
dateFormat: format,
minDate: response.vou_min_date
});
}
$('.woo-vou-recipient-fields tr').show();
if(typeof response.woo_vou_hidden_recipient_fields!=='undefined'&&response.woo_vou_hidden_recipient_fields.length > 0){
$.each(response.woo_vou_hidden_recipient_fields, function (index, value){
$('#'+value).parents('tr').hide();
$('.'+value).hide();
});
}}
$('.woo-loder-wrap').hide();
});
});
$(document).on('change', '.wc-block-add-to-cart-with-options-variation-selector-attribute-options__pill-input', function(){
let variationId=$(this).closest('form').find('input[name="variation_id"]').val();
if(variationId){
$(".single_variation_wrap").trigger("show_variation", [{ variation_id: variationId }]);
}});
$(".single_variation_wrap").on("hide_variation", function(event){
jQuery(".woo-vou-fields-wrapper-variation").hide();
jQuery(".woo-vou-preview-pdf-wrap").hide();
});
$(document).on('click', '.woo-vou-view-preview-template-img', function(){
var pswpElement=document.querySelectorAll('.pswp')[0];
var url=$(this).children('img').data('src');
var items=[
{
src: url,
w: 600,
h: 400
}
];
var options={
index: $(this).data('index'),
mainClass: 'woo-vou-pdf-preview-popup'
};
var gallery=new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
gallery.init();
});
$(document).on('click', '.woo-vou-preview-template-img', function(){
$(this).parents('td').find('.woo-vou-preview-template-img-id').val('');
if(!$(this).hasClass('woo-vou-preview-template-img-border')){
$(this).parents('td').find('*').removeClass('woo-vou-preview-template-img-border');
$(this).addClass('woo-vou-preview-template-img-border');
$(this).parents('td').find('.woo-vou-preview-template-img-id').val($(this).attr('data-id'));
}else{
$(this).removeClass('woo-vou-preview-template-img-border');
}});
$('.woo-vou-preview-template-img-id').each(function(){
var selected_template=$(this).val();
$(this).parents('td').find('img[data-id="'+selected_template+'"]').addClass('woo-vou-preview-template-img-border');
});
jQuery('.woo-vou-meta-datetime').each(function(){
var jQuerythis=jQuery(this),
format=jQuerythis.attr('rel'),
id=jQuerythis.attr('id');
if(id=='_woo_vou_start_date'){
var expire_date=jQuery('#_woo_vou_exp_date');
jQuerythis.datetimepicker({
ampm: true,
dateFormat:format,
showTime: false,
timeText: WooVouPublic.time_text, hourText: WooVouPublic.hour_text, minuteText: WooVouPublic.minute_text, currentText: WooVouPublic.current_text, closeText: WooVouPublic.close_text,
onSelect: function (selectedDateTime){
expire_date.datetimepicker('option', 'minDate', jQuerythis.datetimepicker('getDate'));
}});
}else if(id=='_woo_vou_exp_date'){
var start_date=jQuery('#_woo_vou_start_date');
jQuerythis.datetimepicker({
ampm: true,
dateFormat:format,
showTime: false,
timeText: WooVouPublic.time_text, hourText: WooVouPublic.hour_text, minuteText: WooVouPublic.minute_text, currentText: WooVouPublic.current_text, closeText: WooVouPublic.close_text,
onSelect: function (selectedDateTime){
start_date.datetimepicker('option', 'maxDate', jQuerythis.datetimepicker('getDate'));
}});
}else{
jQuerythis.datetimepicker({ampm: true,dateFormat:format, timeText: WooVouPublic.time_text, hourText: WooVouPublic.hour_text, minuteText: WooVouPublic.minute_text, currentText: WooVouPublic.current_text, closeText: WooVouPublic.close_text });//,timeFormat:'hh:mm:ss',showSecond:true
}});
if($('.woo_vou_multi_select').length){
$('.woo_vou_multi_select').select2();
}
$(document).on("click", ".toggle-row", function(){
if(jQuery(this).closest('tr').hasClass('is-expanded') ){
jQuery(this).closest('tr').removeClass('is-expanded');
jQuery(this).closest('tr').find('td').each(function(){
if(! jQuery(this).hasClass('column-primary') ){
jQuery(this).hide();
}});
}else{
jQuery(this).closest('tr').addClass('is-expanded');
jQuery(this).closest('tr').find('td').each(function(){
jQuery(this).show();
});
}});
if($("input[name^='_woo_vou_recipient_giftdate']").length){
var format=jQuery("input[name^='_woo_vou_recipient_giftdate']").attr('rel');
var max_date='';
if(WooVouPublic.vou_max_date!=''){
$("input[name^='_woo_vou_recipient_giftdate']").datepicker({
dateFormat: format,
minDate: WooVouPublic.vou_min_date,
maxDate: WooVouPublic.vou_max_date
});
}else{
$("input[name^='_woo_vou_recipient_giftdate']").datepicker({
dateFormat: format,
minDate: WooVouPublic.vou_min_date
});
}}
woo_vou_toggle_recipient_delivery_method();
$(document).on('click', '.woo-vou-delivery-method', function(){
woo_vou_toggle_recipient_delivery_method();
});
$(".woo_vou_cust_date_field").each(function(){
format=jQuery(this).attr('rel');
$(this).datepicker({
dateFormat: format,
});
});
var preview_file='';
if(!$('ul.woocommerce-error').length){
$('.single-product div.type-product').before('<ul class="woocommerce-error" role="alert"></ul>');
$('ul.woocommerce-error').hide();
}
$(document).on('click', '.woo_vou_preview_pdf',function(e){
var clicked_el=$(this);
e.preventDefault();
$('ul.woocommerce-error').html('');
$('div.woocommerce-message').hide();
if(WooVouPublic.is_preview_pdf_options!='newtab'){
$('img.woo-vou-preview-loader').show();
$('.woo-vou-preview-pdf-overlay').show();
}
var error=false;
var vou_template_id='';
$(document).find('.woo-vou-preview-template-img-id').each(function(){
if($(this).prev().is(':visible')&&$('.woo-vou-preview-template-img-id').length){
vou_template_id=$(this).val();
if(!$.trim(vou_template_id)){
$('ul.woocommerce-error').html($('ul.woocommerce-error').html() + WooVouPublic.vou_template_err);
error=true;
}}
});
$(document).find('input[class^="woo-vou-recipient-"], textarea[class^="woo-vou-recipient-"]').each(function(){
if($(this).is(':visible')&&!$(this).attr('disabled')&&$(this).data('required')&&!$.trim($(this).val())){
const raw_recipient_key=$(this).attr('name');
const recipient_key=raw_recipient_key.substring(raw_recipient_key.lastIndexOf("_woo_vou_")+9, raw_recipient_key.lastIndexOf("[")) + "_err";
$('ul.woocommerce-error').html($('ul.woocommerce-error').html() + WooVouPublic[recipient_key]);
error=true;
}});
if($('#vou_set_pay_your_price').val()==''){
$('ul.woocommerce-error').html($('ul.woocommerce-error').html() + WooVouPublic.pay_your_price_err);
error=true;
}
if(error){
$('img.woo-vou-preview-loader').hide();
$('.woo-vou-preview-pdf-overlay').hide();
$('ul.woocommerce-error').show();
if($("ul.woocommerce-error").length > 0){
$('html, body').animate({ scrollTop: ($("ul.woocommerce-error").offset().top - $("ul.woocommerce-error").height()) }, 800);
}}else{
$('.woocommerce-error').hide();
var data={
'action': 'woo_vou_add_to_cart_validation',
'woo_vou_is_ajax': true
};
if(WooVouPublic.is_variable){
data['product_id']=clicked_el.parent().find('input[name="woo_vou_product_id"]').val();
data['variation_id']=clicked_el.parent().find('input[name="woo_vou_variation_id"]').val();
}else{
data['product_id']=clicked_el.parent().find('input[name="woo_vou_product_id"]').val();
}
$(document).find('input[name^="_woo_vou_"], textarea[name^="_woo_vou_"]').each(function(){
if($(this).is(':visible')&&!$(this).attr('disabled')){
var woo_vou_field_name=$(this).attr('name');
if(woo_vou_field_name.indexOf("_woo_vou_delivery_method") >=0){
data[$(this).attr('name')]=$('input[name="'+woo_vou_field_name+'"]:checked').val();
}else{
data[$(this).attr('name')]=$(this).val();
}}
});
$(document).find('.woo-vou-preview-template-img-wrap').each(function(){
if($(this).is(':visible')){
var input=$(this).next();
data[input.attr('name')]=input.val();
}});
if(WooVouPublic.payyourprice&&$('input[name="'+WooVouPublic.payyourprice+'"]').length){
data['payyourprice']=$('input[name="'+WooVouPublic.payyourprice+'"]').val();
}
if(WooVouPublic.is_preview_pdf_options=='newtab'){
var custom_preview_form='<form method="post" action="" target="_blank" id="woo-you-preview-form">';
$.each(data, function(key, value){
if(key!='action'&&key!='woo_vou_is_ajax'){
custom_preview_form=custom_preview_form + '<input type="hidden" name="'+key+'" value="'+value+'">';
}
custom_preview_form=custom_preview_form + '<input type="hidden" name="is_preview" value="true">';
custom_preview_form=custom_preview_form + '<input type="hidden" name="quantity" value="'+$(document).find('div.quantity input[id^="quantity_"]').val()+'">';
custom_preview_form=custom_preview_form + '<input type="hidden" name="pay_your_price" value="'+$('input[id="vou_set_pay_your_price"]').val()+'">';
});
$('body').find('#woo-you-preview-form').remove();
$('body').append(custom_preview_form);
$('#woo-you-preview-form').submit();
return true;
}else{
jQuery.post(WooVouPublic.ajaxurl, data, function(response){
var response_data=jQuery.parseJSON(response);
if(response_data.valid){
data.action='woo_vou_generate_preview_pdf';
data.is_preview='true';
data.pay_your_price=$('#vou_set_pay_your_price').val();
data.quantity=$(document).find('div.quantity input[id^="quantity_"]').val();
jQuery.post(WooVouPublic.ajaxurl, data, function(response){
var response_data=jQuery.parseJSON(response);
$('img.woo-vou-preview-loader').hide();
if(response_data.pdf_name){
preview_file=response_data.pdf_name;
$('div.woo-vou-preview-pdf-content div.woo-vou-popup').html(response_data.pdf_preview);
$('div.woo-vou-preview-pdf-content, .woo-vou-preview-pdf-overlay').show();
}else{
$('div.woo-vou-preview-pdf-content div.woo-vou-popup').html('Some issue occured.');
}});
}else{
$('img.woo-vou-preview-loader').hide();
$('.woo-vou-preview-pdf-overlay').hide();
$('ul.woocommerce-error').html(response_data.html);
$('ul.woocommerce-error').show();
$('html, body').animate({ scrollTop: ($("div.woocommerce").offset().top - $("div.woocommerce").height()) }, 800);
}});
}}
});
$(document).on('click', '.woo-vou-close-button, .woo-vou-preview-pdf-overlay', function(e){
e.preventDefault();
var data={
action:'woo_vou_unlink_preview_pdf',
preview_file_name:preview_file,
security:WooVouPublic.security,
};
jQuery.post(WooVouPublic.ajaxurl, data, function(response){
$('div.woo-vou-popup-content, div.woo-vou-popup-overlay').hide();
});
});
$(document).on('blur change', '#vou_set_pay_your_price', function(e){
var vou_set_pay_your_price=jQuery(this).val().replace(/[^\d.]/g, '');
jQuery(this).val(vou_set_pay_your_price);
});
$(document).on('click', '.vou_pay_your_amount_button', function(e){
$('#vou_set_pay_your_price').val($(this).data('vou_pay_your_amount'));
});
});
function woo_vou_toggle_recipient_delivery_method(){
jQuery('table.woo-vou-recipient-delivery-method tr.woo-vou-delivery-method-wrapper input.woo-vou-delivery-method').each(function(){
if(jQuery(this).is(':checked')){
jQuery(this).closest('tr.woo-vou-delivery-method-wrapper').nextAll().each(function(){
jQuery(this).show();
jQuery(this).find('td.value input, td.value textarea').removeAttr('disabled');
});
}else{
jQuery(this).closest('tr.woo-vou-delivery-method-wrapper').nextAll().each(function(){
jQuery(this).hide();
jQuery(this).find('td.value input, td.value textarea').attr('disabled', 'disabled');
});
}});
}
function woo_vou_used_codes_ajax_pagination(pid){
var woo_vou_start_date=jQuery('#woo_vou_hid_start_date').val();
var woo_vou_end_date=jQuery('#woo_vou_hid_end_date').val();
var woo_vou_post_id=jQuery('#woo_vou_product_filter').val();
var woo_vou_partial_used_voucode=jQuery('input[name="woo_vou_partial_used_voucode"]:checked').length;
var data={
action:'woo_vou_used_codes_next_page',
paging:pid,
woo_vou_start_date:woo_vou_start_date,
woo_vou_end_date:woo_vou_end_date,
woo_vou_post_id:woo_vou_post_id,
current_page_url:window.location.href,
woo_vou_partial_used_voucode: woo_vou_partial_used_voucode
};
jQuery('.woo-vou-usedcodes-loader').show();
jQuery('.woo-vou-used-codes-paging').hide();
jQuery.post(WooVouPublic.ajaxurl, data, function(response){
var newresponse=jQuery(response).filter('.woo-vou-used-codes-html').html();
jQuery('.woo-vou-usedcodes-loader').hide();
jQuery('.woo-vou-used-codes-html').html(newresponse);
});
return false;
}
function woo_vou_purchased_codes_ajax_pagination(pid){
var woo_vou_post_id=jQuery('#woo_vou_product_filter').val();
var woo_vou_start_date=jQuery('#woo_vou_hid_start_date').val();
var woo_vou_end_date=jQuery('#woo_vou_hid_end_date').val();
var woo_vou_partial_used_voucode=jQuery('input[name="woo_vou_partial_used_voucode"]:checked').length;
var data={
action: 'woo_vou_purchased_codes_next_page',
paging: pid,
woo_vou_start_date:woo_vou_start_date,
woo_vou_end_date:woo_vou_end_date,
woo_vou_post_id:woo_vou_post_id,
current_page_url:window.location.href,
woo_vou_partial_used_voucode: woo_vou_partial_used_voucode
};
jQuery('.woo-vou-purchasedcodes-loader').show();
jQuery('.woo-vou-purchased-codes-paging').hide();
jQuery.post(WooVouPublic.ajaxurl, data, function(response){
var newresponse=jQuery(response).filter('.woo-vou-purchased-codes-html').html();
jQuery('.woo-vou-purchased-codes-loader').hide();
jQuery('.woo-vou-purchased-codes-html').html(newresponse);
});
return false;
}
function woo_vou_unused_codes_ajax_pagination(pid){
var woo_vou_post_id=jQuery('#woo_vou_product_filter').val();
var woo_vou_partial_used_voucode=jQuery('input[name="woo_vou_partial_used_voucode"]:checked').length;
var data={
action:'woo_vou_unused_codes_next_page',
paging:pid,
woo_vou_post_id:woo_vou_post_id,
current_page_url: window.location.href,
woo_vou_partial_used_voucode: woo_vou_partial_used_voucode
};
jQuery('.woo-vou-unusedcodes-loader').show();
jQuery('.woo-vou-unused-codes-paging').hide();
jQuery.post(WooVouPublic.ajaxurl, data, function(response){
var newresponse=jQuery(response).filter('.woo-vou-unused-codes-html').html();
jQuery('.woo-vou-unusedcodes-loader').hide();
jQuery('.woo-vou-unused-codes-html').html(newresponse);
});
return false;
}
function woo_vou_unlimited_codes_ajax_pagination(pid){
var woo_vou_start_date=jQuery('#woo_vou_hid_start_date').val();
var woo_vou_end_date=jQuery('#woo_vou_hid_end_date').val();
var woo_vou_post_id=jQuery('#woo_vou_product_filter').val();
var data={
action:'woo_vou_unlimited_codes_next_page',
paging:pid,
woo_vou_start_date:woo_vou_start_date,
woo_vou_end_date:woo_vou_end_date,
woo_vou_post_id:woo_vou_post_id,
current_page_url:window.location.href,
};
jQuery('.woo-vou-unlimitedcodes-loader').show();
jQuery('.woo-vou-unlimited-codes-paging').hide();
jQuery.post(WooVouPublic.ajaxurl, data, function(response){
var newresponse=jQuery(response).filter('.woo-vou-unlimited-codes-html').html();
jQuery('.woo-vou-unlimitedcodes-loader').hide();
jQuery('.woo-vou-unlimited-codes-html').html(newresponse);
});
return false;
};