<?php
declare(strict_types=1);

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

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

$sql = "SELECT tr.id, tr.result_number, tr.test_method, tr.standard_applied, tr.test_date,
               tr.verification_date, tr.overall_result, tr.is_accredited_test,
               s.sample_number,
               lr.request_number,
               a.application_number,
               CONCAT(TRIM(ut.first_name),' ',TRIM(ut.last_name)) AS tested_by_name,
               CONCAT(TRIM(uv.first_name),' ',TRIM(uv.last_name)) AS verified_by_name
        FROM lab_test_results tr
        INNER JOIN lab_samples s ON tr.sample_id = s.id
        INNER JOIN lab_test_requests lr ON s.request_id = lr.id
        INNER JOIN applications a ON lr.application_id = a.id
        INNER JOIN users ut ON tr.tested_by = ut.id
        LEFT JOIN users uv ON tr.verified_by = uv.id
        ORDER BY tr.updated_at DESC
        LIMIT 400";
$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 test results</h1>
        <p>Result records per sample with verification trail.</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_samples.php"><i class="ri-test-tube-line"></i> Samples</a>
            <a class="qa-btn" href="test_result_parameters.php"><i class="ri-list-settings-line"></i> Parameters</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-file-chart-line"></i> Results</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>Result</th>
                        <th>Sample</th>
                        <th>Request</th>
                        <th>Application</th>
                        <th>Method</th>
                        <th>Standard</th>
                        <th>Overall</th>
                        <th>Test date</th>
                        <th>Tested by</th>
                        <th>Verified by</th>
                        <th>Accredited</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="11"><i class="ri-inbox-line me-2"></i>No results</td></tr>
                    <?php else: ?>
                        <?php foreach ($rows as $r): ?>
                        <tr>
                            <td class="iscms-mono" style="font-weight:600; color:var(--ssbs-blue)"><?= iscms_h($r['result_number']) ?></td>
                            <td class="iscms-mono"><small><?= iscms_h($r['sample_number']) ?></small></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['test_method'] ?? '')) ?></small></td>
                            <td><small><?= iscms_val($r['standard_applied'] ?? null) ?></small></td>
                            <td><?= iscms_status_badge((string) ($r['overall_result'] ?? '')) ?></td>
                            <td><small class="text-muted"><?= iscms_val($r['test_date'] ?? null) ?></small></td>
                            <td><small><?= iscms_h(trim((string) ($r['tested_by_name'] ?? ''))) ?></small></td>
                            <td><small><?= iscms_val(trim((string) ($r['verified_by_name'] ?? '')) ?: null) ?></small></td>
                            <td><?= !empty($r['is_accredited_test']) ? '<span class="badge bg-success-subtle text-success" style="font-size:.65rem">Yes</span>' : '<span class="badge bg-secondary-subtle text-secondary" style="font-size:.65rem">No</span>' ?></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
