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

<!DOCTYPE html>
<html lang="en">
<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta charset="utf-8">
        <link href="css/bootstrap.min.css" rel="stylesheet">
        <script src="js/bootstrap.min.js"></script>
        <style>
        html {
            font-family: Arial;
            display: inline-block;
            margin: 0px auto;
            text-align: center;
        }

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

        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;}

        @media screen and (max-width: 600px) {
            ul.topnav li.right, 
            ul.topnav li {float: none;}
        }
        
        img {
            display: block;
            margin-left: auto;
            margin-right: auto;
        }

        .table-container {
            width: 70%;
            margin: auto; /* agar tabel berada di tengah */
        }

        .table {
            width: 100%; /* tabel mengisi seluruh lebar container */
        }

        .top-controls {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }

        .search-container {
            display: flex;
            align-items: center;
        }

        .search-container input {
            margin-right: 10px;
            width: 80%; /* Set the width of the search input */
            padding: 10px; /* Add padding for better look */
            font-size: 16px;
        }
        .search-container a {
            width: 20%; /* Set the width of the button */
            padding: 10px; /* Add padding for better look */
            font-size: 16px; /* Increase font size */
            text-align: center; /* Center align the text */
        }
        </style>
        
        <!-- <title>Home : NodeMCU V3 ESP8266 / ESP12E with MYSQL Database</title> -->
    </head>
    
    <body>
        <!-- <h2>NodeMCU V3 ESP8266 / ESP12E with MYSQL Database</h2> -->
        <ul class="topnav">
            <li><a class="active" href="#">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 href="grafik.php">Grafik</a></li>
        </ul>
        <br>
        <h3></h3>
        
        <!-- <img src="gambar.png" alt="" style="width:55%;"> -->

        <?php
        require_once 'Database.php';

        // Koneksi ke database
        $pdo = Database::connect();

        // Ambil satu set data sensor terbaru dari tabel tb_sensor
        $sensorStmt = $pdo->query("SELECT * FROM tb_sensor ORDER BY tanggal DESC LIMIT 1");
        $sensor = $sensorStmt->fetch(PDO::FETCH_ASSOC);

        $dataSaved = false;

        // Cek apakah ada data sensor yang ditemukan
        if ($sensor && isset($_GET['id']) && isset($_GET['action']) && $_GET['action'] == 'save') {
            $id = $_GET['id'];

            // Ambil data pasien berdasarkan id
            $patientStmt = $pdo->prepare("SELECT * FROM table_nodemcu_rfidrc522_mysql WHERE id = ?");
            $patientStmt->execute([$id]);
            $patient = $patientStmt->fetch(PDO::FETCH_ASSOC);

            if ($patient) {
                // Tambahkan data baru ke tabel tb_gabungan
                $combinedData = [
                    'name' => $patient['name'],
                    'user_id' => $patient['id'],
                    'gender' => $patient['gender'],
                    'umur' => $patient['umur'],
                    'mobile' => $patient['mobile'],
                    'detakjantung' => $sensor['detakjantung'],
                    'oksigen' => $sensor['oksigen'],
                    'tanggal' => $sensor['tanggal'],
                    'status' => $sensor['status']
                ];

                $insertQuery = "
                    INSERT INTO tb_gabungan (name, user_id, gender, umur, mobile, detakjantung, oksigen, tanggal, status)
                    VALUES (:name, :user_id, :gender, :umur, :mobile, :detakjantung, :oksigen, :tanggal, :status)
                ";
                $stmt = $pdo->prepare($insertQuery);
                $stmt->execute($combinedData);

                $dataSaved = true;
            }
        }

        // Ambil semua data dari tabel tb_gabungan untuk ditampilkan
        $gabunganStmt = $pdo->query("SELECT * FROM tb_gabungan");
        $gabunganData = $gabunganStmt->fetchAll(PDO::FETCH_ASSOC);

        // Tutup koneksi ke database
        Database::disconnect();
        ?>

        <?php if ($dataSaved): ?>
        <script>
            alert("Data berhasil digabungkan.");
        </script>
        <?php endif; ?>

        <div class="container table-container">
            <div class="top-controls">
                <h2>Data Gabungan</h2>
                <div class="search-container">
                    <input type="text" id="searchInput" onkeyup="searchTable()" placeholder="Cari data...">
                    <a href="excel.php" class="btn btn-secondary">Export Excel</a>
                </div>
            </div>
            <table class="table table-striped table-bordered ">
                <thead style="background-color: #10a0c5; color: white;">
                    <tr>
                        <th>ID</th>
                        <th>Nama</th>
                        <th>User ID</th>
                        <th>Jenis Kelamin</th>
                        <th>umur</th>
                        <th>Nomor Telepon</th>
                        <th>Detak Jantung</th>
                        <th>Oksigen</th>
                        <th>Tanggal</th>
                        <th>status</th>
                        <th>aksi</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if ($gabunganData): ?>
                        <?php foreach ($gabunganData as $row): ?>
                            <tr>
                                <td><?php echo htmlspecialchars($row['id']); ?></td>
                                <td><?php echo htmlspecialchars($row['name']); ?></td>
                                <td><?php echo htmlspecialchars($row['user_id']); ?></td>
                                <td><?php echo htmlspecialchars($row['gender']); ?></td>
                                <td><?php echo htmlspecialchars($row['umur']); ?></td>
                                <td><?php echo htmlspecialchars($row['mobile']); ?></td>
                                <td><?php echo htmlspecialchars($row['detakjantung']); ?></td>
                                <td><?php echo htmlspecialchars($row['oksigen']); ?></td>
                                <td><?php echo htmlspecialchars($row['tanggal']); ?></td>
                                <td><?php echo htmlspecialchars($row['status']); ?></td>
                                <td>
                                    <!-- <a href="hapusgabungan.php?delete_id=<?php echo $row['id']; ?>" class="btn btn-danger">Hapus</a> -->
                                    <a href="riwayat.php?user_id=<?php echo htmlspecialchars($row['user_id']); ?>" class="btn btn-info">Riwayat</a>

                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="11" class="text-center">Tidak ada data.</td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
            <!-- <a href="grafik.php" class="btn btn-primary mt-3">Kembali ke Halaman Utama</a> -->
        </div>

        <script> //pencarian
            function searchTable() {
                // Mendapatkan nilai input pencarian
                var input = document.getElementById("searchInput");
                var filter = input.value.toUpperCase();
                
                // Mendapatkan baris-baris data dalam tabel
                var table = document.getElementsByTagName("table")[0];
                var rows = table.getElementsByTagName("tr");
                
                // Iterasi melalui setiap baris dan menyembunyikan yang tidak cocok
                for (var i = 0; i < rows.length; i++) {
                    var cells = rows[i].getElementsByTagName("td");
                    var found = false;
                    for (var j = 0; j < cells.length; j++) {
                        var cell = cells[j];
                        if (cell) {
                            if (cell.innerHTML.toUpperCase().indexOf(filter) > -1) {
                                found = true;
                                break;
                            }
                        }
                    }
                    if (found) {
                        rows[i].style.display = "";
                    } else {
                        rows[i].style.display = "none";
                    }
                }
            }
        </script>

    </body>
</html>
