function afficherPlus() {
  var rechercher = $('#rechercher').val();
  var minPrice = $('#minPrice').val();
  var maxPrice = $('#maxPrice').val();
  var marque = $('#marque').val();
  var critere1 = $('#critere1').val();
  var critere2 = $('#critere2').val();
  var critere3 = $('#critere3').val();
  var critere4 = $('#critere4').val();
  var critere5 = $('#critere5').val();
  var critere6 = $('#critere6').val();
  var critere7 = $('#critere7').val();
  var critere8 = $('#critere8').val();
  var critere9 = $('#critere9').val();
  var critere10 = $('#critere10').val();
  var liafficherplus = $('#liafficherplus');

  $('#spinnerload').removeClass('d-none');
  $('#avoirplus').text('chargement');
  $('#seeMoreIcon').addClass('d-none');

  $.ajax({
    url: dataFamille.filter_products_second,
    type: 'POST',
    timeout: 400000,
    data: {
      'rechercher': rechercher,
      'minPrice': minPrice,
      'maxPrice': maxPrice,
      'marque': marque,
      'critere1': critere1,
      'critere2': critere2,
      'critere3': critere3,
      'critere4': critere4,
      'critere5': critere5,
      'critere6': critere6,
      'critere7': critere7,
      'critere8': critere8,
      'critere9': critere9,
      'critere10': critere10,
      'idFamille': dataFamille.id,
    },
    dataType: 'json',
    success: function (data) {
      $('#rechercher').val(data.rechercher);
      $('#minPrice').val(data.minPrice);
      $('#maxPrice').val(data.maxPrice);
      $('#marque option[value="' + data.marque + '"]').attr('selected', 'selected');
      $('#critere1 option[value="' + data.critere1 + '"]').attr('selected', 'selected');
      $('#critere2 option[value="' + data.critere2 + '"]').attr('selected', 'selected');
      $('#critere3 option[value="' + data.critere3 + '"]').attr('selected', 'selected');
      $('#critere4 option[value="' + data.critere4 + '"]').attr('selected', 'selected');
      $('#critere5 option[value="' + data.critere5 + '"]').attr('selected', 'selected');
      $('#critere6 option[value="' + data.critere6 + '"]').attr('selected', 'selected');
      $('#critere7 option[value="' + data.critere7 + '"]').attr('selected', 'selected');
      $('#critere8 option[value="' + data.critere8 + '"]').attr('selected', 'selected');
      $('#critere9 option[value="' + data.critere9 + '"]').attr('selected', 'selected');
      $('#critere10 option[value="' + data.critere10 + '"]').attr('selected', 'selected');
      liafficherplus.replaceWith(data.listP);
      showImages('.product-list__item-img');
    }

  });
}

function afficherPlus2() {
  var rechercher = $('#rechercher').val();
  var minPrice = $('#minPrice').val();
  var maxPrice = $('#maxPrice').val();
  var marque = $('#marque').val();
  var critere1 = $('#critere1').val();
  var critere2 = $('#critere2').val();
  var critere3 = $('#critere3').val();
  var critere4 = $('#critere4').val();
  var critere5 = $('#critere5').val();
  var critere6 = $('#critere6').val();
  var critere7 = $('#critere7').val();
  var critere8 = $('#critere8').val();
  var critere9 = $('#critere9').val();
  var critere10 = $('#critere10').val();
  var liafficherplus = $('#liafficherplus');

  $('#spinnerload').removeClass('d-none');
  $('#avoirplus').text('chargement');
  $('#seeMoreIcon').addClass('d-none');

  $.ajax({
    url: dataFamille.filter_products_third,
    type: 'POST',
    timeout: 400000,
    data: {
      'rechercher': rechercher,
      'minPrice': minPrice,
      'maxPrice': maxPrice,
      'marque': marque,
      'critere1': critere1,
      'critere2': critere2,
      'critere3': critere3,
      'critere4': critere4,
      'critere5': critere5,
      'critere6': critere6,
      'critere7': critere7,
      'critere8': critere8,
      'critere9': critere9,
      'critere10': critere10,
      'idFamille': dataFamille.id,
    },
    dataType: 'json',
    success: function (data) {
      $('#rechercher').val(data.rechercher);
      $('#minPrice').val(data.minPrice);
      $('#maxPrice').val(data.maxPrice);
      $('#marque option[value="' + data.marque + '"]').attr('selected', 'selected');
      $('#critere1 option[value="' + data.critere1 + '"]').attr('selected', 'selected');
      $('#critere2 option[value="' + data.critere2 + '"]').attr('selected', 'selected');
      $('#critere3 option[value="' + data.critere3 + '"]').attr('selected', 'selected');
      $('#critere4 option[value="' + data.critere4 + '"]').attr('selected', 'selected');
      $('#critere5 option[value="' + data.critere5 + '"]').attr('selected', 'selected');
      $('#critere6 option[value="' + data.critere6 + '"]').attr('selected', 'selected');
      $('#critere7 option[value="' + data.critere7 + '"]').attr('selected', 'selected');
      $('#critere8 option[value="' + data.critere8 + '"]').attr('selected', 'selected');
      $('#critere9 option[value="' + data.critere9 + '"]').attr('selected', 'selected');
      $('#critere10 option[value="' + data.critere10 + '"]').attr('selected', 'selected');
      liafficherplus.replaceWith(data.listP);
      showImages('.product-list__item-img');
    }
  });
}

function afficherPlus3() {
  var rechercher = $('#rechercher').val();
  var minPrice = $('#minPrice').val();
  var maxPrice = $('#maxPrice').val();
  var marque = $('#marque').val();
  var critere1 = $('#critere1').val();
  var critere2 = $('#critere2').val();
  var critere3 = $('#critere3').val();
  var critere4 = $('#critere4').val();
  var critere5 = $('#critere5').val();
  var critere6 = $('#critere6').val();
  var critere7 = $('#critere7').val();
  var critere8 = $('#critere8').val();
  var critere9 = $('#critere9').val();
  var critere10 = $('#critere10').val();
  var liafficherplus = $('#liafficherplus');

  $('#spinnerload').removeClass('d-none');
  $('#avoirplus').text('chargement');
  $('#seeMoreIcon').addClass('d-none');

  $.ajax({
    url: dataFamille.filter_products_four,
    type: 'POST',
    timeout: 400000,
    data: {
      'rechercher': rechercher,
      'minPrice': minPrice,
      'maxPrice': maxPrice,
      'marque': marque,
      'critere1': critere1,
      'critere2': critere2,
      'critere3': critere3,
      'critere4': critere4,
      'critere5': critere5,
      'critere6': critere6,
      'critere7': critere7,
      'critere8': critere8,
      'critere9': critere9,
      'critere10': critere10,
      'idFamille': dataFamille.id,
    },
    dataType: 'json',
    success: function (data) {
      $('#rechercher').val(data.rechercher);
      $('#minPrice').val(data.minPrice);
      $('#maxPrice').val(data.maxPrice);
      $('#marque option[value="' + data.marque + '"]').attr('selected', 'selected');
      $('#critere1 option[value="' + data.critere1 + '"]').attr('selected', 'selected');
      $('#critere2 option[value="' + data.critere2 + '"]').attr('selected', 'selected');
      $('#critere3 option[value="' + data.critere3 + '"]').attr('selected', 'selected');
      $('#critere4 option[value="' + data.critere4 + '"]').attr('selected', 'selected');
      $('#critere5 option[value="' + data.critere5 + '"]').attr('selected', 'selected');
      $('#critere6 option[value="' + data.critere6 + '"]').attr('selected', 'selected');
      $('#critere7 option[value="' + data.critere7 + '"]').attr('selected', 'selected');
      $('#critere8 option[value="' + data.critere8 + '"]').attr('selected', 'selected');
      $('#critere9 option[value="' + data.critere9 + '"]').attr('selected', 'selected');
      $('#critere10 option[value="' + data.critere10 + '"]').attr('selected', 'selected');
      liafficherplus.replaceWith(data.listP);
      showImages('.product-list__item-img');
    }
  });
}

function showImages(el) {
  var windowHeight = jQuery(window).height();
  $(el).each(function () {
    var thisPos = $(this).offset().top;

    var topOfWindow = $(window).scrollTop();
    if (topOfWindow + windowHeight - 200 > thisPos) {
      $(this).addClass("fadeIn");
    }
  });
}

function filter() {
  var rechercher = $('#rechercher').val();
  var minPrice = $('#minPrice').val();
  var maxPrice = $('#maxPrice').val();
  var marque = $('#marque').val();
  var critere1 = $('#critere1').val();
  var critere2 = $('#critere2').val();
  var critere3 = $('#critere3').val();
  var critere4 = $('#critere4').val();
  var critere5 = $('#critere5').val();
  var critere6 = $('#critere6').val();
  var critere7 = $('#critere7').val();
  var critere8 = $('#critere8').val();
  var critere9 = $('#critere9').val();
  var critere10 = $('#critere10').val();
  var list = $('#ulProducts');

  $('.product-list__body').css('background-color', 'transparent');
  $('#loading-image').show();

  $.ajax({
    url: dataFamille.filter_products_first,
    type: 'POST',
    timeout: 400000,
    data: {
      'rechercher': rechercher,
      'minPrice': minPrice,
      'maxPrice': maxPrice,
      'marque': marque,
      'critere1': critere1,
      'critere2': critere2,
      'critere3': critere3,
      'critere4': critere4,
      'critere5': critere5,
      'critere6': critere6,
      'critere7': critere7,
      'critere8': critere8,
      'critere9': critere9,
      'critere10': critere10,
      'idFamille': dataFamille.id,
    },
    dataType: 'json',
    success: function (data) {
      $('#productList').append(data);
      $('#rechercher').val(data.rechercher);
      $('#minPrice').val(data.minPrice);
      $('#maxPrice').val(data.maxPrice);
      $('#marque option[value="' + data.marque + '"]').attr('selected', 'selected');
      $('#critere1 option[value="' + data.critere1 + '"]').attr('selected', 'selected');
      $('#critere2 option[value="' + data.critere2 + '"]').attr('selected', 'selected');
      $('#critere3 option[value="' + data.critere3 + '"]').attr('selected', 'selected');
      $('#critere4 option[value="' + data.critere4 + '"]').attr('selected', 'selected');
      $('#critere5 option[value="' + data.critere5 + '"]').attr('selected', 'selected');
      $('#critere6 option[value="' + data.critere6 + '"]').attr('selected', 'selected');
      $('#critere7 option[value="' + data.critere7 + '"]').attr('selected', 'selected');
      $('#critere8 option[value="' + data.critere8 + '"]').attr('selected', 'selected');
      $('#critere9 option[value="' + data.critere9 + '"]').attr('selected', 'selected');
      $('#critere10 option[value="' + data.critere10 + '"]').attr('selected', 'selected');
      list.html(data.listP);
      showImages('.product-list__item-img');
    },
    complete: function () {
      $('#loading-image').hide();
      $('.product-list__body').css('background-color', 'rgba(127, 135, 184, 0.2)');
    }
  });
}

function resetFilter() {
  $('#rechercher').val('');
  $('#minPrice').val(0);
  $('#maxPrice').val(100000);

  if ($('#marque').val() != '') {
    tommarque.clear(); tommarque.clearOptions(); tommarque.sync();
  }

  if ($('#critere1').val() != '') {
    tomcritere1.clear(); tomcritere1.clearOptions(); tomcritere1.sync();
  }

  if ($('#critere2').val() != '') {
    tomcritere2.clear(); tomcritere2.clearOptions(); tomcritere2.sync();
  }

  if ($('#critere3').val() != '') {
    tomcritere3.clear(); tomcritere3.clearOptions(); tomcritere3.sync();
  }

  if ($('#critere4').val() != '') {
    tomcritere4.clear(); tomcritere4.clearOptions(); tomcritere4.sync();
  }

  if ($('#critere5').val() != '') {
    tomcritere5.clear(); tomcritere5.clearOptions(); tomcritere5.sync();
  }

  if ($('#critere6').val() != '') {
    tomcritere6.clear(); tomcritere6.clearOptions(); tomcritere6.sync();
  }

  if ($('#critere7').val() != '') {
    tomcritere7.clear(); tomcritere7.clearOptions(); tomcritere7.sync();
  }

  if ($('#critere8').val() != '') {
    tomcritere8.clear(); tomcritere8.clearOptions(); tomcritere8.sync();
  }

  if ($('#critere9').val() != '') {
    tomcritere9.clear(); tomcritere9.clearOptions(); tomcritere9.sync();
  }

  if ($('#critere10').val() != '') {
    tomcritere10.clear(); tomcritere10.clearOptions(); tomcritere10.sync();
  }
}

var settings = {
  maxItems: 1,
  allowEmptyOption: true,
  plugins: [
    'dropdown_input', 'clear_button'
  ],
};

var tommarque = new TomSelect('#marque', settings);
var tomcritere1 = new TomSelect('#critere1', settings);
var tomcritere2 = new TomSelect('#critere2', settings);
var tomcritere3 = new TomSelect('#critere3', settings);
var tomcritere4 = new TomSelect('#critere4', settings);
var tomcritere5 = new TomSelect('#critere5', settings);
var tomcritere6 = new TomSelect('#critere6', settings);
var tomcritere7 = new TomSelect('#critere7', settings);
var tomcritere8 = new TomSelect('#critere8', settings);
var tomcritere9 = new TomSelect('#critere9', settings);
var tomcritere10 = new TomSelect('#critere10', settings);
var timer;

$(document).ready(function () {

  settings.placeholder = "";
  tommarque.settings.placeholder = "";
  tomcritere1.settings.placeholder = "";
  tomcritere2.settings.placeholder = "";
  tomcritere3.settings.placeholder = "";
  tomcritere4.settings.placeholder = "";
  tomcritere5.settings.placeholder = "";
  tomcritere6.settings.placeholder = "";
  tomcritere7.settings.placeholder = "";
  tomcritere8.settings.placeholder = "";
  tomcritere9.settings.placeholder = "";
  tomcritere10.settings.placeholder = "";

  $("#rechercher, #marque, #critere1, #critere2, #critere3, #critere4, #critere5, #critere6, #critere7, #critere8, #critere9, #critere10").each(function () {
    $(this).val('');
    $(this).change(function () {
      filter();
    });
  });

  $("#idfilterProducts").submit(function (e) {
    e.preventDefault();
  });

  $("#minPrice, #maxPrice").bind('keyup', function () {
    clearTimeout(timer);
    timer = setTimeout(filter, 500)
  });

  $(window).scroll(function () {
    showImages('.product-list__item-img');
  });
});