<?php
session_start();
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/components/check.php";
include_once($path);

?>
<!--
 =========================================================
* Black Dashboard PRO - v1.1.1
=========================================================

* Product Page: https://themes.getbootstrap.com/product/black-dashboard-pro-premium-bootstrap-4-admin/
* Copyright 2019 Creative Tim (https://www.creative-tim.com)

* Coded by Creative Tim

=========================================================

* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-->


<!DOCTYPE html>
<html lang="en">
<head>

<?php

$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/components/header.php";
include_once($path);

?>

<script src="/js/js_page_sspecific/main_elements2.js" type="text/javascript"  ></script>
<script src="/js/sw_support/Logger.js" type="text/javascript"  ></script>
<script src="/js/sw_support/GlobalLoader.js" type="text/javascript"  ></script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js" type="text/javascript"></script>
<link href="/css/dash_style.css" rel="stylesheet" />
  
<script>
	var wss_port = 18892;
	var addr = "wss://rbf.nanodesign.sk:" + wss_port + "/websocket/";

	let msg_tree = {};
	let msg_types = {};
	let msg_subtypes = {};
	let coin_data_types = {};
	coin_data_types[1] = {};
	coin_data_types[2] = {};

	let ws = new WebSocket(addr);
	ws.onmessage = (msg) => {
		const data = JSON.parse(msg["data"]);

		msg_types[data["type"]] = data;
		msg_subtypes[data["subtype"]] = data;

		if(data["type"] === "Update" && data["subtype"] === "new_coin_data") {
			for(const d of data["data"]["data"]) {
				coin_data_types[data["data"]["device_type"]][d["type"]] = d;
			}
		}

		console.log(JSON.parse(msg["data"]));
	};


	// var global_socket_wrapper = new SocketWrapper( addr );
	var debug = <?php	if ( $_GET["debug"] == 1 ) echo 1; else echo 0; ?>;

	// var main_window = 0;
	// var global_init = 0;
	// var load = new Loader();

    // var public_name_table = 0;	
    // var gLoadingDone = 0;
    // var gLoadDoneInterval = 0;
	
	// function handleMsgs( type, device_type, data, coin_id ) {
		
	// 	const d = new Date();
	// 	const timestamp = d.getTime();
	// 	main_window.updateDeviceActivity(coin_id, timestamp);

	// 	if ( device_type == 2 ) {
	// 		if ( type == 2 ) {
	// 			//report data
    // 	        main_window.setDeviceBattery( coin_id, data["battery"]*25, data["charger"] );
	// 			main_window.setDeviceFwVersion( coin_id, data["system_v"], data["b_system_v"]);
	// 			main_window.setDeviceState(coin_id, data["state"]);
    	        
	// 		}

	// 		if ( type == 1 ) {
	// 			// ignore messages with no actual light/accelerometer data
	// 			if(data["g_1"].length > 0) {
	// 				main_window.pushGraphDataToDevice(coin_id, "graph1", data);
	// 			}
	// 		}
	// 		if ( type == 5 ) {
	// 			// calculated values
	// 			main_window.setDeviceValue( coin_id, "hr", parseInt(  data["hr"] ) );

	// 			let spo2 = parseInt(data["spo2"]);
	// 			if(spo2 > 100) {
	// 				spo2 = 100;
	// 			} else if (spo2 < 90) {
	// 				spo2 = 90;
	// 			}

	// 			main_window.setDeviceValue( coin_id, "spo2", spo2 );
	// 			main_window.setDeviceValue( coin_id, "rr", parseInt(  data["rr"] ) );
	// 			main_window.setDeviceValue( coin_id, "resp1", parseInt(  data["resp"] ) );
	// 			main_window.setDeviceValue( coin_id, "stress", parseInt(  data["stress"] ) );
	// 			main_window.setDeviceSignalQ( coin_id, data["signal_q"]);
	// 		}
			
	// 		if ( type == 7 ) {
	// 			//markers
	// 			main_window.setDeviceMarker( coin_id );
				
	// 		}
			
	// 		if ( type == 6 ) {
	// 			//memory type
	// 		}
	// 		if ( type == 4 ) {
	// 			//acc
	// 			main_window.setDeviceRotation( coin_id, data["rotation"]);
	// 			main_window.setDeviceActivity( coin_id, data["acc"]);
	// 			main_window.setDeviceValue( coin_id, "fall", parseInt(  data["fall"] ) );
	// 		}		
			
	// 		if ( type == 8 ) {
			
	// 		}
			
	// 		if ( type == 9 ) {
	// 			$("#temp-" + coin_id).html( parseInt( data["temp"]*100 ) / 100 );

	// 			let temp1 = parseInt(data["temp1"] * 100) / 100;
	// 			if(temp1 <= 36) {
	// 				temp1 = "--";
	// 			}
	// 			main_window.setDeviceValue( coin_id, "temp1", temp1);
	// 			//errors
	// 		}			
		
	// 	} 
		
	// 	if ( device_type == 1 ) {
		    
		      
	// 		if ( type == 0 ) {
	// 		}
			
	// 		if ( type == 9 ) {
	// 			let temp1 = parseInt(data["temp1"] * 100) / 100;
	// 			if(temp1 <= 36) {
	// 				temp1 = "--";
	// 			}
	// 			main_window.setDeviceValue( coin_id, "temp1", temp1);
	// 		}
			
	// 		if ( type == 2 ) {
	// 			main_window.setDeviceBattery( coin_id, data["battery"]*25 );
	// 		}
			
	// 		if ( type == 7 ) {
	// 			var t = data["temp"];
	// 			var t1 = data["temp1"];
	// 		}
			
	// 		if ( type == 8 ) {
			
	// 		}
			
	// 		if ( type == 10 ) {
	// 		}
	// 	} 
		
	// }
	
	// function liveUpdate() {

	// 	this.handleLive = function( data ) {
	// 		if ( global_init == 0 )
	// 			return 0;
			
	// 		main_window.setDeviceSNR( data["coin_id"], data[ "snr" ] );
			
	// 		for ( msg in data["data"] ) {
	// 			if ( "type" in data["data"][msg] ) {
	// 			}else{
	// 				continue;
	// 			}

	// 			handleMsgs( data["data"][msg]["type"], data["device_type"], data["data"][msg], data["coin_id"]  );
	// 		}
			
	// 	}
		
	// }

	// function start1() {
	// 	console.log( "Running" );
	// 	main_window = new MainDashboard( "active_meas_with_graph", "active_meas", "active_coms", "nonresponsive", gElemContainer );
	// 	// document.getElementById('disable-device-transitions').addEventListener("change", (e) => main_window.lock_device_positions = e.target.value);
	// 	createNewCommands( "task", { "task_name": "GetData", "params": {"query": 'SELECT id, name, type FROM public."Coins"' } }, processCoinNameIdTable ); 
		
	// }
	
    // function createDashboard( data ) {
	// 	data = JSON.parse( data["result"] );
		
	// 	var t = 0; 
	// 	for( const device_id in data ) {
	// 		try{
	// 			 t = public_name_table[ device_id ];
	// 		}catch(ex) {
	// 			t = public_name_table[ device_id ];
	// 		}
			
	// 		console.log( t );
	// 		if ( typeof t !== 'undefined' ) 
	// 			main_window.pushDevice( device_id, t, data[ device_id ] );
	// 	}
		
	// 	main_window.init();
	// 	global_init = 1;
		
	// }
	
	// function processCoinNameIdTable( data ) {
	// 	data = data["result"];
		
	// 	public_name_table = {};
		
	// 	for( var x = 0; x < data.length; x++ ) {
	// 		var type = data [ x ][ 2 ];
	// 		var name = data [ x ][ 1 ];
	// 		var id = data [ x ][ 0 ];
	// 		if ( data [ x ][ 1 ] == null ) {
	// 			name = "N/O";
	// 		}  
	// 		main_window.device_name_table[ id ] = name;
	// 		if ( type > 2 )
	// 			type = 2; 
	// 		public_name_table[ id ] = type;
	// 	}
	// 	createNewCommands( "task", { "task_name": "GetDataFromModel", "params": {"model": 'LiveViewStorage' } }, createDashboard ); 
	// }
	
	// function start() {

	// 	var ff = new liveUpdate();

	// 	var ff3 = Math.floor(Date.now() / 1000);

	// 	global_socket_wrapper.init();
	// 	global_socket_wrapper.registerLiveUpdater("new_coin_data", ff);
	// 	gElemContainer.load();
	// 	setTimeout( start1, 3500 );
	// 	// setInterval( animationStep, 200 );
	// 	// setInterval(main_window.stepAnimation, 200);
	// 	gLoadDoneInterval = setInterval( checkLoadingDone, 3500 );
	// }
	// function checkLoadingDone() {
	// 	if ( gLoadingDone > 3 ) {
	// 		load.clear();
	// 		clearInterval( gLoadDoneInterval );
	// 	}
	// }

	// function searchChanged (){
	// 	main_window.handleSearchChange($("#search-bar").val());
	// }

	// function clearSearch() {
	// 	$('#search-bar').val("");
	// 	searchChanged();
	// }

	// function showMeas( id, conf ) {
	// 	window.location = "https://gui.iotmed.sk/components/coin/coin_meas.php?dev_id=" + id + "&conf=" + conf;
	// 	return 1;
	
	// }

	// function nameChanged(id, name) {
	// 	// showInfoMsg( "Data saved", 2);
	// 	log.print( LogPriority.LOW, "Name of coin " + id + " was changed to " + name, "MAIN WINDOW" );
	// 	main_window.setDeviceName(id, name);
	// }
	
	// function changeName( id ) {
	// 	$('#coin_name_change_id').text(id);
	// 	// active_id = id;
	// 	// $("#exampleModal").modal( 'toggle' );
	// 	// $("#meno_s" ).html( id );
	// }
	
	// function sendNameChange() {
	
	// 	var names = $("#new_name").val();
	// 	var id = $('#coin_name_change_id').text();
		
	// 	var timestamp = Date.now()/1000;
	// 	createNewCommands( "task", { "task_name": "GetData", "params": {"query": "UPDATE public.\"Coins\" SET  \"name\" = '" + names + "' WHERE id = " + id } }, () => nameChanged(id, names) );
	// 	// showInfoMsg( "Saving data...", 1);
	// 	//setTimeout( requestCoinNameIdTable, 6000 );
	// 	log.print( LogPriority.LOW, "Requesting to change name of coin " + id + " to " + name, "MAIN WINDOW" );
		
	// 	//requestCoinNameIdTable();
	// 	$("#exampleModal").modal("hide");

	// }

		
</script>		
</head>

<body class="sidebar-mini" onload="start()" style="overflow:hidden;" >
	<script>
		load.init();
	</script>
	 <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="false" >
	  <div class="modal-dialog" role="document" id="dialog-windows">
	    <div class="modal-content">
	      <div class="modal-header">
	        <h5 class="modal-title" id="exampleModalLabel">Zmena mena <span id="meno_s"></span> pre id <span id="coin_name_change_id">--</span></h5>
	        <button type="button" class="close" data-dismiss="modal" aria-label="Zatvoriť">
	          <span aria-hidden="true">&times;</span>
	        </button>
	      </div>
	      <div class="modal-body">
	        <p> <span class="item-elem">Nove meno:</span> <span class="item-elem" ><input type="text" id="new_name" placeholder="Nove meno..." /></span> </p>
	        <p> <span></span> </p>
	        
	      </div>
	      <div class="modal-footer">
	        <button type="button" class="btn btn-primary" data-dismiss="modal">Zatvoriť</button>
	        <button type="button" class="btn btn-secondary" onclick="sendNameChange();">Uložiť</button>
	      </div>
	    </div>
	  </div>
	</div>
	
	  <div class="wrapper">
	    <div class="navbar-minimize-fixed">
	      <button class="minimize-sidebar btn btn-link btn-just-icon">
	        <i class="tim-icons icon-align-center visible-on-sidebar-regular text-muted"></i>
	        <i class="tim-icons icon-bullet-list-67 visible-on-sidebar-mini text-muted"></i>
	      </button>
	    </div>
	        <?php
	        
	        $path = $_SERVER['DOCUMENT_ROOT'];
        $path .= "/components/sidebar.php";
        include_once($path);
        
        ?>
	    <div class="main-panel ps ps--active-y ps--scrolling-y momentum-scroll scroll-pane" style="overflow-y: scroll !important;">
	    
	    
	    
	      <!-- Navbar -->
		<?php
	        
	        $path = $_SERVER['DOCUMENT_ROOT'];
        	$path .= "/components/plugin.php";
        	include_once($path);
        
        ?>
	    <?php
	        
	        $path = $_SERVER['DOCUMENT_ROOT'];
            $path .= "/components/navbar.php";
            include_once($path);
        
        ?>
	      <!-- End Navbar -->
		      <div class="content">
			 		<p><i class="tim-icons icon-zoom-split" style="display:inline-block;"  ></i><input placeholder="Vyhladať..." id="search-bar" value="" oninput="searchChanged()" class="form-control" style="display:inline-block;width:95%;margin-left:10px;" /><i class="tim-icons icon-simple-remove" onclick="clearSearch()" style="margin-left:10px;display:inline-block;"  ></i></p>

					<label style="color: rgba(255, 255, 255, 0.8); line-height: 2rem; font-size: 1rem">
						<input type="checkbox" id="disable-device-transitions"/>
						Uzamknúť pozíciu zariadení
					</label>

					<details open>
						<summary style="width:100%; text-align:left!important;" class="text-center">
							<h3 style="display: inline-block;">Aktívne meranie s krivkou</h3>
						</summary>
						<div class="row row-cols-1 row-cols-md-2" id="active_meas_with_graph"></div>
					</details>

					<details open>
						<summary style="width:100%; text-align:left!important;" class="text-center">
							<h3 style="display: inline-block;">Aktívne meranie</h3>
						</summary>
						<div class="row row-cols-1 row-cols-md-2" id="active_meas"></div>
					</details>

					<details open>
						<summary style="width:100%; text-align:left!important;" class="text-center">
							<h3 style="display: inline-block;">Aktívna komunikacia</h3>
						</summary>
						<div class="row row-cols-1 row-cols-md-2" id="active_coms"></div>
					</details>

					<details open>
						<summary style="width:100%; text-align:left!important;" class="text-center">
							<h3 style="display: inline-block;">Nereaguje</h3>
						</summary>
						<div class="row row-cols-1 row-cols-md-2" id="nonresponsive"></div>
					</details>
		      </div>
	      </div>
			
		<?php
	        
	        $path = $_SERVER['DOCUMENT_ROOT'];
            $path .= "/components/footer.php";
            include_once($path);
            
        ?>
	    </div>
	  <!--   Core JS Files   -->
	    <?php
	        
	        $path = $_SERVER['DOCUMENT_ROOT'];
            $path .= "/components/code_footer.php";
            include_once($path);
            
        ?>

</body>

</html>
