<?php
declare(strict_types=1);

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

$conn = iscms_init();
$pageTitle = 'Laboratories';
$activeNav = 'laboratory';

$sql = "SELECT id, lab_name, lab_code, accreditation, location, phone, email,
               is_internal, is_active, created_at
        FROM laboratories
        ORDER BY lab_name ASC
        LIMIT 200";
$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>Laboratories</h1>
        <p>Accredited and internal labs available for test routing.</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_samples.php"><i class="ri-test-tube-line"></i> Samples</a>
            <a class="qa-btn" href="applications.php"><i class="ri-file-list-3-line"></i> Applications</a>
        </div>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-building-4-line"></i> Directory</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>Code</th>
                        <th>Name</th>
                        <th>Accreditation</th>
                        <th>Location</th>
                        <th>Contact</th>
                        <th>Internal</th>
                        <th>Active</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="7"><i class="ri-inbox-line me-2"></i>No laboratories</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['lab_code']) ?></td>
                            <td><?= iscms_h($r['lab_name']) ?></td>
                            <td><small><?= iscms_val($r['accreditation'] ?? null) ?></small></td>
                            <td><small><?= iscms_val($r['location'] ?? null) ?></small></td>
                            <td><small><?php
                                $em = trim((string) ($r['email'] ?? ''));
                                $ph = trim((string) ($r['phone'] ?? ''));
                                $c = array_filter([$ph, $em]);
                                echo $c !== [] ? iscms_h(implode(' | ', $c)) : '<span class="text-muted">-</span>';
                            ?></small></td>
                            <td><?= !empty($r['is_internal']) ? '<span class="badge bg-info-subtle text-primary" style="font-size:.65rem">Yes</span>' : '<span class="badge bg-secondary-subtle text-secondary" style="font-size:.65rem">No</span>' ?></td>
                            <td><?= !empty($r['is_active']) ? '<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();
