<?php
session_start();
include 'layouts/dbconn.php';
include 'layouts/session.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

$year = $_GET['year'] ?? date('Y');

// Assume lab_orders exists as noted in medical_history.php logic
// Fetch "Completed" lab orders
// Using dummy query structure if lab_orders not fully known, but following standard pattern
$orders = [];
try {
    $sql = "SELECT lo.*, u.full_name 
            FROM lab_orders lo
            JOIN patients p ON lo.patient_id = p.patient_id
            JOIN users u ON p.user_id = u.user_id
            WHERE YEAR(lo.order_date) = ? AND lo.status = 'COMPLETED'
            ORDER BY lo.order_date DESC";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("s", $year);
    $stmt->execute();
    $res = $stmt->get_result();
    while ($row = $res->fetch_assoc())
        $orders[] = $row;
} catch (Exception $e) {
    // Falls back to empty if table missing
}
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Test Results Archive')); ?>
    <link href="assets/libs/datatables.net-bs5/css/dataTables.bootstrap5.min.css" rel="stylesheet" />
    <?php include 'layouts/head-css.php'; ?>
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-12">
                            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                                <h4 class="mb-sm-0">Test Results Archive</h4>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-body">
                                    <form method="GET" class="mb-4 d-flex align-items-center gap-2">
                                        <label>Filter Year:</label>
                                        <select name="year" class="form-select" style="width: 150px;"
                                            onchange="this.form.submit()">
                                            <?php for ($i = date('Y'); $i >= 2020; $i--): ?>
                                                <option value="<?php echo $i; ?>" <?php echo $year == $i ? 'selected' : ''; ?>>
                                                    <?php echo $i; ?></option>
                                            <?php endfor; ?>
                                        </select>
                                    </form>

                                    <?php if (count($orders) > 0): ?>
                                        <table id="archiveTable" class="table table-bordered dt-responsive nowrap">
                                            <thead>
                                                <tr>
                                                    <th>Date</th>
                                                    <th>Patient</th>
                                                    <th>Test Name</th>
                                                    <th>Result Summary</th>
                                                    <th>Action</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php foreach ($orders as $order): ?>
                                                    <tr>
                                                        <td><?php echo date('Y-m-d', strtotime($order['order_date'])); ?></td>
                                                        <td><?php echo htmlspecialchars($order['full_name']); ?></td>
                                                        <td><?php echo htmlspecialchars($order['test_name']); ?></td>
                                                        <td>Completed</td>
                                                        <td>
                                                            <a href="test_results.php?id=<?php echo $order['order_id']; ?>"
                                                                class="btn btn-sm btn-info">View</a>
                                                        </td>
                                                    </tr>
                                                <?php endforeach; ?>
                                            </tbody>
                                        </table>
                                    <?php else: ?>
                                        <div class="alert alert-info">No archived test results found for
                                            <?php echo $year; ?>.</div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
    <script src="assets/libs/datatables.net/js/jquery.dataTables.min.js"></script>
    <script src="assets/libs/datatables.net-bs5/js/dataTables.bootstrap5.min.js"></script>
    <script>$('#archiveTable').DataTable();</script>
</body>

</html>