<?php
declare(strict_types=1);

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

$conn = iscms_init();
$pageTitle = 'Inspections';
$activeNav = 'inspections';

$sql = "SELECT i.id, i.inspection_number, i.inspection_type, i.status, i.scheduled_date, i.conducted_date,
               i.location, i.outcome, i.overall_score, i.is_unannounced,
               a.application_number,
               o.org_name,
               ic.title AS checklist_title,
               CONCAT(TRIM(ui.first_name),' ',TRIM(ui.last_name)) AS inspector_name,
               CONCAT(TRIM(us.first_name),' ',TRIM(us.last_name)) AS supervisor_name
        FROM inspections i
        INNER JOIN applications a ON i.application_id = a.id
        INNER JOIN organizations o ON a.org_id = o.id
        LEFT JOIN inspection_checklists ic ON i.checklist_id = ic.id
        INNER JOIN users ui ON i.inspector_id = ui.id
        LEFT JOIN users us ON i.supervisor_id = us.id
        ORDER BY i.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>Inspections</h1>
        <p>Field inspections linked to applications, inspectors, and outcomes.</p>
    </div>

    <div class="section-card mb-4">
        <div class="section-header">
            <h2><i class="ri-links-line"></i> Related registers</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="checklist_items.php"><i class="ri-list-check-2"></i> Checklist items</a>
            <a class="qa-btn" href="inspection_checklist_responses.php"><i class="ri-checkbox-circle-line"></i> Checklist responses</a>
            <a class="qa-btn" href="inspection_findings.php"><i class="ri-error-warning-line"></i> Findings</a>
            <a class="qa-btn" href="inspection_documents.php"><i class="ri-attachment-2"></i> Documents</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-search-2-line"></i> Register</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>Type</th>
                        <th>Status</th>
                        <th>Scheduled</th>
                        <th>Conducted</th>
                        <th>Checklist</th>
                        <th>Inspector</th>
                        <th>Outcome</th>
                        <th>Score</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="11"><i class="ri-inbox-line me-2"></i>No inspections</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><small><?= iscms_h(str_replace('_', ' ', (string) $r['inspection_type'])) ?></small></td>
                            <td><?= iscms_status_badge((string) ($r['status'] ?? '')) ?></td>
                            <td><small class="text-muted"><?= iscms_val($r['scheduled_date'] ?? null) ?></small></td>
                            <td><small class="text-muted"><?= iscms_val($r['conducted_date'] ?? null) ?></small></td>
                            <td><small><?= iscms_val($r['checklist_title'] ?? null) ?></small></td>
                            <td><small><?= iscms_h(trim((string) ($r['inspector_name'] ?? ''))) ?></small></td>
                            <td><?php
                                $outv = trim((string) ($r['outcome'] ?? ''));
                                echo $outv !== '' ? iscms_status_badge($outv) : '<span class="text-muted">-</span>';
                            ?></td>
                            <td><small><?= isset($r['overall_score']) && $r['overall_score'] !== null && $r['overall_score'] !== '' ? iscms_h((string) $r['overall_score']) : '<span class="text-muted">-</span>' ?></small></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
