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

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

$test_id = $_GET['test_id'] ?? 0;
$msg = "";

if ($test_id && isset($_POST['collect_sample'])) {
    $notes = $_POST['notes'];
    $collected_at = date('Y-m-d H:i:s');

    // Update Order Test Status
    $sql = "UPDATE lab_order_tests SET status = 'SAMPLE_COLLECTED', started_at = ? WHERE order_test_id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("si", $collected_at, $test_id);

    if ($stmt->execute()) {
        // Also update parent order if all tests are collected (optional logic, skipping for simplicity)
        // Redirect back to pending list
        header("Location: pending_lab_tests.php");
        exit();
    } else {
        $msg = "Error collecting sample: " . $conn->error;
    }
}

// Fetch Info
if ($test_id) {
    $sql = "SELECT t.*, 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.order_test_id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i", $test_id);
    $stmt->execute();
    $row = $stmt->get_result()->fetch_assoc();
}
?>

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

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Sample Collection')); ?>
    <?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-md-6 offset-md-3">
                            <div class="card">
                                <div class="card-header">
                                    <h4 class="card-title">Collect Sample</h4>
                                </div>
                                <div class="card-body">
                                    <?php if ($msg): ?>
                                        <div class="alert alert-danger"><?php echo $msg; ?></div><?php endif; ?>

                                    <?php if ($test_id && $row): ?>
                                        <div class="mb-4">
                                            <h5><?php echo htmlspecialchars($row['first_name'] . ' ' . $row['last_name']); ?>
                                            </h5>
                                            <p class="text-muted">Test: <?php echo htmlspecialchars($row['test_name']); ?>
                                            </p>
                                        </div>

                                        <form method="POST">
                                            <div class="mb-3">
                                                <label class="form-label">Collection Notes / Specimen ID</label>
                                                <textarea class="form-control" name="notes"
                                                    placeholder="Enter Label ID or notes..."></textarea>
                                            </div>
                                            <div class="mb-3 form-check">
                                                <input type="checkbox" class="form-check-input" id="confirm" required>
                                                <label class="form-check-label" for="confirm">I confirm the sample matches
                                                    the patient details.</label>
                                            </div>
                                            <button type="submit" name="collect_sample" class="btn btn-primary w-100">Mark
                                                as Collected</button>
                                        </form>
                                    <?php else: ?>
                                        <div class="alert alert-warning">No test selected.</div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>

</html>