<?php
include 'db.php';

if (isset($_GET['nik'])) {
    $nik = $conn->real_escape_string($_GET['nik']);
    $query = "SELECT * FROM pasien WHERE nik = '$nik'";
    $result = $conn->query($query);
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Hasil Pemeriksaan</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <style>
        body { background-color: #f4f9f4; }
        .card { border-radius: 1rem; margin-top: 30px; }
        .card-header { background-color: #28a745; color: white; }
        .preview-img { max-width: 100%; height: auto; border: 1px solid #ddd; border-radius: 4px; padding: 5px; }
        .info-table td { padding: 6px 12px; }
    </style>
</head>
<body>
<div class="container">
    <div class="card shadow">
        <div class="card-header text-center">
            <h4>Hasil Pemeriksaan HPV DNA</h4>
        </div>
        <div class="card-body text-center">
            <?php if (isset($result) && $result->num_rows > 0): ?>
                <?php $data = $result->fetch_assoc(); ?>
                <table class="table table-bordered w-50 mx-auto mb-4 info-table">
                    <tr>
                        <th class="text-left">Nama</th>
                        <th class="text-left"><?= htmlspecialchars($data['nama']) ?> </th>
                    </tr>
                    <tr>
                        <th class="text-left">NIK</th>
                        <th class="text-left"><?= htmlspecialchars($data['nik']) ?></th>
                    </tr>
                </table>
                <?php
                    $file = basename($data['hasil_lab']);
                    $file_url = "https://cekhpvdna.pkmpulogadung.com/petugas/uploads/" . rawurlencode($file);
                    $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));

                    if (in_array($ext, ['jpg', 'jpeg', 'png'])) {
                        echo "<img src='$file_url' class='preview-img'>";
                    } elseif ($ext === 'pdf') {
                        echo "<iframe src='$file_url' width='100%' height='600px' class='border rounded'></iframe>";
                    } else {
                        echo "<a href='$file_url' target='_blank'>Lihat File</a>";
                    }

                    echo "<div class='mt-3'><a href='$file_url' download class='btn btn-success'>Download</a></div>";
                ?>
            <?php else: ?>
                <div class="alert alert-warning text-center">
                    Data tidak ditemukan untuk NIK <strong><?= htmlspecialchars($_GET['nik']) ?></strong>.
                </div>
            <?php endif; ?>
        </div>
        <div class="card-footer text-center">
            <a href="index.php" class="btn btn-secondary">Kembali</a>
        </div>
    </div>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>