<?php
// header('Content-type: application/json');
include '../config/database.php';
function _command(){
	$conn = _connectDB();
	$sql = "SELECT * FROM monitor";
	$result = $conn->query($sql);
	$res = [];
	if ($result->num_rows > 0) {
	    while($row = $result->fetch_assoc()) {
			array_push($res,$row);
	    }
	}
	return $res;
}


$data = [];
foreach(_command() as $key => $val){
	//$res = $val['Code'] .' '. $val['Version'] .' = '. $val['Status'] ;
	//array_push($data,$res);
	array_push($data,$val);
	// $data = [];
	// $data['name'] = base64_decode($val['line_name']);
	// $data['product'] = $val['line_product'];
	// $data['person'] = base64_decode($val['line_person']);
}


function _location($ip){
	$curl = curl_init();
	curl_setopt_array($curl, array(
	  CURLOPT_URL => "http://150.95.89.148:8181/?ip=".$ip,
	  CURLOPT_RETURNTRANSFER => true,
	  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	  CURLOPT_CUSTOMREQUEST => "GET"
	));
	$response = curl_exec($curl);
	$err = curl_error($curl);
	curl_close($curl);
	return $response;
}




function _getTime(){
	$current = date("Y-m-d H:i:s");  
	return $current;
}

// $now = date("Y-m-d H:i:s"); 
// if (_getTime() >= $now){
//  echo "true";
// }else{
//   echo "FAlse";
// }

// Array
// (
//     [Code] => HUAYLIKE
//     [Version] => V1
//     [Status] => RUN
//     [Timeout] => 2020-09-18 13:38:30
//     [Comment] => 
// )

?>

<!DOCTYPE html>
<html>
<head>
	<title>MONITOR</title>
	<link rel="stylesheet" type="text/css" href="assets/Semantic-UI-CSS/semantic.min.css">
	<script src="https://code.jquery.com/jquery-3.1.1.min.js"  integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="  crossorigin="anonymous"></script>
	<script src="assets/Semantic-UI-CSS/semantic.min.js"></script>
</head>
<body>
	<br><br>
	<div class="ui container">



		<div class="ui three column grid">

			<style>
				.ui.table td {
				    padding: 0em 0em !important;	
				    text-align: inherit;
				}
				.ui.grid>* {
				    padding-left: 0rem;
				    padding-right: 0rem;
				}
			</style>	


<table class="ui celled table">
  <thead>
    <tr>
      <th></th>
      <th><center>ชื่อบอท</center></th>
      <!-- <th><center>อัพเดทเมื่อ</center></th> -->
      <th><center>สถานะ</center></th>
      <th><center>รายละเอียด</center></th>
    </tr>
  </thead>
  <tbody>

  		<?php $total_group = 0; ?>
  		<?php $total_bot = 0; ?>
		<?php foreach ($data as $key => $val){ ?>
			<?php 
				$total_bot++;
				$total_group = $total_group+(int)$val['Total'];



				$spd = substr($val['Speed'],0,3);
				$scolor = '#000';
				if($spd == '0.0'){
					$scolor = 'green';
				}else if($spd == '0.1'){
					$scolor = '#ffa446';
				}else if($spd == '0.2'){
					$scolor = '#ff7b2e';	
				}else if($spd == '0.3'){
					$scolor = '#ff5200';
				}else if($spd == '0.4'){
					$scolor = '#ff2626';
				}

				if($val['Status'] == 'RUN'){
					$color = 'green';
				}else if($val['Status'] == 'OFFLINE'){
					$color = 'red';
				}
				$IP = '';
				if($val['ip'] != '0.0.0.0'){
					//$IP = json_decode(_location($val['ip']))->ip;
				}
			?>





    <tr>
      <td><img style="width: 30px;" src="assets/icon/<?php echo $val['Code']; ?>.jpg"></td>
      <td><b><a href="groups.php?product=<?php echo $val['Code']; ?>&version=<?php echo $val['Version']; ?>"><?php echo substr($val['Code'],0,12).' ['.$val['Version'].']'; ?></a></b> <br><span class="date"><?php echo $val['Timeout']; ?></td>
      <!-- <td><center><span class="date"><?php echo $val['Timeout']; ?></span></center></td> -->
      <td><center><span style="color:<?php echo $color; ?>"><?php echo $val['Status']; ?></span></center></td>
      <td><span class="date">อยู่ <b><?php echo $val['Total']; ?></b> กลุ่ม | SPEED : <span style="color:<?php echo $scolor; ?>"><?php echo $val['Speed']; ?></span> <br> [ <u><?php echo $val['ip']; ?></u> ] </span></td>
    </tr>












		  <?php } ?>


  </tbody>
  <tfoot>
    <tr>
    	<th></th>
    	<th>รวม <?php echo $total_bot;?> ตัว</th>
    	<!-- <th></th> -->
    	<th></th>
    	<th>รวม <?php echo $total_group;?> กลุ่ม</th>
  	</tr>
  </tfoot>
</table>








		</div>









	</div>


</body>
</html>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<audio id='alert' src="alert.mp3"></audio>
<script>

$(document).ready(function() {

	var vid = document.getElementById("alert");

	function _play() {
	    vid.play();
	}

	function _pause() {
	    vid.pause();
	}
	
	setInterval(function(){
		$.get( "http://150.95.89.148/api/py/monitor/api.php", function( data ) {
		    var a = jQuery.parseJSON(data);
		    var _count = 0;
			$.each(a, function(i, item) {
               	if(item.Status === 'OFFLINE'){_count++;}
            });	
			if(_count >= 7){
				_play();
				console.log("ERROR");
			}else{
				_pause();
				console.log("SUCCESS");

			}
		});
	},10000);



});

</script>