(function ($) {
    "use strict";

    /*==================================================================
    [ Modal ]*/
    $('.modal-subscribe').on('click',function(e){
        e.stopPropagation();
    })
    
    $('.btn-close-modal').on('click', function(){
        $('#subscribe').modal('hide');
    });

})(jQuery);


function getNetworks(node) {
    $.get( "wifi_networks.php", function( data ) {
        //console.log(data);
        let networks = JSON.parse(data);
	var selectedItem = $(node).find(":selected").text();
	//console.log(selectedItem + ' was selected...');
	$(node).empty();
	$(node).append(new Option('Select a wireless network...','selectItem'));
	//$(node).html('<option value="SelectItem">Select a network<option>');
	for (const [key, value] of Object.entries(networks)) {
            //console.log("Adding "+key+" to networks...");
            //$(node).append('<option value="'+key+'">'+key+'</option>');
	    $(node).append(new Option(key,key));
	    if (key == selectedItem) {
		//console.log('Updating to previously selected item!');
		$(node + ' option[value='+selectedItem+']').attr('selected','selected');
	    }
        }

      });
}

getNetworks('#wifiscan');
let scan = setInterval(function () {
    networks = getNetworks('#wifiscan');
}, 20000);

let timeElapsed = 0

function updateProgressBar(node) {

  //$(node).html((timeToReload-timeElapsed)+'s to reload');
  let newWidth = Math.round((timeElapsed/timeToReload)*100);
  if (newWidth > 100) {
    newWidth = 100;
  }
  //$(node).css('width',newWidth+'%');
  
  $(node).animate({width: newWidth+'%'},1000,'linear');
}


let progress = setInterval(function () {
    timeElapsed++;
    updateProgressBar('#myBar');
}, 1000);
