$(document).ready(function() {
  var
  token = $('body').data('token');

  $('.js-clean-notification').on('click', function(){

			var notifications,
					data = {};

			if($(this).hasClass('js-close')){
				notifications = $(this).closest('.js-evaluation-dados');
			}else{
				notifications = $('.js-evaluation-dados');
			}

			data['dados'] = notifications.toArray().map(function(notification){
				return {'id' : $(notification).data('id'), 'type' : $(notification).data('type')} ;
		 	});

			$.ajax({
				url: 'index.php?route=common/notification/clear&token=' + token,
				type: 'POST',
				dataType: 'json',
				data: data,
				beforeSend: function(){},
				complete: function(){},
				success: function(json) {
					$.each(notifications, function(index, element){
							setTimeout(function(){
								$(element).hide("slide", { direction: "right" }, 400, function(){
									$(element).remove();
								});
							}, index*400);
					});
				}
			});

  });

	/* Inicio - Avaliações */
	// Limitar caracteres da avaliação
	$('.js-evaluation-text').each(function() {
		var
		text  = $(this).data('text'),
		limit = $(this).hasClass('js-evaluation-product') ? 30 : 90;

		if (text.length > limit) {
			$(this).text(text.substr(0, limit) + '...');
			if ($(this).hasClass('js-evaluation-message')) {
				$(this).append('<span class="text-small text-primary text-decoration-none left-margin-small cursor-pointer js-evaluation-see js-more">Ver mais</span>');
			}
		} else {
			$(this).text(text);
		}
	});
	// Mostrar ou ocultar as avaliações
	$('.js-evaluation-dados').on('click', '.js-evaluation-see', function(event) {
		event.preventDefault();
		event.stopPropagation();
		var text = $(this).closest('.js-evaluation-message').data('text');
		if ($(this).hasClass('js-more')) {
			$(this).closest('.js-evaluation-message').text(text).append('<span class="text-small text-primary text-decoration-none cursor-pointer left-margin-small js-evaluation-see js-less">Ver menos</span>');
		} else {
			$(this).closest('.js-evaluation-message').text(text.substr(0, 90) + '...').append('<span class="text-small text-primary text-decoration-none cursor-pointer left-margin-small js-evaluation-see js-more">Ver mais</span>');
		}
	});
	// Aprovar ou reprovar as avaliações
	$('.js-evaluation-action').on('click', function(event) {

		event.preventDefault();
		event.stopPropagation();

		var
		evaluation = $(this).closest('.js-evaluation-dados'),
		id = evaluation.data('id'),
		url = 'index.php?route=catalog/';

		if(evaluation.hasClass('js-read-message')){
      url = 'index.php?route=common/notification/updateNotificationRead&token='; // Marcar notofocação como Lida.
    }else{
      url += evaluation.hasClass('js-store-rating') ? 'store_rating/StoreRatingHome&token=' : 'review/reviewhome&token='; // Avaliação da loja ou do Produto
    }

		url += token + '&selected=' + id + '&action=update&status=';
		url += $(this).hasClass('js-approve') ? '1' : '0'; // Aprovar ou Reprovar

		evaluation.addClass('hidden');

		$.ajax({
			url: url,
			type: 'GET',
			dataType: 'json'
		})
		.done(function() {
			evaluation.remove();
		}).error( function(xhr, ajaxOptions, thrownError) {
        console.log(xhr,ajaxOptions,thrownError );
		}).fail(function() {
			evaluation.removeClass('hidden');
		}).always(function() {
			// console.log("complete");
		}).success(function(data){
		  if(data.success){
        notificationMsg(data['message'], 'success');
      }
      var QdtNotify = document.getElementsByClassName('qtd-notification');
      url = 'index.php?route=common/notification/getQuantityNotification&token=';
      url += token + '&selected=' + id + '&action=update&status=';
      url += $(this).hasClass('js-approve') ? '1' : '0'; // Aprovar ou Reprovar
      $.ajax({
        url: url,
        type: 'GET',
        dataType: 'json'
      }).success(function(val) {
        if (val.value) {

          (val.value) > 9? '9+' : QdtNotify.item(0).textContent = val.value;
        }
      });
    })
	});
	/* Final - Avaliações */


});