<?php
declare(strict_types=1);

require_once __DIR__ . '/includes/iscms_layout.php';

$conn = iscms_init();
$pageTitle = 'Lab samples';
$activeNav = 'laboratory';

$sql = "SELECT s.id, s.sample_number, s.sample_description, s.batch_number, s.quantity, s.unit,
               s.condition_on_receipt, s.status, s.received_date, s.collection_date,
               lr.request_number,
               a.application_number,
               CONCAT(TRIM(u.first_name),' ',TRIM(u.last_name)) AS received_by_name
        FROM lab_samples s
        INNER JOIN lab_test_requests lr ON s.request_id = lr.id
        INNER JOIN applications a ON lr.application_id = a.id
        LEFT JOIN users u ON s.received_by = u.id
        ORDER BY s.updated_at DESC
        LIMIT 500";
$result = $conn->query($sql);
$rows = $result ? $result->fetch_all(MYSQLI_ASSOC) : [];
$dbError = $result ? null : $conn->error;

iscms_render_head($pageTitle);
iscms_render_sidebar($activeNav);
iscms_render_topbar($pageTitle);
?>
    <div class="page-header">
        <h1>Lab samples</h1>
        <p>Samples received against test requests.</p>
    </div>

    <div class="section-card mb-4">
        <div class="section-header">
            <h2><i class="ri-links-line"></i> Related</h2>
        </div>
        <div class="quick-actions">
            <a class="qa-btn" href="lab_test_requests.php"><i class="ri-flask-line"></i> Test requests</a>
            <a class="qa-btn" href="lab_test_results.php"><i class="ri-file-chart-line"></i> Results</a>
            <a class="qa-btn" href="lab_documents.php"><i class="ri-attachment-2"></i> Lab documents</a>
        </div>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-test-tube-line"></i> Samples</h2>
            <span class="text-muted" style="font-size:.75rem"><?= count($rows) ?> shown</span>
        </div>
        <div class="table-responsive">
            <?php if ($dbError): ?>
                <div class="alert alert-danger m-3" style="font-size:.85rem">Unable to load data: <?= iscms_h($dbError) ?></div>
            <?php else: ?>
            <table class="ssbs-table">
                <thead>
                    <tr>
                        <th>Sample</th>
                        <th>Request</th>
                        <th>Application</th>
                        <th>Description</th>
                        <th>Batch</th>
                        <th>Qty</th>
                        <th>Condition</th>
                        <th>Status</th>
                        <th>Received by</th>
                        <th>Received</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="10"><i class="ri-inbox-line me-2"></i>No samples</td></tr>
                    <?php else: ?>
                        <?php foreach ($rows as $r): ?>
                        <?php
                            $qty = isset($r['quantity']) ? (string) $r['quantity'] : '';
                            $un = (string) ($r['unit'] ?? '');
                            $qtyDisp = trim($qty . ' ' . $un);
                        ?>
                        <tr>
                            <td class="iscms-mono" style="font-weight:600; color:var(--ssbs-blue)"><?= iscms_h($r['sample_number']) ?></td>
                            <td class="iscms-mono"><small><?= iscms_h($r['request_number']) ?></small></td>
                            <td class="iscms-mono"><small><?= iscms_h($r['application_number']) ?></small></td>
                            <td><small><?= iscms_h((string) ($r['sample_description'] ?? '')) ?></small></td>
                            <td class="iscms-mono"><small><?= iscms_val($r['batch_number'] ?? null) ?></small></td>
                            <td><small><?= iscms_h($qtyDisp) ?></small></td>
                            <td><?= iscms_status_badge((string) ($r['condition_on_receipt'] ?? '')) ?></td>
                            <td><?= iscms_status_badge((string) ($r['status'] ?? '')) ?></td>
                            <td><small><?= iscms_val(trim((string) ($r['received_by_name'] ?? '')) ?: null) ?></small></td>
                            <td><small class="text-muted"><?= iscms_val($r['received_date'] ?? null) ?></small></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
