var rpress_scripts;
jQuery(document)
  .ready(function ($) {
    // Hide un-necessary elements. These are things that are required in case JS breaks or isn't present
    $('.rpress-no-js')
      .hide();
    //Hide delivery error when switch tabs
    $('body')
      .on('click', '.rpress-delivery-options li.nav-item', function (e) {
        e.preventDefault();
        $(this)
          .parents('.rpress-delivery-wrap')
          .find('.rpress-order-time-error')
          .addClass('hide');
      });
    // Show the login form on the checkout page
    $('#rpress_checkout_form_wrap')
      .on('click', '.rpress_checkout_register_login', function () {
        var $this = $(this);
        $this.addClass('rp-loading')
        $this.find('.rp-ajax-toggle-text')
          .addClass('rp-text-visibility')
        payment_form = $('#rpress_payment_mode_select_wrap,#rpress_purchase_form_wrap');
        ajax_loader = '<span class="rpress-loading-ajax rpress-loading"></span>';
        data = {
          action: $this.data('action'),
          security: rpress_scripts.checkout_nonce
        };
        payment_form.hide();
        $.post(rpress_scripts.ajaxurl, data, function (checkout_response) {
          $('#rpress_checkout_login_register')
            .html(rpress_scripts.loading);
          $('#rpress_checkout_login_register')
            .html(checkout_response);
          // Hide the ajax loader
          $('.rpress-cart-ajax')
            .hide();
          $this.removeClass('rp-loading')
          $this.find('.rp-ajax-toggle-text')
            .removeClass('rp-text-visibility')
          //Show the payment form
          if (data.action == 'rpress_checkout_register')
            payment_form.show();
        });
        return false;
      });
    // Process the login form via ajax
    $(document)
      .on('click', '#rpress_purchase_form #rpress_login_fields input[type=submit]', function (e) {
        e.preventDefault();
        var complete_purchase_val = $(this)
          .val();
        $(this)
          .val(rpress_global_vars.purchase_loading);
        $(this)
          .after('<span class="rp-loading"></span>');
        var data = {
          action: 'rpress_process_checkout_login',
          rpress_ajax: 1,
          security: (typeof rpress_global_vars !== 'undefined' ? rpress_global_vars.checkout_nonce : rpress_scripts.checkout_nonce),
          rpress_user_login: $('#rpress_login_fields #rpress_user_login')
            .val(),
          rpress_user_pass: $('#rpress_login_fields #rpress_user_pass')
            .val()
        };
        $.post(rpress_global_vars.ajaxurl, data, function (data) {
          if ($.trim(data) == 'success') {
            $('.rpress_errors')
              .remove();
            window.location = rpress_scripts.checkout_page;
          } else {
            $('#rpress_login_fields input[type=submit]')
              .val(complete_purchase_val);
            $('.rp-loading')
              .remove();
            $('.rpress_errors')
              .remove();
            $('#rpress-user-login-submit')
              .before(data);
          }
        });
      });
    // Load the fields for the $this payment method
    $('select#rpress-gateway, input.rpress-gateway')
      .change(function (e) {
        var payment_mode = $('#rpress-gateway option:selected, input.rpress-gateway:checked')
          .val();
        if (payment_mode == '0') {
          return false;
        }
        $('#rpress_purchase_form input[name="rpress-gateway"]')
          .val(payment_mode);
        rpress_load_gateway(payment_mode);
        return false;
      });
    // Auto load first payment gateway
    if (rpress_scripts.is_checkout == '1') {
      var chosen_gateway = false;
      var ajax_needed = false;
      if ($('select#rpress-gateway, input.rpress-gateway')
        .length) {
        chosen_gateway = $("meta[name='rpress-chosen-gateway']")
          .attr('content');
        ajax_needed = true;
      }
      if (!chosen_gateway) {
        chosen_gateway = rpress_scripts.default_gateway;
      }
      if (chosen_gateway) {
        $('#rpress_purchase_form input[name="rpress-gateway"]')
          .val(chosen_gateway);
      }
      if (ajax_needed) {
        // If we need to ajax in a gateway form, send the requests for the POST.
        setTimeout(function () {
          rpress_load_gateway(chosen_gateway);
        }, 200);
      } else {
        // The form is already on page, just trigger that the gateway is loaded so further action can be taken.
        $('body')
          .trigger('rpress_gateway_loaded', [chosen_gateway]);
      }
    }
    // Process checkout
    $(document)
      .on('click', '#rpress_purchase_form #rpress_purchase_submit [type=submit]', function (e) {
        var rpressPurchaseform = document.getElementById('rpress_purchase_form');
        if (typeof rpressPurchaseform.checkValidity === "function" && false === rpressPurchaseform.checkValidity()) {
          return;
        }
        e.preventDefault();
        var selectedServiceType = '';
        var selectedServiceTime = '';
        var selectedServiceDate = '';
        var $activeServiceTab = $('.rp-checkout-service-option #rpressdeliveryTab .single-service-selected.active, #rpress_checkout_order_details #rpressdeliveryTab .single-service-selected.active')
          .first();
        if ($activeServiceTab.length) {
          selectedServiceType = String($activeServiceTab.data('service-type') || '')
            .toLowerCase();
        }
        if (!selectedServiceType) {
          selectedServiceType = String(rp_getCookie('service_type') || '')
            .toLowerCase();
        }
        var $checkoutScope = $('.rp-checkout-service-option')
          .first();
        if (!$checkoutScope.length) {
          $checkoutScope = $('#rpress_checkout_order_details')
            .first();
        }
        var $activePane = $();
        if (selectedServiceType) {
          $activePane = $checkoutScope.find('.delivery-settings-wrapper#nav-' + selectedServiceType)
            .first();
        }
        if (!$activePane.length) {
          $activePane = $checkoutScope.find('.delivery-settings-wrapper.active')
            .first();
        }
        if ($activePane.length) {
          selectedServiceTime = String($activePane.find('.rpress-hrs')
            .first()
            .val() || '');
          selectedServiceDate = String($activePane.find('.rpress_get_delivery_dates')
            .first()
            .val() || '');
        }
        if (selectedServiceType) {
          rp_setCookie('service_type', selectedServiceType, rp_scripts.expire_cookie_time);
        }
        if (selectedServiceTime) {
          rp_setCookie('service_time', selectedServiceTime, rp_scripts.expire_cookie_time);
        }
        if (selectedServiceDate) {
          rp_setCookie('service_date', selectedServiceDate, rp_scripts.expire_cookie_time);
          rp_setCookie('delivery_date', selectedServiceDate, rp_scripts.expire_cookie_time);
        }
        var selectedGateway = $('#rpress-gateway option:selected, input.rpress-gateway:checked')
          .val();
        if (selectedGateway && selectedGateway !== '0') {
          $('#rpress_purchase_form input[name="rpress-gateway"]')
            .val(selectedGateway);
        }
        var complete_purchase_val = $(this)
          .val();
        $(this)
          .val(rpress_global_vars.purchase_loading);
        $(this)
          .prop('disabled', true);
        $(this)
          .after('<span class="rp-loading"></span>');
        $(this).parents('body').append('<div class="blur-content"><p class="blur-txt">'+rpress_scripts.blurtxt+'</p><span class="blur-loader"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="margin: auto; background: rgb(255, 255, 255); display: block; shape-rendering: auto;" width="200px" height="200px" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid"><circle cx="50" cy="50" r="32" stroke-width="8" stroke="#fe718d" stroke-dasharray="50.26548245743669 50.26548245743669" fill="none" stroke-linecap="round"><animateTransform attributeName="transform" type="rotate" repeatCount="indefinite" dur="1s" keyTimes="0;1" values="0 50 50;360 50 50"></animateTransform></circle></svg></span></div>');
        var requestData = $('#rpress_purchase_form')
          .serialize() + '&action=rpress_process_checkout&rpress_ajax=true';
        if (selectedGateway && selectedGateway !== '0') {
          requestData += '&rpress-gateway=' + encodeURIComponent(selectedGateway);
        }
        if (selectedServiceType) {
          requestData += '&rpress_service_type=' + encodeURIComponent(selectedServiceType);
        }
        if (selectedServiceTime) {
          requestData += '&rpress_service_time=' + encodeURIComponent(selectedServiceTime);
        }
        if (selectedServiceDate) {
          requestData += '&rpress_service_date=' + encodeURIComponent(selectedServiceDate);
        }
        $.post(rpress_global_vars.ajaxurl, requestData,
          function (data) {
            if ($.trim(data) == 'success') {
              $('.rpress_errors')
                .remove();
              $('.rpress-error')
                .hide();
                $('#rpress_final_total_wrap .rp-loading').hide();
              $('.blur-content').hide();
              $(rpressPurchaseform)
                .submit();
            } else {
              $('#rpress-purchase-button')
                .val(complete_purchase_val);
              $('.rp-loading')
                .remove();
              $('.rpress_errors')
                .remove();
              $('.blur-content').hide();
              if ($("#rpress_final_total_wrap .rp-loading").length) {
                  $("#rpress_final_total_wrap .rp-loading").hide();
                }
              $('.rpress-error')
                .hide();
              $(rpress_global_vars.checkout_error_anchor)
                .before(data);
              $('#rpress-purchase-button')
                .prop('disabled', false);
              $(document.body)
                .trigger('rpress_checkout_error', [data]);
            }
          });
      });
    // Update state field
    $(document.body)
      .on('change', '#rpress_cc_address input.card_state, #rpress_cc_address select, #rpress_address_country', update_state_field);
    function update_state_field() {
      var $this = $(this);
      var $form;
      var is_checkout = typeof rpress_global_vars !== 'undefined';
      var field_name = 'card_state';
      if ($(this)
        .attr('id') == 'rpress_address_country') {
        field_name = 'rpress_address_state';
      }
      if ('card_state' != $this.attr('id')) {
        // If the country field has changed, we need to update the state/province field
        var postData = {
          action: 'rpress_get_states',
          country: $this.val(),
          field_name: field_name,
          security: (typeof rpress_global_vars !== 'undefined' ? rpress_global_vars.checkout_nonce : rpress_scripts.checkout_nonce),
        };
        $.ajax({
          type: "POST",
          data: postData,
          url: rpress_scripts.ajaxurl,
          xhrFields: {
            withCredentials: true
          },
        })
          .fail(function (data) {
            if (window.console && window.console.log) {
              console.log(data);
            }
          })
          .done(function (data) {
            if (is_checkout) {
              var ns = "nostates";
              if (data != ns) {
                $("#card_state").parent().show();
                $("#card_state").replaceWith (data);
              }
              else {
                if (data === ns) {
                  $("#card_state").parent().hide();
                }
              }
              recalculate_taxes();
            }
          });
      } else {
        if (is_checkout) {
          recalculate_taxes();
        }
      }
      return false;
    }
    // If is_checkout, recalculate sales tax on postalCode change.
    $(document.body)
      .on('change', '#rpress_cc_address input[name=card_zip]', function () {
        if (typeof rpress_global_vars !== 'undefined') {
          recalculate_taxes();
        }
      });
    $("#rpressModal")
      .on('hide.bs.modal', function () {
        $('.modal-backdrop.in')
          .remove();
      });
  });
// Load a payment gateway
function rpress_load_gateway(payment_mode) {
  // Show the ajax loader
  jQuery('.rpress-cart-ajax')
    .show();
  jQuery('#rpress_purchase_form_wrap')
    .html('<span class="rpress-loading-ajax rpress-loading"></span>');
  var url = rpress_scripts.ajaxurl;
  if (url.indexOf('?') > 0) {
    url = url + '&';
  } else {
    url = url + '?';
  }
  url = url + 'payment-mode=' + payment_mode;
  jQuery('#rpress_purchase_form input[name="rpress-gateway"]')
    .val(payment_mode);
  rp_setCookie('payment_mode', payment_mode, rp_scripts.expire_cookie_time);
  jQuery.post(url, {
    action: 'rpress_load_gateway',
    rpress_payment_mode: payment_mode,
    security: rpress_scripts.load_gateway_nonce
  },
    function (response) {
      jQuery('#rpress_purchase_form_wrap')
        .html(response);
      jQuery('.rpress-no-js')
        .hide();
      jQuery('body')
        .trigger('rpress_gateway_loaded', [payment_mode]);
    });
}
