<?php
declare(strict_types=1);

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

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

$sql = "SELECT f.id, f.inspection_id, f.finding_code, f.severity, f.finding_type, f.title,
               LEFT(f.description, 120) AS desc_snip, f.standard_clause, f.status,
               f.response_due_date, f.response_date,
               i.inspection_number,
               a.application_number,
               o.org_name
        FROM inspection_findings f
        INNER JOIN inspections i ON f.inspection_id = i.id
        INNER JOIN applications a ON i.application_id = a.id
        INNER JOIN organizations o ON a.org_id = o.id
        ORDER BY f.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>Inspection findings</h1>
        <p>Non-conformances and observations raised during inspections.</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="inspection_documents.php"><i class="ri-attachment-2"></i> Documents</a>
            <a class="qa-btn" href="non_conformances.php"><i class="ri-error-warning-line"></i> Non-conformances</a>
        </div>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-error-warning-line"></i> Findings</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>Application</th>
                        <th>Organisation</th>
                        <th>Code</th>
                        <th>Title</th>
                        <th>Severity</th>
                        <th>Type</th>
                        <th>Status</th>
                        <th>Clause</th>
                        <th>Due</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="10"><i class="ri-inbox-line me-2"></i>No findings</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['inspection_number']) ?></td>
                            <td class="iscms-mono"><small><?= iscms_h($r['application_number']) ?></small></td>
                            <td><small><?= iscms_h($r['org_name']) ?></small></td>
                            <td class="iscms-mono"><small><?= iscms_val($r['finding_code'] ?? null) ?></small></td>
                            <td><small><?= iscms_h((string) ($r['title'] ?? '')) ?></small></td>
                            <td><?= iscms_status_badge((string) ($r['severity'] ?? '')) ?></td>
                            <td><small><?= iscms_h(str_replace('_', ' ', (string) ($r['finding_type'] ?? ''))) ?></small></td>
                            <td><?= iscms_status_badge((string) ($r['status'] ?? '')) ?></td>
                            <td class="iscms-mono"><small><?= iscms_val($r['standard_clause'] ?? null) ?></small></td>
                            <td><small class="text-muted"><?= iscms_val($r['response_due_date'] ?? null) ?></small></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
