<?php
declare(strict_types=1);

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

$conn = iscms_init();
$pageTitle = 'Non-conformances';
$activeNav = 'non_conformances';

$sql = "SELECT n.id, n.nc_number, n.nc_source, n.nc_type, n.severity, n.status,
               LEFT(n.description, 100) AS desc_snip, n.response_due_date, n.resolved_date,
               o.org_name,
               p.product_name,
               CONCAT(TRIM(ur.first_name),' ',TRIM(ur.last_name)) AS reported_by_name,
               CONCAT(TRIM(ua.first_name),' ',TRIM(ua.last_name)) AS assigned_to_name
        FROM non_conformances n
        INNER JOIN organizations o ON n.org_id = o.id
        LEFT JOIN products p ON n.product_id = p.id
        INNER JOIN users ur ON n.reported_by = ur.id
        LEFT JOIN users ua ON n.assigned_to = ua.id
        ORDER BY n.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>Non-conformances</h1>
        <p>NC register from inspections, labs, complaints, and market surveillance.</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="enforcement_notices.php"><i class="ri-scales-3-line"></i> Enforcement notices</a>
            <a class="qa-btn" href="inspection_findings.php"><i class="ri-error-warning-line"></i> Inspection findings</a>
            <a class="qa-btn" href="organizations.php"><i class="ri-building-2-line"></i> Organisations</a>
        </div>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-error-warning-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>NC number</th>
                        <th>Organisation</th>
                        <th>Product</th>
                        <th>Source</th>
                        <th>Type</th>
                        <th>Severity</th>
                        <th>Status</th>
                        <th>Reported by</th>
                        <th>Assigned</th>
                        <th>Due</th>
                        <th>Resolved</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="11"><i class="ri-inbox-line me-2"></i>No non-conformances</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['nc_number']) ?></td>
                            <td><small><?= iscms_h($r['org_name']) ?></small></td>
                            <td><small><?= iscms_val($r['product_name'] ?? null) ?></small></td>
                            <td><small><?= iscms_h(str_replace('_', ' ', (string) $r['nc_source'])) ?></small></td>
                            <td><small><?= iscms_h((string) ($r['nc_type'] ?? '')) ?></small></td>
                            <td><?= iscms_status_badge((string) ($r['severity'] ?? '')) ?></td>
                            <td><?= iscms_status_badge((string) ($r['status'] ?? '')) ?></td>
                            <td><small><?= iscms_h(trim((string) ($r['reported_by_name'] ?? ''))) ?></small></td>
                            <td><small><?= iscms_val(trim((string) ($r['assigned_to_name'] ?? '')) ?: null) ?></small></td>
                            <td><small class="text-muted"><?= iscms_val($r['response_due_date'] ?? null) ?></small></td>
                            <td><small class="text-muted"><?= iscms_val($r['resolved_date'] ?? null) ?></small></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
