<?php
declare(strict_types=1);

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

$conn = iscms_init();
$pageTitle = 'Inspection checklists';
$activeNav = 'inspections';

$sql = "SELECT ic.id, ic.title, ic.category, ic.version, ic.is_active, ic.created_at,
               st.standard_number, st.title AS standard_title
        FROM inspection_checklists ic
        LEFT JOIN standards st ON ic.standard_id = st.id
        ORDER BY ic.title ASC
        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>Inspection checklists</h1>
        <p>Reusable checklist templates linked to standards where applicable.</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="checklist_items.php"><i class="ri-list-check-2"></i> Checklist items</a>
            <a class="qa-btn" href="inspections.php"><i class="ri-search-2-line"></i> Inspections</a>
            <a class="qa-btn" href="inspection_checklist_responses.php"><i class="ri-checkbox-circle-line"></i> Responses</a>
            <a class="qa-btn" href="standards.php"><i class="ri-book-2-line"></i> Standards</a>
        </div>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-file-list-3-line"></i> Templates</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>Title</th>
                        <th>Category</th>
                        <th>Version</th>
                        <th>Standard</th>
                        <th>Active</th>
                        <th>Created</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="6"><i class="ri-inbox-line me-2"></i>No checklists</td></tr>
                    <?php else: ?>
                        <?php foreach ($rows as $r): ?>
                        <?php
                            $std = trim((string) ($r['standard_number'] ?? '') . ' ' . (string) ($r['standard_title'] ?? ''));
                            $std = trim($std);
                        ?>
                        <tr>
                            <td><strong><?= iscms_h($r['title']) ?></strong></td>
                            <td><small><?= iscms_val($r['category'] ?? null) ?></small></td>
                            <td class="iscms-mono"><small><?= iscms_h((string) ($r['version'] ?? '1.0')) ?></small></td>
                            <td><small><?= $std !== '' ? iscms_h($std) : '<span class="text-muted">-</span>' ?></small></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>
                            <td><small class="text-muted"><?= !empty($r['created_at']) ? iscms_h(substr((string) $r['created_at'], 0, 10)) : '-' ?></small></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
