if (typeof attention !== 'undefined' && attention != '') {
  notificationMsg(attention, 'warning');
}
if (typeof success !== 'undefined' && success != '') {
  notificationMsg(success, 'success');
}
if (typeof error !== 'undefined' && error != '') {
  notificationMsg(error, 'error');
}


if ($('#ftr-postcode-form')) {
  $('#ftr-postcode-form').on('submit', function(e) {

    e.preventDefault();
    var $this = $(this);
    ($data = {}), ($element = $('#postcode'));

    $data['postcode'] = encodeURIComponent($element.val());
    $data['zone_id'] = typeof $element.data('zone-id') != 'undefined' ? $element.data('zone-id') : '';
    $data['freteproduto_id'] = $element.data('product-id');

    $.ajax({
      url: $this.prop('action'),
      type: $this.prop('method'),
      data: $data,
      dataType: 'json',
      beforeSend: function() {
        $('#btn-postcode').prop('disabled', true);
        $('.ftr-loading-bar').removeClass('ftr-none');
      },
      complete: function(json) {
        $('#btn-postcode').prop('disabled', false);
        $('.ftr-loading-bar').addClass('ftr-none');
      },
      success: function(json) {
        clearNotification();
        if (json['error']) {
          if (json['error']['warning']) {
            notificationMsg(json['error']['warning'], 'warning');
          }

          if (json['error']['zone']) {
            if ($('.ftr-error').length == 0) {
              $('select[name="zone_id"]')
                .closest('div[class^=ftr], label[class^=ftr], div[class*=ftr], label[class*=ftr]')
                .append(
                  $('<span>')
                    .addClass('ftr-textfield__error')
                    .text(json['error']['zone'])
                );
            }
          }

          if (json['error']['postcode']) {
            if ($('.ftr-error').length == 0) {
              $('input[name=postcode]')
                .closest('div[class^=ftr], label[class^=ftr], div[class*=ftr], label[class*=ftr]')
                .append(
                  $('<span>')
                    .addClass('ftr-field__error')
                    .text(json['error']['postcode'])
                );
            }
          }
        }

        if (json['shipping_method']) {
          /* INICIO - Mensagens e borda de erro */
          if ($('.zone_id').hasClass('field-entry-error')) {
            $('.zone_id').removeClass('field-entry-error');
          }
          if ($('.js-zone-id-error').length > 0) {
            $('.js-zone-id-error').remove();
          }
          if ($('#postcode').hasClass('field-entry-error')) {
            $('#postcode').removeClass('field-entry-error');
          }
          if ($('.js-postcode-error').length > 0) {
            $('.js-postcode-error').remove();
          }
          /* FINAL - Mensagens e borda de erro */

          var htmlPostcodeResult = '';
          htmlPostcodeResult = '<hr class="ftr-out-space--v-m">';
          htmlPostcodeResult += '<div>';
          htmlPostcodeResult +=
            '<span class="ftr-text--m"><b>' + language.getText('cart', 'text_shipping_method') + '</b></span>';

          htmlPostcodeResult +=
            '<form action="' + language.getText('cart', 'action') + '" method="post" enctype="multipart/form-data">';
          for (i in json['shipping_method']) {
            htmlPostcodeResult +=
              '<span class="ftr-text--grey ftr-block ftr-out-space--t-m"><b>' +
              json['shipping_method'][i]['title'] +
              '</b></span>';

            if (!json['shipping_method'][i]['error']) {
              for (j in json['shipping_method'][i]['quote']) {
                htmlPostcodeResult +='<input class="ftr-radio" id="' + json['shipping_method'][i]['quote'][j]['code'] + '" ';
                htmlPostcodeResult += 'type="radio" name="shipping_method" ';
                htmlPostcodeResult += 'value="' + json['shipping_method'][i]['quote'][j]['code'] + '" ';
                if (json['shipping_method'][i]['quote'][j]['code'] == language.getText('cart', 'shipping_method')) {
                  htmlPostcodeResult += 'checked ';
                }
                htmlPostcodeResult += '>';

                if(i == 'pickup' || i == 'kanguPickup'){
                  htmlPostcodeResult += '<label class="ftr-radio-label ftr-block ftr-out-space--t-n" for="' + json['shipping_method'][i]['quote'][j]['code'] + '">';
                  htmlPostcodeResult += '<span class="ftr-radio-label__text">' + json['shipping_method'][i]['quote'][j]['title'] + ' - ' + json['shipping_method'][i]['quote'][j]['text'] +'</span>';
                  htmlPostcodeResult += '<span class="ftr-text--grey">' + json['shipping_method'][i]['quote'][j]['pickup_address'] + '</span>';
                  htmlPostcodeResult += '</label>';
                }else{
                  htmlPostcodeResult += '<label class="ftr-radio-label ftr-block ftr-out-space--t-n" for="' + json['shipping_method'][i]['quote'][j]['code'] + '">';
                  htmlPostcodeResult += '<span class="ftr-radio-label__text">' + json['shipping_method'][i]['quote'][j]['title'] + ' - ' + json['shipping_method'][i]['quote'][j]['text'] +'</span>';
                  htmlPostcodeResult += '</label>';
                }
              }
            } else {
              htmlPostcodeResult += '<span class="">' + json['shipping_method'][i]['error'] + '</span>';
            }
          }

          htmlPostcodeResult += '<input type="hidden" name="next" value="shipping">';

          htmlPostcodeResult +=
            '<button type="submit" id="button-shipping" class="ftr-btn ftr-background--primary ftr-js-ripple-effect" disabled><span>' +
            language.getText('cart', 'button_shipping') +
            '</span></button>';

          htmlPostcodeResult += '</form>';
          htmlPostcodeResult += '</div>';

          $('#shipping-options').html(htmlPostcodeResult);

          $('input[name=shipping_method]').on('change', function() {
            $('#button-shipping').prop('disabled', false);
          });
        }

        $('#btn-postcode').prop('disabled', false);
      },
    });
  });
}

if ($('.ftr-buy-later')) {
  $('.ftr-buy-later').each(function () {
    var item = $(this);
    item.on('click', function (e) {
      e.preventDefault();
      e.stopPropagation();
      var $this = $(this);
      $.ajax({
        url: $this.href,
        method: 'POST',
        success: function (data) {
          window.location.href = $this.data('remove');
        }
      });
    })
  });
}