<?php
    require_once 'Database.php';
    $Write="<?php $" . "UIDresult=''; " . "echo $" . "UIDresult;" . " ?>";
    file_put_contents('UIDContainer.php',$Write);
?>

<!DOCTYPE html>
<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.0">
    <title>Grafik Sensor</title>

    <link rel="stylesheet" href="assets/css/bootstrap.min.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="assets/js/mdb.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.9.2/html2pdf.bundle.min.js"></script>

    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        
        .fixed-width-container {
            width: 1000px;
            margin: 0 auto;
        }

        textarea {
            resize: none;
        }

        ul.topnav {
            list-style-type: none;
            margin: 20px auto;
            padding: 0;
            overflow: hidden;
            background-color: #4CAF50;
            width: 1000px;
        }

        ul.topnav li {float: left;}

        ul.topnav li a {
            display: block;
            color: white;
            text-align: center;
            padding: 14px 16px;
            text-decoration: none;
        }

        ul.topnav li a:hover:not(.active) {background-color: #3e8e41;}

        ul.topnav li a.active {background-color: #333;}

        ul.topnav li.right {float: right;}

        .table-container {
            width: 1000px;
            overflow-x: auto;
        }

        .card-container {
            display: flex;
            justify-content: space-between;
            margin-top: 20px;
            width: 1000px;
        }

        .card {
            width: 48%;
        }

        .button-container {
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <ul class="topnav">
        <li><a href="rekapdata.php">Rekap Data</a></li>
        <li><a href="user data.php">Data Pasien</a></li>
        <li><a href="homepage.php">Registrasi</a></li>
        <li><a href="read tag.php">Baca Tag ID</a></li>
        <li><a class="active" href="#">Grafik</a></li>
    </ul>

    <div class="fixed-width-container">
        <div style="text-align: center; margin-top: 20px;">
            <?php
            if (isset($_GET['id'])) {
                $id = $_GET['id'];
                $pdo = Database::connect();
                $stmt = $pdo->prepare("SELECT * FROM table_nodemcu_rfidrc522_mysql WHERE id = ?");
                $stmt->execute([$id]);
                $row = $stmt->fetch(PDO::FETCH_ASSOC);

                if ($row) {
                    echo '<h2>Data Pasien</h2>';
                    echo '<div class="table-container">';
                    echo '<table class="table table-striped table-bordered">';
                    echo '<tr style="background-color: #10a0c5; color: #FFFFFF;">';
                    echo '<th>Nama</th><th>ID</th><th>Jenis Kelamin</th><th>Umur</th><th>Nomor Telepon</th><th>Aksi</th>';
                    echo '</tr>';
                    echo '<tr>';
                    echo '<td>' . htmlspecialchars($row['name']) . '</td>';
                    echo '<td>' . htmlspecialchars($row['id']) . '</td>';
                    echo '<td>' . htmlspecialchars($row['gender']) . '</td>';
                    echo '<td>' . htmlspecialchars($row['umur']) . '</td>';
                    echo '<td>' . htmlspecialchars($row['mobile']) . '</td>';
                    echo '<td><a href="rekapdata.php?id=' . $row['id'] . '&action=save" class="btn btn-primary">Simpan</a></td>';
                    echo '</tr>';
                    echo '</table>';
                    echo '</div>';
                } else {
                    echo '<p>Data tidak ditemukan.</p>';
                }
                Database::disconnect();
            } else {
                echo '<p>ID pasien tidak diberikan.</p>';
            }
            ?>
        </div>

        <div class="button-container" style="text-align: center;">
            <button id="startButton" class="btn btn-primary">Mulai</button>
            <button id="stopButton" class="btn btn-danger" style="display: none;">Berhenti</button>
        </div>
        
        <div class="card-container">
            <div class="card text-center">
                <div class="card-header" style="font-size: 30px; font-weight: bold; background-color: rgb(6,57,112); color:white;">
                Detak Jantung
                </div>
                <div class="card-body">
                    <h1><span id="cekdetakjantung">0</span></h1>
                </div>
            </div>

            <div class="card text-center">
                <div class="card-header" style="font-size: 30px; font-weight: bold; background-color: rgb(17, 83, 30, 1); color:white;">
                    Oksigen/Spo2
                </div>
                <div class="card-body">
                    <h1><span id="cekoksigen">0</span></h1>
                </div>
            </div>
        </div>   

        <div class="card-footer" style="margin-top: 20px; border: 2px solid white; padding: 10px; text-align: center;">
            <span id="cekstatus" style="font-size: 20px; font-weight: bold;">-</span>
        </div>

        <div style="text-align: center;">
            <h3>Monitoring Grafik </h3>
            <p>data yang ditampilkan adalah 5 data terahir</p>
        </div>

        <div id="responscontainer" style="width: 100%; text-align: center;"></div>

        <div style="text-align: center; margin-top: 20px;">
            <button onclick="window.print()" class="btn btn-primary">Cetak</button>
            <button onclick="downloadPDF()" class="btn btn-secondary">PDF</button>
        </div>
    </div>

    <script>
        var refreshInterval;

        function startLoading() {
            $('#startButton').hide();
            $('#stopButton').show();
            refreshInterval = setInterval(function(){
                $('#responscontainer').load('data.php');
                $("#cekdetakjantung").load("cekdetakjantung.php");
                $("#cekoksigen").load("oksigen.php");
                $("#cekstatus").load('cekstatus2.php');
            }, 1000);
        }

        function stopLoading() {
            $('#stopButton').hide();
            $('#startButton').show();
            clearInterval(refreshInterval);
        }

        $(document).ready(function(){
            $('#startButton').click(startLoading);
            $('#stopButton').click(stopLoading);
        });

        function downloadPDF() {
            const element = document.body;
            const opt = {
                margin:       0.5,
                filename:     'data_pasien.pdf',
                image:        { type: 'jpeg', quality: 0.98 },
                html2canvas:  { scale: 2 },
                jsPDF:        { unit: 'px', format: [1600, 1208], orientation: 'portrait' }
            };
            html2pdf().set(opt).from(element).save();
        }
    </script>
</body>
</html>