$(document).ready(function() {

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

  $('.js-questioning').on('click', function(event) {
    event.preventDefault();

    $('.warning, .fa-warning').remove();
    $(this).closest('.js-content-question').find('.js-question-delete').addClass('hidden');

    var questioningForm = $('.js-questioning-form');

    $('.js-questioning').removeClass('hidden');
    $(this).addClass('hidden');

    $('[name="question"]').val('');
    $('[name="question"]').removeClass('border-error');

    $(this).closest('.js-questioning-container').append(questioningForm);
    questioningForm.removeClass('hidden');
  });


  $('.js-questioning-form-cancel').on('click', function(event) {
    event.preventDefault();
    $('.js-questioning').removeClass('hidden');
    $(this).closest('.js-content-question').find('.js-question-delete').removeClass('hidden');
    $(this).closest('.js-questioning-form').addClass('hidden');
  });


  $('[name="question"]').on('click', function(event) {
    event.preventDefault();
    $(this).removeClass('border-error');
  });

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

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

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

    var data = {},
        $this;

    data['question_id'] = $(this).data('question-id');
    $this = $(this);

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

        if(json['success']){
          $this.closest('.rel').fadeOut("slow", "linear", function(){
            $this.closest('.rel').remove();
          });
        }

      }
    });

  });


  $('.js-questioning-form').on('submit', function(event) {

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

    event.preventDefault();
    var
      data        = {},
      $this       = $(this),
      question_id = $this.siblings('.js-questioning').data('question-id'),
      question    = $('[name="question"]'),
      questionVal = question.val(),
      url         = $this.attr('action');

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

    data['question_id'] = question_id;
    data['answer']      = questionVal;

    $.ajax({
      url: url,
      type: 'POST',
      dataType: 'json',
      timeout:4000,
      data: data,
      beforeSend: function(){
        question.prop('disabled', true);
        $this.find('.js-btn-send').addClass('btn-loading');
        $this.find('.js-questioning-form-cancel').prop('disabled', true);
      },
      complete: function(){
        question.prop('disabled', false);
        $this.find('.js-btn-send').removeClass('btn-loading');
        $this.find('.js-questioning-form-cancel').prop('disabled', false);
      },
      success: function(json){

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

        if(json['success']){
          $this.addClass('hidden');
          $this.closest('.js-content-question').find('.js-questioning').remove();
          $this.closest('.js-content-question').find('.col-xs').append('<div><span class="text-medium text-gray">'+json['answer']+'</span><span class="text-small text-gray"> ('+json['data']+')</span></div>');
        }

      }
    });

  });

  function displayOrderBy($this){
    if (($this).find('option').filter(':selected').data('type') == 'text') {
      $('.number').addClass('hidden');
      $('.text').removeClass('hidden');
    } else {
      $('.text').addClass('hidden');
      $('.number').removeClass('hidden');
    }
  }

  displayOrderBy($('.js-sort-by-option'));

  $('.js-sort-by-option, .js-order-by-sequence').on('change', function(e) {
    if($(this).hasClass('js-sort-by-option')){
      location = $(this).find('option').filter(':selected').data('href');
    }else{
      location = $(this).filter(':checked').data('href');
    }
  });

  $(".hasDatePicker").myDatePicker({
    maxDate:'today'
  });


  function filter() {
  	url = 'index.php?route=module/mercadolivre/question&token='+token+'&type='+info.titleModules;

  	var filter_mercaid = $('input[name=\'filter_mercaid\']').attr('value');
  	if (filter_mercaid) {
  		url += '&filter_mercaid=' + encodeURIComponent(filter_mercaid);
    }

    var filter_model = $('input[name=\'filter_model\']').attr('value');
  	if (filter_model) {
  		url += '&filter_model=' + encodeURIComponent(filter_model);
      var filter_content_model = $('select[name=\'filter_content_model\']').attr('value');
      if (filter_content_model) {
    		url += '&filter_content_model=' + encodeURIComponent(filter_content_model);
    	}
    }

  	var filter_data_start = $('input[name=\'filter_data_start\']').attr('value');
  	if (filter_data_start) {
  		url += '&filter_data_start=' + encodeURIComponent(filter_data_start);
    }

  	var filter_data_end = $('input[name=\'filter_data_end\']').attr('value');
  	if (filter_data_end) {
  		url += '&filter_data_end=' + encodeURIComponent(filter_data_end);
    }

    var filter_question_status = $('input[name=\'filter_question_status\']:checked').attr('value');
    if (filter_question_status != '*') {
      url += '&filter_question_status=' + encodeURIComponent(filter_question_status);
    }

  	location = url;
  }

  $('.js-filter').on('submit', function(event) {
    event.preventDefault();
    filter();
  });


});
