<!DOCTYPE html>
<?php
	include_once('/var/www/html/token.php');

  include_once('../config.php');

  include_once('../functions.php');
	
	if ($_SERVER['REQUEST_METHOD']=='POST') 

    $input = filter_input_array(INPUT_POST);

	else

    $input = filter_input_array(INPUT_GET);

	$user = strip_tags($input['u']);
	
  $server= strip_tags($input['s']);
	
  $token = $input['token'];
	
  mylog("{$user} Token is " . $token);

  function encrypt_decrypt($action, $string) {

		require('/var/www/privatekey.php');
		
		$output = false;
		
		$iv= '2017';
		
		$encrypt_method = "AES-256-CBC";
		
		$secret_key = $privatekey;
		
		$secret_iv = $iv;
		
		$key = hash('sha256', $secret_key);
		
		$iv = substr(hash('sha256', $secret_iv), 0, 16);
		
		if( $action == 'encrypt' ) {
		
			$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
		
			$output = base64_encode($output);
		
		} else if( $action == 'decrypt' ){

			$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);

		}

		return $output;

	}
	
  $show=0;

  $showUser = ['mlara','noemiy','noemi'];

  $tmpUser = encrypt_decrypt('decrypt', $user);

  if(in_array($tmpUser, $showUser)) 

    $show = 1;

  if(!empty($user)) 
  
    $show=1;
  
    $q = ($show==1)  ? "'getqueuestats.php'" : "";

  $agt = "'hudinfomem.php?token={$token}&s={$server}&user={$user}&a={$input['a']}&" . (isset($_GET['showlogout'])?"?showlogout=1":"") . "'";
	
	$token = md5("IOFIDOFJkjdfois..309=)d" . date("Y/m/d H:") . ((date("H") + 3) * 295));

?>
<script src="jquery-latest.js"></script>
<script>
var auto_refresh = function()
  {
     $.ajax({
        timeout: 800,
	      url: <?php echo $agt?>,
       }).done(function(data) {
        $("#alertinfo").html("");
	      $("#agentsinfo").html(data);
        setTimeout(auto_refresh, 1000);
      }).fail(function(e,data) {
        $("#alertinfo").html("System is under heavy load, please refresh or reload the page");
        setTimeout(auto_refresh, 30000);
      });
<?php if($show==1):?>
     $.ajax({
        timeout: 1500,
        method: 'GET',
	      url: <?php echo $q?>,
       }).done(function(data) {
	      $("#queuesinfo").html(data);
      }).fail(function(e, data) {
        $("#alertinfo").html(`Reload for queue statistics`);
        console.log(`Error fetching queue statistics:`);
        console.log(data);
        console.log(`Message:`);
        if (e && e.responseText) {
          console.log('Response:', e.responseText);
          $("#alertinfo").append('<br>' + e.responseText);
        } else if (e && e.statusText) {
          console.log('Status:', e.statusText);
          $("#alertinfo").append('<br>' + e.statusText);
        }
      });

<?php endif;?>
  };

<?php
if ($_GET['audio']=="1")  {
?>
   var audio_refresh = setInterval(
   function()
   {
      $("#playaudio").load(<?php echo $audio?>);
   }, 4000);
<?php
}
?>

</script>

<script>
$(document).ready(function () {

  setTimeout(auto_refresh, 1000);

  //auto_refresh();

});
</script>

<script type="text/javascript">
	function popup(url,name,ancho,alto) {
	var posicion_x; 
	var posicion_y; 
	posicion_x=(screen.width/2)-(ancho/2); 
	posicion_y=(screen.height/2)-(alto/2); 
	window.open(url, name, "width="+ancho+",height="+alto+",menubar=0,toolbar=0,directories=0,scrollbars=yes,resizable=no");
	}
</script>

<script type="text/javascript">
function popUp(URL) {
	var swidth= 600;
	var sheight = 600;
	posx=(screen.width/2)-(swidth/2); 
	posy=(screen.height/2)-(sheight/2);
	posx=swidth;
	posy=sheight;
	window.open(URL, 'Clocked Out Extension Disconnected on Active Call', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width='+posx+',height='+posy+',left = 390,top = 50');
}
</script>

<script type="text/javascript">
	$(document).ready(function(){
		$(".botonExcel").click(function(event) {
			console.log('Click');
			$("#datasent").val( $("<div>").append( $(".tblexport").eq(0).clone()).html());
			$("#frmexport").submit();
		});
	});
</script>

<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>MyLinguistica HUD</title>
    <link rel="stylesheet" href="bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
  </head>
  <body>
  <div id="alertinfo"></div>
  <div id="agentsinfo">
  Loading info...
   </div>
<table noborder width=100%><tr><td>
<?php
    if (isset($_GET['showlogout']))
        echo "<a href=hud.php?s=$server&u=$user&token=$token>Hide logout agents</a>";
    else
        echo "<a href=hud.php?s=$server&u=$user&showlogout=1&token=$token>Show logout agents</a>";
?>
<?php if($show==1):?>
</td><td><a href=agentsrt.php>Show agents response time (VoIP speed)</td></tr></table><br>
  Color code for queues:<br>
  <table class="table table-striped table-condensed table-bordered">
  <tr><td class="info">No additional agents needed</td><td class=warning>Less than optimal agents available</td><td class=danger>Insufficient agents for volume</td></tr>
  </table>

  <div id="queuesinfo">
    Loading Info...
   </div>

  <div id="playaudio">
  </div>
<?php endif;?>
</body>
</html>
