<?php
require_once 'Database.php';

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

// Ambil nama pasien dari parameter URL
$patientName = isset($_GET['name']) ? $_GET['name'] : '';

// Ambil data pasien berdasarkan nama
$gabunganStmt = $pdo->prepare("SELECT * FROM tb_gabungan WHERE name = ?");
$gabunganStmt->execute([$patientName]);
$gabunganData = $gabunganStmt->fetchAll(PDO::FETCH_ASSOC);

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

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

        .table-container {
            width: 70%;
            margin: auto;
        }

        .table {
            width: 100%;
        }
    </style>
</head>
<body>
    <div class="container table-container">
        <h2>Riwayat Pasien: <?php echo htmlspecialchars($patientName); ?></h2>
        <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>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="11" class="text-center">Tidak ada data.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
        <a href="rekapdata.php" class="btn btn-primary mt-3">Kembali ke Halaman Utama</a>
    </div>

    <!-- Grafik Detak Jantung dan Oksigen -->
    <div class= "panel panel-primary">
        <div class="container mt-5">
            <canvas id="myChart"></canvas>
        </div>
        <div class = "panel-body">

            <script>
            var ctx = document.getElementById('myChart').getContext('2d');
            var myChart = new Chart(ctx, {
                type: 'line',
                data: {
                    labels: [
                        <?php foreach ($gabunganData as $row): ?>
                            '<?php echo htmlspecialchars($row['tanggal']); ?>',
                        <?php endforeach; ?>
                    ],
                    datasets: [{
                        label: 'Detak Jantung',
                        backgroundColor: 'rgba(30, 129, 176, 0.5)',
                        borderColor: 'rgb(30, 129, 176)',
                        borderWidth: 1,
                        lineTension: 0.4, // Sesuaikan dengan kebutuhan
                        pointRadius: 5, // Ukuran titik
                        data: [
                            <?php foreach ($gabunganData as $row): ?>
                                <?php echo htmlspecialchars($row['detakjantung']); ?>,
                            <?php endforeach; ?>
                        ]
                    }, {
                        label: 'Oksigen',
                        backgroundColor: 'rgba(125, 168, 89, 0.5)',
                        borderColor: 'rgb(125, 168, 89)',
                        borderWidth: 1,
                        lineTension: 0.4, // Sesuaikan dengan kebutuhan
                        pointRadius: 5, // Ukuran titik
                        data: [
                            <?php foreach ($gabunganData as $row): ?>
                                <?php echo htmlspecialchars($row['oksigen']); ?>,
                            <?php endforeach; ?>
                        ]
                    }]
                },
                options: {
                    scales: {
                        y: {
                            beginAtZero: true
                        }
                    },
                    responsive: true,
                    maintainAspectRatio: true
                }
            });
            </script>
        </div>
    </div>
</body>
</html>
