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

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

// Samples Collected but not yet Completed
$sql = "SELECT t.order_test_id, t.started_at as collected_at, 
               tt.test_name, p.patient_number, u.first_name, u.last_name
        FROM lab_order_tests t
        JOIN lab_orders o ON t.order_id = o.order_id
        JOIN patients p ON o.patient_id = p.patient_id
        JOIN users u ON p.user_id = u.user_id
        LEFT JOIN test_types tt ON t.test_type_id = tt.test_type_id
        WHERE t.status = 'SAMPLE_COLLECTED'
        ORDER BY t.started_at ASC";
$res = $conn->query($sql);
?>

<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Sample Tracking')); ?>
    <?php include 'layouts/head-css.php'; ?>
    <link href="assets/libs/datatables.net-bs5/css/dataTables.bootstrap5.min.css" rel="stylesheet" />
</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">
                            <h4 class="mb-sm-0">Sample Tracking</h4>
                        </div>
                    </div>
                    <div class="card">
                        <div class="card-body">
                            <table id="sampleTable" class="table table-bordered dt-responsive nowrap">
                                <thead>
                                    <tr>
                                        <th>Patient</th>
                                        <th>Test</th>
                                        <th>Collected At</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php while ($row = $res->fetch_assoc()): ?>
                                        <tr>
                                            <td><?php echo htmlspecialchars($row['first_name'] . ' ' . $row['last_name']); ?>
                                            </td>
                                            <td><?php echo htmlspecialchars($row['test_name']); ?></td>
                                            <td><?php echo $row['collected_at']; ?></td>
                                            <td>
                                                <a href="test_results_entry.php?test_id=<?php echo $row['order_test_id']; ?>"
                                                    class="btn btn-sm btn-info">Process Sample</a>
                                            </td>
                                        </tr>
                                    <?php endwhile; ?>
                                </tbody>
                            </table>
                        </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>$('#sampleTable').DataTable();</script>
</body>

</html>