window.waitEvent('scripts_document', function() {

  window.initAviseme = function (){

      $(".js-btn-aviseme").on('click', function(){
          var
              data = {},
              $this = $(this);

          data['product_id']   = $this.data('product-id');
          data['product_name'] = $this.data('product-name');

          $.ajax({
              url: 'index.php?route=product/avise_me&display=true',
              dataType: 'html',
              type: 'get',
              data: data,
              beforeSend: function() {
                  $this.addClass("btn-loading");
              },
              complete: function() {
                  $this.removeClass("btn-loading");
              },
              success: function(html) {
                  $('.js-content-aviseme').append(html);
                  $('.overlay, .js-aviseme-cancel').on('click', function(event) {
                      event.preventDefault();
                      $('.js-content-aviseme > *').remove();
                  });
              }
          });
      });

      $('body').on('click','.js-btn-aviseme-send', function(){
          $('.js-error').remove();
          $('.js-field-entry').removeClass('border-error');

          var data = {};
          data['product_id'] = $('.js-btn-aviseme-send').data('product-id');
          data['name']       = $("input[type=text][name=name]").val();
          data['email']      = $("input[type=text][name=email]").val();
          data['captcha']    = (typeof $("input[type=text][name=captcha]").val() == "undefined") ? false : $("input[type=text][name=captcha]").val();
          data['g-recaptcha-response'] = ($('.g-recaptcha').length > 0) ? grecaptcha.getResponse() : false ;

          $.ajax({
              url: 'index.php?route=product/avise_me/send',
              dataType: 'json',
              type: 'post',
              data: data,
              beforeSend: function() {
                  $('.js-btn-aviseme-send').addClass("btn-loading");
              },
              complete: function() {
                  $('.js-btn-aviseme-send').removeClass("btn-loading");
              },
              success: function(json) {
                  if(json['error']){
                      $.each(json['error'], function(index, value){
                          $("input[name="+index+"]").addClass('border-error').after('<span class="text-error text-small visible-block top-margin-small js-error">'+value+'</span>');
                      });
                      if(json['error']['google_recaptcha']){
                          $('#g-recaptcha').after('<span class="text-error text-small visible-block top-margin-small js-error">' + json['error']['google_recaptcha'] + '</span>');
                      }
                  }
                  if(json['success']){
                      $('.js-aviseme-container').height($('.js-aviseme-container').height());
                      $('.js-aviseme-form').animate({
                          'margin-top': '-100%',
                      }, 300, function(){
                          $('.js-aviseme-form').remove();
                          $('.js-aviseme-container').append('<div class="row middle-xs full-height text-center"><div class="col-xs-12"><div class="content-box"><i class="fas fa-thumbs-up text-success bigger-text-large visible-block"></i><span class="text-x-large text-gray visible-block vertical-padding-large">' + json['success'] + '</span><button class="btn-default btn-large btn-full js-aviseme-continue"><span>Concluir</span></button></div></div></div>');
                          $('.js-aviseme-continue, .js-aviseme-cancel').on('click', function(event) {
                              event.preventDefault();
                              $('.js-content-aviseme > *').remove();
                          });
                      });
                  }
              }
          });
      });

  }


  initAviseme();

});
