<?php
session_start();
if (!isset($_SESSION['patient_id'])) {
    header("Location: ../login.php");
    exit();
}
include '../../include/config.php';

$user_id = $_SESSION['patient_id'];

$lab_results = $conn->query("
    SELECT * FROM lab_results 
    WHERE patient_id = '$user_id' 
    ORDER BY test_date DESC
");

$page_title = 'Lab Results | UHD';
include '../../include/header.php';
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Lab Results</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css">
</head>
<body>
<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2 class="text-white"><i class="fas fa-flask me-2"></i> Lab Results</h2>
        <a href="index.php" class="btn btn-outline-light rounded-pill">← Back</a>
    </div>

    <?php if ($lab_results && $lab_results->num_rows > 0): ?>
        <div class="row">
            <?php while($l = $lab_results->fetch_assoc()): ?>
                <div class="col-md-6 mb-3">
                    <div class="bg-white rounded-4 p-3">
                        <div class="d-flex justify-content-between">
                            <h6><?= htmlspecialchars($l['test_name']) ?></h6>
                            <span class="badge bg-<?= $l['status'] == 'normal' ? 'success' : ($l['status'] == 'abnormal' ? 'warning' : 'danger') ?>">
                                <?= ucfirst($l['status']) ?>
                            </span>
                        </div>
                        <p class="small text-muted mb-1">Date: <?= date('M d, Y', strtotime($l['test_date'])) ?></p>
                        <p class="small text-muted">Result: <?= htmlspecialchars($l['result']) ?></p>
                        <?php if($l['normal_range']): ?>
                            <p class="small text-muted mb-0">Normal Range: <?= htmlspecialchars($l['normal_range']) ?></p>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endwhile; ?>
        </div>
    <?php else: ?>
        <div class="alert alert-info text-center py-5">
            <i class="fas fa-microscope fa-3x mb-3"></i>
            <h5>No lab results yet</h5>
            <p>Your lab results will appear here</p>
        </div>
    <?php endif; ?>
</div>
<?php include '../../include/footer.php'; ?>