<?php
declare(strict_types=1);

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

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

$sql = "SELECT r.id, r.inspection_id, r.response, r.remarks, r.evidence_path, r.created_at,
               i.inspection_number,
               ci.clause_reference,
               LEFT(ci.item_text, 100) AS item_snip
        FROM inspection_checklist_responses r
        INNER JOIN inspections i ON r.inspection_id = i.id
        INNER JOIN checklist_items ci ON r.checklist_item_id = ci.id
        ORDER BY r.created_at DESC
        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 responses</h1>
        <p>Recorded answers per checklist item for each inspection.</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="inspections.php"><i class="ri-search-2-line"></i> Inspections</a>
            <a class="qa-btn" href="checklist_items.php"><i class="ri-list-check-2"></i> Checklist items</a>
        </div>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-checkbox-circle-line"></i> Responses</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>Inspection</th>
                        <th>Clause</th>
                        <th>Item (excerpt)</th>
                        <th>Response</th>
                        <th>Remarks (excerpt)</th>
                        <th>Recorded</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="6"><i class="ri-inbox-line me-2"></i>No responses</td></tr>
                    <?php else: ?>
                        <?php foreach ($rows as $r): ?>
                        <?php
                            $rm = (string) ($r['remarks'] ?? '');
                            $rmDisp = $rm !== '' ? (strlen($rm) > 80 ? substr($rm, 0, 77) . '...' : $rm) : '';
                        ?>
                        <tr>
                            <td class="iscms-mono" style="font-weight:600; color:var(--ssbs-blue)"><?= iscms_h($r['inspection_number']) ?></td>
                            <td class="iscms-mono"><small><?= iscms_val($r['clause_reference'] ?? null) ?></small></td>
                            <td><small><?= iscms_val($r['item_snip'] ?? null) ?></small></td>
                            <td><?= iscms_status_badge((string) ($r['response'] ?? '')) ?></td>
                            <td><small><?= $rmDisp !== '' ? iscms_h($rmDisp) : '<span class="text-muted">-</span>' ?></small></td>
                            <td><small class="text-muted"><?= !empty($r['created_at']) ? iscms_h(substr((string) $r['created_at'], 0, 19)) : '-' ?></small></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
