<?php
session_start();
if(!isset($_SESSION['user']) || empty($_SESSION['user'])) {
    header("location: login.php"); exit();
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="expires" content="0">
	<title>Cardinal 825 - Programmable Digital Weight Indicator</title>
	<link href="main.css" rel="stylesheet" type="text/css">
	<link href="filemgr.css" rel="stylesheet" type="text/css">
    <link href="cardinal.css" rel="stylesheet" type="text/css">

    <meta name="viewport" content="width=device-width">
    <script src="jquery-3.6.1.min.js" crossorigin="anonymous"></script>
    <script src="bootstrap.bundle.min.js" crossorigin="anonymous"></script>

</head>
    

<style>
        html { -moz-osx-font-smoothing: grayscale; -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility; height: 100%; scroll-behavior: smooth;}
        *,*::before,*::after { box-sizing: border-box;}
        body { margin: 20px; font-size:15px; color:#222;background:#FFFFFF; }
        body.navbar-fixed { margin-top:55px; }
        a, a:hover, a:visited, a:focus { text-decoration:none !important; }
        .filename, td, th { white-space:nowrap  }
        .navbar-brand { font-weight:bold; }
        .nav-item.avatar a { cursor:pointer;text-transform:capitalize; }
        .nav-item.avatar a > i { font-size:15px; }
        .nav-item.avatar .dropdown-menu a { font-size:13px; }
        #search-addon { font-size:12px;border-right-width:0; }
        .brl-0 { background:transparent;border-left:0; border-top-left-radius: 0; border-bottom-left-radius: 0; }
        .brr-0 { border-top-right-radius: 0; border-bottom-right-radius: 0; }
        .bread-crumb { color:#cccccc;font-style:normal; }
        #main-table { transition: transform .25s cubic-bezier(0.4, 0.5, 0, 1),width 0s .25s;}
        #main-table .filename a { color:#222222; }
        .table td { vertical-align:middle!important; }
        .table th { background-color: lightcoral;}
        .table tr:nth-child(odd) { background-color: mistyrose; }
        .table .custom-checkbox-td .custom-control.custom-checkbox, .table .custom-checkbox-header .custom-control.custom-checkbox { }
        .table-sm td, .table-sm th { padding:0.4rem; }
        .table-bordered td, .table-bordered th { border:1px solid #000000; }  /*Middle lines of table */
        .hidden { display:none  }
        pre.with-hljs { padding:0; overflow: hidden;  }
        pre.with-hljs code { margin:0;border:0;overflow:scroll;  }
        code.maxheight, pre.maxheight { max-height:512px  }
        .fa.fa-caret-right { font-size:1.2em;margin:0 4px;vertical-align:middle;color:#ececec  }
        .fa.fa-home { font-size:1.3em;vertical-align:bottom  }
        .path { margin-bottom:10px  }
        form.dropzone { min-height:200px;border:2px dashed #007bff;line-height:6rem; }
        .right { text-align:right  }
        .center, .close, .login-form, .preview-img-container { text-align:center  }
        .message { padding:4px 7px;border:1px solid #ddd;background-color:#fff  }
        .message.ok { border-color:green;color:green  }
        .message.error { border-color:red;color:red  }
        .message.alert { border-color:orange;color:orange  }
        .preview-img { max-width:100%;max-height:80vh;background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAAKklEQVR42mL5//8/Azbw+PFjrOJMDCSCUQ3EABZc4S0rKzsaSvTTABBgAMyfCMsY4B9iAAAAAElFTkSuQmCC) }
        .inline-actions > a > i { font-size:1em;margin-left:5px;background:#3785c1;color:#fff;padding:3px 4px;border-radius:3px; }
        .preview-video { position:relative;max-width:100%;height:0;padding-bottom:62.5%;margin-bottom:10px  }
        .preview-video video { position:absolute;width:100%;height:100%;left:0;top:0;background:#000  }
        .compact-table { border:0;width:auto  }
        .compact-table td, .compact-table th { width:100px;border:0;text-align:center  }
        .compact-table tr:hover td { background-color:#fff  }
        .filename { max-width:420px;overflow:hidden;text-overflow:ellipsis  }
        .break-word { word-wrap:break-word;margin-left:30px  }
        .break-word.float-left a { color:#7d7d7d  }
        .break-word + .float-right { padding-right:30px;position:relative  }
        .break-word + .float-right > a { color:#7d7d7d;font-size:1.2em;margin-right:4px  }
        #editor { position:absolute;right:15px;top:100px;bottom:15px;left:15px  }
        @media (max-width:481px) {
            #editor { top:150px; }
        }
        #normal-editor { border-radius:3px;border-width:2px;padding:10px;outline:none; }
        .btn-2 { padding:4px 10px;font-size:small; }
        li.file:before,li.folder:before { font:normal normal normal 14px/1 FontAwesome;content:"\f016";margin-right:5px }
        li.folder:before { content:"\f114" }
        i.fa.fa-folder-o { color:#0157b3 }
        i.fa.fa-picture-o { color:#26b99a }
        i.fa.fa-file-archive-o { color:#da7d7d }
        .btn-2 i.fa.fa-file-archive-o { color:inherit }
        i.fa.fa-css3 { color:#f36fa0 }
        i.fa.fa-file-code-o { color:#007bff }
        i.fa.fa-code { color:#cc4b4c }
        i.fa.fa-file-text-o { color:#0096e6 }
        i.fa.fa-html5 { color:#d75e72 }
        i.fa.fa-file-excel-o { color:#09c55d }
        i.fa.fa-file-powerpoint-o { color:#f6712e }
        i.go-back { font-size:1.2em;color:#007bff; }
        .main-nav { padding:0.2rem 1rem;box-shadow:0 4px 5px 0 rgba(0, 0, 0, .14), 0 1px 10px 0 rgba(0, 0, 0, .12), 0 2px 4px -1px rgba(0, 0, 0, .2)  }
        .dataTables_filter { display:none; }
        table.dataTable thead .sorting { cursor:pointer;background-repeat:no-repeat;background-position:center right;}
        table.dataTable thead .sorting_asc { cursor:pointer;background-repeat:no-repeat;background-position:center right;}
        table.dataTable thead .sorting_desc { cursor:pointer;background-repeat:no-repeat;background-position:center right;}
        table.dataTable thead tr:first-child th.custom-checkbox-header:first-child { background-image:none; }
        .footer-action li { margin-bottom:10px; }
        .app-v-title { font-size:24px;font-weight:300;letter-spacing:-.5px;text-transform:uppercase; }
        hr.custom-hr { border-top:1px dashed #8c8b8b;border-bottom:1px dashed #fff; }
        #snackbar { visibility:hidden;min-width:250px;margin-left:-125px;background-color:#333;color:#fff;text-align:center;border-radius:2px;padding:16px;position:fixed;z-index:1;left:50%;bottom:30px;font-size:17px; }
        #snackbar.show { visibility:visible;-webkit-animation:fadein 0.5s, fadeout 0.5s 2.5s;animation:fadein 0.5s, fadeout 0.5s 2.5s; }
        @-webkit-keyframes fadein { from { bottom:0;opacity:0; }
        to { bottom:30px;opacity:1; }
        }
        @keyframes fadein { from { bottom:0;opacity:0; }
        to { bottom:30px;opacity:1; }
        }
        @-webkit-keyframes fadeout { from { bottom:30px;opacity:1; }
        to { bottom:0;opacity:0; }
        }
        @keyframes fadeout { from { bottom:30px;opacity:1; }
        to { bottom:0;opacity:0; }
        }
        #main-table span.badge { border-bottom:2px solid #f8f9fa }
        #main-table span.badge:nth-child(1) { border-color:#df4227 }
        #main-table span.badge:nth-child(2) { border-color:#f8b600 }
        #main-table span.badge:nth-child(3) { border-color:#00bd60 }
        #main-table span.badge:nth-child(4) { border-color:#4581ff }
        #main-table span.badge:nth-child(5) { border-color:#ac68fc }
        #main-table span.badge:nth-child(6) { border-color:#45c3d2 }
        @media only screen and (min-device-width:768px) and (max-device-width:1024px) and (orientation:landscape) and (-webkit-min-device-pixel-ratio:2) { .navbar-collapse .col-xs-6 { padding:0; }
        }
        .btn.active.focus,.btn.active:focus,.btn.focus,.btn.focus:active,.btn:active:focus,.btn:focus { outline:0!important;outline-offset:0!important;background-image:none!important;-webkit-box-shadow:none!important;box-shadow:none!important }
        .lds-facebook { display:none;position:relative;width:64px;height:64px }
        .lds-facebook div,.lds-facebook.show-me { display:inline-block }
        .lds-facebook div { position:absolute;left:6px;width:13px;background:#007bff;animation:lds-facebook 1.2s cubic-bezier(0,.5,.5,1) infinite }
        .lds-facebook div:nth-child(1) { left:6px;animation-delay:-.24s }
        .lds-facebook div:nth-child(2) { left:26px;animation-delay:-.12s }
        .lds-facebook div:nth-child(3) { left:45px;animation-delay:0s }
        @keyframes lds-facebook { 0% { top:6px;height:51px }
        100%,50% { top:19px;height:26px }
        }
        ul#search-wrapper { padding-left: 0;border: 1px solid #ecececcc; } ul#search-wrapper li { list-style: none; padding: 5px;border-bottom: 1px solid #ecececcc; }
        ul#search-wrapper li:nth-child(odd){ background: #f9f9f9cc;}
        .c-preview-img { max-width: 300px; }
        .border-radius-0 { border-radius: 0; }
        .float-right { float: right; }
        .table-hover>tbody>tr:hover>td:first-child { border-left: 1px solid #1b77fd; }
        #main-table tr.even { background-color: #F8F9Fa; }
        .filename>a>i {margin-right: 3px;}

        #Group1 tr.even { background-color: red; }
    </style>

<body>

<div class="container">
    <div class="row">
        <div class="col-sm-4" id ="main_tab">
            <img src="Cardinal_logo.png" width="60%" background-size="100%">
        </div>

        <br><br><br>
		<div class="col-sm-8" id ="main_tab">
				<ul class="nav nav-pills ">
					<li class="nav-item bg-light">
                        <a href="index.php" class="nav-link"
						data-toggle="pill" data-target="#tabone">home</a>
                    </li>
					<li class="nav-item bg-light"><a href="tinyfilemanager.php" class="nav-link"
						data-toggle="pill" data-target="#tabtwo">files</a>
                    </li>
					<li class="nav-item bg-light"><a href="diagnostics.php" class="nav-link active"
                        hrefdiagnostics.php" data-target="#tabthree" data-toggle="pill">diagnostics</a>
                    </li>
                    <li class="nav-item bg-light"><a class="nav-link"
                        href = "admin.php" data-target="#tabfour" data-toggle="pill">administration</a>
                    </li>
                </ul>
		</div>
    </div>
</div>

<div class="container">
    <div class="row">
        <div class= "img">
            <img src="825_Banner.jpg" style="max-width: 110%; height:130%">
            <prim2 >Core Temperature Log</prim2>
            <second >Temperature records are shown below.</second>
        </div>
    </div>
</div>

<div class="container">
    <div class="row mt-5">

    <?php
        $tzpath = realpath("/etc/localtime");
        if(substr($tzpath, 0, 20) == '/usr/share/zoneinfo/') {
            $tz = substr($tzpath, 20);
            date_default_timezone_set($tz);
        }

        $from = date("Y-m-d", strtotime("-3 day"));
        $to = date("Y-m-d");
        if(isset($_GET['dfrom']))  {
            $from = $_GET['dfrom'];
        }
        if(isset($_GET['dto'])) {
            $to = $_GET['dto'];
        }
        //$curdate =  date("Y-m-d   H:i:s T Y");
        //echo "Date: " . $curdate;
        echo '<form action="" method="get">';
        echo '<table>';
        echo '<tr><td>Date From: </td><td><input type="date" value="'. $from . '" class="form-control" name="dfrom" id="dfrom"></td><td></td>';
        echo '<td>To: </td><td><input type="date" value="' . $to . '" class="form-control" name="dto" id="dto"></td><td><button type="submit">Update</button></td></tr>';
   		echo '</table>';
		echo '</form>';
    ?>
    </div>
</div>

<div class="container">
    <div class="row mt-5">
    <?php

    $tzpath = realpath("/etc/localtime");
    if(substr($tzpath, 0, 20) == '/usr/share/zoneinfo/') {
        $tz = substr($tzpath, 20);
        date_default_timezone_set($tz);
    }

    foreach (glob('/usr/share/825/log/t????-??-??.csv') as $filename) {
        $datestr = substr($filename, 20, 10);
        if($datestr >= $from && $datestr <= $to) {

            echo '<table class="table table-bordered table-hover table-sm" id="main-table">';
            echo '<tr><th>' . $datestr . '</th><th>Temperature °C</th></tr>';

            $fh = fopen($filename, 'r');
            if($fh !== false) {
                while (($line = fgets($fh)) !== false) {
                    $items = explode(',', $line);
                    $hour = (int)($items[0] / 100);
                    $minute = $items[0] - ($hour * 100);
                    echo '<tr><td>' . sprintf('%02d:%02d', $hour, $minute) . '</td><td>' . $items[1] . '</td></tr>';
                }
                fclose($fh);
            } else {
                echo '<tr><td>Error opening file</td><td>' . $filename . '</td></tr>';
            }
            //echo '<tr><td>' . '**' . '</td><td>' . $filename . '</td></tr>';
            echo '</table>';

        }
    }

    ?>
    </div>
</div>

</body>
</html>
