<?php

$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>
	var addrs = "wss://rbf.nanodesign.sk:" + 18892 + "/websocket/";
	var ekg_socket = new SocketWrapper( addrs );
	var global_socket_wrapper = ekg_socket;
	
    function showLiveEKG( id ) {
		window.location = "/components/ekg/ekg_view_v1.php?dev_id=" + id;
    	
    }
    
    function  RowEKGTable1( array, i ) {

	this.row = "";
	this.info = array[ i ];
	this.id = i;

	this.buildOperations = function ( id, name )
	{
		return  "<a tooltip=\"Show live data\" onclick=\"showLiveEKG( "+id+" )\" class=\"btn btn-link btn-info btn-icon btn-sm like\"><i class=\"tim-icons icon-wifi\"></i></a> \
				 <a tooltip=\"Show live data\" onclick=\"showArchiveEKG( "+id+" )\" class=\"btn btn-link btn-info btn-icon btn-sm like\"><i class=\"tim-icons icon-wifi\"></i></a>";
	}



	this.buildTime = function ( type )
	{

		if ( type == 0 )
			return  "<p><span></span></p>";
		else
			return  jsTimestampFull( type );
	}
	
	
	this.createRow = function ()
	{	
		if($("#" + 'BTS-table-' + this.info[ 0 ]).length == 0) {
			this.row = "<tr id='BTS-table-" + this.info[ 0 ] + "' >"
		      + "<td id='" + "_BTS_table_" + this.info[ 0 ] + "_id' editable='editable' >" + this.info[ 0 ] + "</td>"
		      + "<td id='" + "_BTS_table_" + this.info[ 0 ] + "_name' >" + ( this.info[ 1 ] ) + "</td>"
			  + "<td id='" + "_BTS_table_" + this.info[ 0 ] + "_time' >" + this.buildTime( this.info[ 7 ] ) + "</td>"
			  + "<td id='" + "_BTS_table_" + this.info[ 0 ] + "_core_id' editable='editable' >" + (this.info[ 5 ]) + "</td>"
			  + "<td id='" + "_BTS_table_" + this.info[ 0 ] + "_note'>" + ( this.info[ 2 ] ) + "</td>"
			  + "<td id='" + "_BTS_table_" + this.info[ 0 ] + "_reg_time'>" +  this.buildTime( this.info[ 5 ] )  +  "</td>"
			  + "<td id='" + "_BTS_table_" + this.info[ 0 ] + "_last_place' >" +( this.info[ 6 ] ) + "</td>"
			  + "<td id='" + "_BTS_table_" + this.info[ 0 ] + "_operation' >" + this.buildOperations( this.info[ 0 ], this.info[ 1 ] )  + "</td>"
			  + "</tr>";
			console.log( this.row ); 
		}else {


			$("#"  + "_BTS_table_" + this.info[ 0 ] + "_id").empty();
			$("#"  + "_BTS_table_" + this.info[ 0 ] + "_name").empty();
			$("#"  + "_BTS_table_" + this.info[ 0 ] + "_time").empty();
			$("#"  + "_BTS_table_" + this.info[ 0 ] + "_core_id").empty();
			$("#"  + "_BTS_table_" + this.info[ 0 ] + "_note").empty();
			$("#"  + "_BTS_table_" + this.info[ 0 ] + "_reg_time").empty();
			$("#"  + "_BTS_table_" + this.info[ 0 ] + "_last_place").empty();

			$("#"  + "_BTS_table_" + this.info[ 0 ] + "_id").html( this.info[ 0 ] );
			$("#"  + "_BTS_table_" + this.info[ 0 ] + "_name").html( this.info[ 1 ] );
			$("#"  + "_BTS_table_" + this.info[ 0 ] + "_time").html( this.buildTime ( this.info[ 7 ] ) );
			$("#"  + "_BTS_table_" + this.info[ 0 ] + "_core_id").html( this.info[ 5 ] );
			$("#"  + "_BTS_table_" + this.info[ 0 ] + "_note").html( this.info[ 2 ] );
			$("#"  + "_BTS_table_" + this.info[ 0 ] + "_reg_time").html( this.buildTime( this.info[ 4 ] ) );
			$("#"  + "_BTS_table_" + this.info[ 0 ] + "_last_place").html( this.info[ 6 ] );

		}



	}



	//this.createInsertRow()

	this.createRow();


}

    
    
    function drawChart( name, interval_start, interval_length ) {
		return;
		  $( "#example3.1" ).empty();
		  var datas = [];

		  datas.append( ['Activity', 'Start Time', 'End Time'] );

		  for( var x = 0; x <arr.length; x++ ) {
		      
		        var l = [name,
		         			new Date(interval_start),
		         			new Date(interval_start + interval_length)];
			    datas.append( l );
		  }

	    }

        function saveDone() {
        	showInfoMsg( "Data saved", 2);
        	        	
        }
	
		function sendData( id ) {
			var dats = $("#textbox-" + id).val();
			console.log( dats );
			var timestamp = Date.now()/1000;
			createNewCommands( "task", { "task_name": "GetData", "params": {"query": "INSERT INTO public.\"TestCoin\"( \"val\", \"timestamp\", \"type\", coin_id  ) VALUES( '" + dats + "', "+ timestamp +", 0, " + id + " )" } }, saveDone );
			showInfoMsg( "Saving data...", 2);
        	 
		}
	
		function createGraphTable( data ) {
			var dataset = [data];

			var options = {
					id_div_container: "visavail_container",
					id_div_graph: "visavail_graph",
					
					icon: {
						class_has_data: 'fas fa-fw fa-check',
						class_has_no_data: 'fas fa-fw fa-exclamation-circle'
					},
					zoom:{
						enabled:true,
					},
					responsive:{
						enabled:true,
					},
					tooltip: {
						date_plus_time: true
					},
					date_in_utc: true,
				};
				
			var chart = visavail.generate(options, dataset)
		}
		
		function liveUpdate() {

			this.handleLive = function( data ) {
				
				pushNewRow( data, RowMagCradleMain );
			}

		}
		function liveUpdate1() {

			this.handleLive = function( data ) {
				console.log( data );
			}

		}
		var datasource = 0;
		var data_table = 0;
		var global_tables = {};
		var id_settings_table = {};
		
		function createNewTableDataSet( data ) {
			var d =	( data );
			console.log( d ); 
			global_tables[ "global_main_table" ].processData( d );

			try{
				$("#loaderf").hide();
			}catch(err){
				
			}
			load.clear();
		}



		function updateCoinTable() {
			createNewCommands( "task", { "task_name": "GetData", "params": {"query": "SELECT DISTINCT(latest.\"ekgId\") as id, h.name as name, h.note, h.registered, h.place, h.\"coreId\", h.\"lastPlaceChange\", latest.last  \
				FROM ( \
						   SELECT \"ekgId\", MAX(time) \"last\" \
						   FROM public.\"MeasurementEKG\" \
						   GROUP BY \"ekgId\" \
						) as latest \
						INNER JOIN public.\"MeasurementEKG\" t on latest.\"ekgId\" = t.\"ekgId\" and latest.last = t.time \
						INNER JOIN public.\"Probes\" h on h.id = t.\"ekgId\" " } }, createNewTableDataSet ); 
						
		}
		function showArchiveEKG( id ) {
			window.location = "/components/ekg/ekg_archive.php?dev_id=" + id;
		}
		
				
		function start() {
			load.init();
			var ff = new liveUpdate();
			var ff1 = new liveUpdate1();

			global_socket_wrapper.init();
			
			var tables = new DataTable( 0, "datatable", global_socket_wrapper, false, RowEKGTable1, 0  );

			global_tables[ "global_main_table" ] = tables;


			tables.columns = [null , null, null, null, {"type": "num"}, null, null, null ];
			
			updateCoinTable();
			setInterval( updateCoinTable, 16000 );
		}
		
</script>		
</head>

<body class="sidebar-mini" onload="start()" >
  <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">
      <!-- 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">
        <div class="col-md-8 ml-auto mr-auto">
          <h4 class="text-center">Informácie o zariadení</h4>
          <p class="text-center">Podrobné informácie o stave a fungovaní zariadenia. Plná dokumentácia ku zariadeniu je na stránke 
            <a href="https://datatables.net/" target="_blank">dokumentácia.</a>
          </p>
        </div>
        <div class="row mt-5">
          <div class="col-md-12">
            <div class="card">
              <div class="card-body">
                <div class="toolbar">
                  <!--        Here you can write extra buttons/actions for the toolbar              -->
                </div>
                <h4 class="text-left">Tabuľka zariadení EKG</h4>
				<table id="datatable" class="table table-striped">
                  <thead>
                    <tr>
                      <th>ID</th>
                      <th>Name</th>
                      <th>Last communication</th>
                      <th>Core ID</th>
                      <th>Note</th>
                      <th>Registration_time</th>
                      <th>Loc</th>
                      <th class="sorting_desc_disabled sorting_asc_disabled text-right">Operation</th>
                    </tr>
                  </thead>
                  <tbody id="datatable_body">
                   
                  </tbody>
                  <tfoot>
                    <tr>
					  <th>ID</th>
                      <th>Name</th>
                      <th>Last communication</th>
                      <th>Core ID</th>
                      <th>Note</th>
                      <th>Registration time</th>
                      <th>Loc</th>
                      <th class="sorting_desc_disabled sorting_asc_disabled text-right">Operation</th>
                    </tr>
                  </tfoot>
                </table>
			
              </div>
              <!-- end content-->
            </div>
            <!--  end card  -->
          </div>
          <!-- end col-md-12 -->
        </div>
        <!-- end row -->
      </div>
      </div>
		
			<?php
    
        $path = $_SERVER['DOCUMENT_ROOT'];
        $path .= "/components/footer.php";
        include_once($path);
        
        ?>
	
    </div>
  </div>
  <!--   Core JS Files   -->
  		
			<?php
    
        $path = $_SERVER['DOCUMENT_ROOT'];
        $path .= "/components/code_footer.php";
        include_once($path);
        
        ?>
</body>

</html>
