<?php
include '../../../auth.php';
?>
<!DOCTYPE html>
<html>
   <head>
      <meta charset="UTF-8">
	  <link rel="stylesheet" href="style.css">
      <link rel="stylesheet" href="../../../script/tocas/tocas.css">
      <script type='text/javascript' src="../../../script/tocas/tocas.js"></script>
      <script src="../../../script/jquery.min.js"></script>
      <title>WIFI</title>
      <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
   </head>


<body>
<div class="ts fluid borderless slate">
	<div class="ts segment" style="width:100%;">
		<div class="ts header">
			Connect New Wi-Fi Network
			<div class="sub header">All the WiFi SSID scanned within the range of the onboard WLAN card.</div>
		</div>
	</div>
	<div class="ts container">


	</div>
</div>
<br>
<?php
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    echo '<div class="ts message">
    <div class="header">Not Supported OS</div>
    <p>This function is not supported in Windows Host.</p>
</div>';
	exit(0);
} 
?>
<div class="ts container">
<div class="ts cards" id="main_wifi">

</div>

		
		
	</div>
<div class="ts modals dimmer">
<dialog class="ts basic modal" id="modal" style="background-color:white" close>
    <div class="header" style="color:black" id="head_modal">
       
    </div>
	<div class="content" style="color:black" id="content_modal">
		
    </div>
	<div class="actions">
        <Button class="ts primary button" id="connect_btn">Connect</button>
			<button class="ts negative button">Cancel</button>
    </div>
</dialog>
</div>

<div id="msgbox" class="ts bottom right snackbar">
    <div class="content">
        Your request is being processed now.
    </div>
</div>
<script>
startup();

var previouswifi;

function startup(){
//Please ADD ALL LOAD ON STARTUP SCRIPT HERE
	scannetwork();
}

function scannetwork(){
		$('#main_wifi').html("");
		 $("#availablewifi").html('<tr><th>Wi-Fi name</th></tr>');		 
		$.ajaxSettings.async = false; 
		$.getJSON("opr.php?opr=scan", function(result){
            $.each(result, function(i, field){
					var Quality = "icon-0";
					var Quality_int = parseInt(field["Quality"].split("/")[0])/parseInt(field["Quality"].split("/")[1]);
					if(Quality_int == 0){
						Quality = "icon-0";
					}else if(Quality_int > 0 && Quality_int <= 0.25){
						Quality = "icon-1";
					}else if(Quality_int > 0.25 && Quality_int <= 0.5){
						Quality = "icon-2";
					}else if(Quality_int > 0.5 && Quality_int <= 0.75){
						Quality = "icon-3";
					}else if(Quality_int > 0.75 && Quality_int <= 1){
						Quality = "icon-Full";
					}
					$('#main_wifi').append('<div class="ts card"><div class="content"><div class="ts medium comments"><div class="comment"><div class="avatar"><span class="' + Quality + '" style="font-size:2.5em"></span></div><div class="content"><p class="author">' + field["ESSID"] + '</p><div class="text">' + field["Encrpytion_Method"] + '</div><div class="actions" id="act_wifi"><a onclick="conn_modal(this)" ssid="' + field["ESSID"] + '" encrypt="' + field["Encrpytion_Method_Shortname"] + '">Connect</a></div></div></div></div></div></div>');
            });
        });
		$("#main_wifi").append('<div class="ts card"><div class="content"><div class="ts medium comments"><div class="comment"><div class="avatar"><i class="big add icon"></i></div><div class="content"><a class="author">Add new Wi-Fi</a><div class="text">Add new network</div><div class="actions"><a onclick="redirect()">Add</a></div></div></div></div></div></div>');
		$.ajaxSettings.async = true; 

}

// not fixed
function redirect(){
	window.location = "addnew.php";
}
//

function conn_modal(ssid){
	var method = "";
	if($(ssid).attr("encrypt") == "No"){
		method = "no";
	}else if($(ssid).attr("encrypt") == "802.1x"){
		method = "802.1x"
	}else if($(ssid).attr("encrypt") == "WPA2"){
		method = "WPA2"
	}
	
		ts('#modal').modal({
			approve: '.primary',
			deny: '.negative',
			onDeny: function() {
				msg("Cancelled");
			},
			onApprove: function() {
				submit($(ssid).attr('ssid'),method);
			}
		}).modal("show");
	$('#head_modal').html("Connecting to : " + $(ssid).attr('ssid'));
	$('#content_modal').html('<form id="form" class="ts form" id="wifi" action=""><div class="fields">' + form($(ssid).attr('encrypt')) +'</div><input type="hidden" id="ssid" name="ssid" value="' + $(ssid).attr('ssid') + '"><input type="hidden" id="encryption" name="encryption" value="' + method + '"></form>');
	
	var saved=false;
	$("#connect_btn").removeAttr("disabled");
	$.getJSON("opr.php?opr=view_wifistorage", function(result){
           $.each(result, function(i, wifiarray){
			if(wifiarray["ssid"] == $(ssid).attr("ssid")){
				saved=true;
				$('#content_modal').html("Record already exists. You have to remove the WiFi Network Record before reconnecting.");
	$("#connect_btn").attr("disabled","true");
			}
		});
    });
}

function form(method){
console.log(method);
if(method == "WPA2"){
	return '<div class="sixteen wide field"><input id="psk" name="psk" placeholder="Password" type="password"></div>';
}else if(method == "802.1x"){
	return '<div class="eight wide field"><input id="identity" name="identity" placeholder="Username" type="text"></div><div class="eight wide field"><input ut id="psk" name="psk" placeholder="Password" type="password"></div>';
}else if(method == "No"){
	return 'Open Network. No Password Required';
}else{
	return 'System cannot determine wifi encrpyion method.';
}
}

function msg(content) {
		ts('.snackbar').snackbar({
			content: content,
			actionEmphasis: 'negative',
		});
}
	
function submit(){
	$.post( "opr.php?opr=addwifi", $( "#form" ).serialize()).done(function() {
		msg("WPA Updated. Restarting wireless interface services.");
	$.ajax({url:"opr.php?opr=restart",async:false});
		msg('Added Wi-Fi Network.');
  })
  .fail(function() {
    msg( "Failed" );
  });
}	
</script>

</body>
</html>