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

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

$sql = "SELECT t.order_test_id, t.order_id, t.test_type_id, t.status, t.completed_at, t.verified_by,
               o.order_number, p.patient_number, u.first_name, u.last_name,
               tt.test_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 = 'COMPLETED'
        ORDER BY t.completed_at DESC LIMIT 100";

$result = $conn->query($sql);
?>

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

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Complete Tests')); ?>
    <?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">Complete Tests</h4>
                        </div>
                    </div>

                    <div class="card">
                        <div class="card-body">
                            <table id="completeTable" class="table table-bordered dt-responsive nowrap">
                                <thead>
                                    <tr>
                                        <th>Order #</th>
                                        <th>Patient</th>
                                        <th>Test Name</th>
                                        <th>Completed At</th>
                                        <th>Verified</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php while ($row = $result->fetch_assoc()): ?>
                                        <tr>
                                            <td><?php echo htmlspecialchars($row['order_number']); ?></td>
                                            <td><?php echo htmlspecialchars($row['first_name'] . ' ' . $row['last_name']); ?>
                                            </td>
                                            <td><?php echo htmlspecialchars($row['test_name']); ?></td>
                                            <td><?php echo $row['completed_at']; ?></td>
                                            <td>
                                                <?php if ($row['verified_by']): ?>
                                                    <i class="ri-checkbox-circle-fill text-success"></i> Yes
                                                <?php else: ?>
                                                    <i class="ri-close-circle-fill text-danger"></i> No
                                                <?php endif; ?>
                                            </td>
                                            <td>
                                                <a href="test_results.php?id=<?php echo $row['order_test_id']; ?>"
                                                    class="btn btn-sm btn-info">View</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>$('#completeTable').DataTable();</script>
</body>

</html>