<?php
declare(strict_types=1);

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

$conn = iscms_init();
$pageTitle = 'Checklist items';
$activeNav = 'inspections';

$sql = "SELECT ci.id, ci.checklist_id, ci.clause_reference, ci.item_text, ci.guidance,
               ci.is_critical, ci.sort_order,
               ic.title AS checklist_title
        FROM checklist_items ci
        INNER JOIN inspection_checklists ic ON ci.checklist_id = ic.id
        ORDER BY ic.title ASC, ci.sort_order ASC, ci.id ASC
        LIMIT 600";
$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>Checklist items</h1>
        <p>Individual clauses and prompts within each inspection checklist template.</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="inspection_checklists.php"><i class="ri-file-list-3-line"></i> Checklist templates</a>
            <a class="qa-btn" href="inspection_checklist_responses.php"><i class="ri-checkbox-circle-line"></i> Responses</a>
        </div>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-list-check-2"></i> Items</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>Checklist</th>
                        <th>Clause</th>
                        <th>Item</th>
                        <th>Critical</th>
                        <th>Order</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="5"><i class="ri-inbox-line me-2"></i>No items</td></tr>
                    <?php else: ?>
                        <?php foreach ($rows as $r): ?>
                        <?php
                            $txt = (string) ($r['item_text'] ?? '');
                            $txtDisp = strlen($txt) > 120 ? substr($txt, 0, 117) . '...' : $txt;
                        ?>
                        <tr>
                            <td><small><?= iscms_h($r['checklist_title']) ?></small></td>
                            <td class="iscms-mono"><small><?= iscms_val($r['clause_reference'] ?? null) ?></small></td>
                            <td><small><?= iscms_h($txtDisp) ?></small></td>
                            <td><?= !empty($r['is_critical']) ? '<span class="badge bg-danger-subtle text-danger" 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"><?= (int) ($r['sort_order'] ?? 0) ?></small></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
