$(document).ready(function() {

  var
  token = $('body').data('token'),
  info = $('[data-info]').data('info');


   $('#question-info').on('click', '.js-control-saved-answers',  function(event) {
    event.preventDefault();
    const CHEVRON = $('.fa-chevron-down');
    if ($('.js-saved-answers').hasClass('hidden')) {
      $('.js-saved-answers').removeClass('hidden');
    } else {
      $('.js-saved-answers').addClass('hidden');
    }
  });

  $('#question-info').on('click', '.js-answer-saved-content', function(event) {
    event.preventDefault();
    const ANSWER = $(this).find('.text-medium').text();
    const TEXTAREA = $('textarea[name="question"]').val();
    $('textarea[name="question"]').val(TEXTAREA + ANSWER);
  });

  $('#question-info').on('click', '.js-answer-saved-remove', function(event) {
    event.preventDefault();
    $(this).closest('.js-answer-saved').remove();
  });


  $('#question-info').on('click', '.js-save-answers', function(event){

    $('.warning, .fa-warning').remove();

    let
      $this    = $(this),
      data     = {},
      question = $('[name="question"]');

    if (question.val().trim() == '') {
      question.addClass('border-error');
      return false;
    }

    data['answer'] = question.val();

    $.ajax({
      url: 'index.php?route=module/mercadolivre/answers/insert&token=' + token,
      type: 'POST',
      dataType: 'json',
      data: data,
      beforeSend: function(){
        $this
          .closest('.js-content-question')
          .append(
            '<div class="overlay-abs overlay-white-6 "><img class="center-middle-absolute" src="../assets/img/loading-black.svg"></div>'
          );
      },
      complete: function(){
        $this.closest('.js-content-question').find('.overlay-abs').remove();
      },
      success: function(json){

        if(json['error']){
          $('.js-header-notifications').append('<div class="warning background-error top-margin-medium full-width padding-small text-error">'+json['error']+'</div>');
          $this.closest('.js-content-question').prepend('<i class="fas fa-exclamation-triangle text-error right-padding-medium"></i>');
        }

        if(json['success']){
          getListAnswers($this);
        }

      }
    });

  });


  $('#question-info').on('click', '.js-delete-answers', function(event){

    if(!confirm('Você realmente deseja excluir esta resposta?')){
      return false;
    }

    $('.warning, .fa-warning').remove();

    var data = {},
        $this = $(this);

    data['answers_id'] = $this.data('answers-id');

    $.ajax({
      url: 'index.php?route=module/mercadolivre/answers/delete&token=' + token,
      type: 'POST',
      dataType: 'json',
      data: data,
      beforeSend: function(){
        $this
          .closest('.js-answer-saved-content')
          .append(
            '<div class="overlay-abs overlay-white-6 "><img class="center-middle-absolute" src="../assets/img/loading-black.svg"></div>'
          );
      },
      complete: function(){
        $this.closest('.js-answer-saved-content').find('.overlay-abs').remove();
      },
      success: function(json){

        if(json['error']){
          $('.js-header-notifications').append('<div class="warning background-error top-margin-medium full-width padding-small text-error">'+json['error']+'</div>');
          $this.closest('.js-answer-saved-content').prepend('<i class="fas fa-exclamation-triangle text-error right-padding-medium"></i>');
        }

        if(json['success']){
            $this.closest('.js-answer-saved').remove();
        }

      }
    });

  });


  function getListAnswers($this){

    $.ajax({
      url: 'index.php?route=module/mercadolivre/answers&token=' + token,
      type: 'POST',
      dataType: 'html',
      data: '',
      beforeSend: function(){
        $this
          .closest('.js-answer-saved-content')
          .append(
            '<div class="overlay-abs overlay-white-6 "><img class="center-middle-absolute" src="../assets/img/loading-black.svg"></div>'
          );
      },
      complete: function(){
        $this.closest('.js-answer-saved-content').find('.overlay-abs').remove();
      },
      success: function(html){
        $('.js-content-answers').empty().append(html);
        $('.js-saved-answers').removeClass('hidden');
      }
    });

  }



});
