"use strict";
var KTDatatablesBasicBasic = function() {
var table 

var columnsToHide = new Array();
	var initTable1 = function() {
		
		table = $('#kt_table_1');

		// begin first table
		table.DataTable({
			"iDisplayLength":50,
			"processing":true,
			"serverSide":true,
			responsive: true,
			"ajax": { url: "be_services/leads/retrieveData.php",
			type:"POST",
			'data': function(data){
          
          var capFrom = $('#capMin').val();
          var capTo = $('#capMax').val();

			data.campaignId = window.campaignId
          data.applyFilter = 0;
          
          data.searchCapFrom = capFrom
          data.searchCapTo = capTo
          data.searchByGender = $('#sesso').val();
          data.searchByCity = $('#citta').val();

			data.filterCount = window.filtersNum
          for(var f = 0; f < window.filtersNum; f++)
          {
          	data['filter'+f] = new Object();
          	data['filter'+f].qId = $("#filtro"+f).data("qid");
          	data['filter'+f].aId = $("#filtro"+f).val()

          	if($("#filtro"+f).val() != 0)
          	{
          		data.applyFilter = 1
          	}
          }
          console.log(data)
      }
  },
			"columnDefs": [
            {
            },
            ],
            dom: 'liprtip',
		});

	};
		

	return {

		//main function to initiate the module
		init: function() {
			initTable1();
		},

	};

}();

jQuery(document).ready(function() {
	KTDatatablesBasicBasic.init();

	$(".filtra").on("click",function(){
		console.log(window.filtersNum)
		$('#kt_table_1').DataTable().clear().destroy();
		KTDatatablesBasicBasic.init();
	})

	$(".esporta").on("click",function(){

		console.log($('#kt_table_1').DataTable().page.info())
		var capFrom = $('#capMin').val();
		var capTo = $('#capMax').val();

		var data = new Object();
		data.applyFilter = 0;
		data.campaignId = window.campaignId
        data.start =  $('#kt_table_1').DataTable().page.info().start 
        data.length = $('#kt_table_1').DataTable().page.info().length
          data.searchCapFrom = capFrom
          data.searchCapTo = capTo
          data.searchByGender = $('#sesso').val();
          data.searchByCity = $('#citta').val();

			data.filterCount = window.filtersNum
          for(var f = 0; f < window.filtersNum; f++)
          {
          	data['filter'+f] = new Object();
          	data['filter'+f].qId = $("#filtro"+f).data("qid");
          	data['filter'+f].aId = $("#filtro"+f).val()

          	if($("#filtro"+f).val() != 0)
          	{
          		data.applyFilter = 1
          	}
          }

          console.log(data)

		$.ajax({ 
			url: "be_services/leads/exportData.php",
			method:"POST",
			data: data,
			success: function(data)
			{
				console.log(data)
				document.location = data;
			}
  })
		
		
	})



});