<?php
declare(strict_types=1);

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

$conn = iscms_init();
$pageTitle = 'Enforcement notices';
$activeNav = 'enforcement';

$sql = "SELECT e.id, e.notice_number, e.notice_type, e.status, e.issue_date, e.response_deadline,
               LEFT(e.description, 90) AS desc_snip,
               o.org_name,
               p.product_name,
               n.nc_number,
               CONCAT(TRIM(ui.first_name),' ',TRIM(ui.last_name)) AS issued_by_name,
               CONCAT(TRIM(ua.first_name),' ',TRIM(ua.last_name)) AS authorized_by_name
        FROM enforcement_notices e
        INNER JOIN organizations o ON e.org_id = o.id
        LEFT JOIN products p ON e.product_id = p.id
        LEFT JOIN non_conformances n ON e.nc_id = n.id
        INNER JOIN users ui ON e.issued_by = ui.id
        LEFT JOIN users ua ON e.authorized_by = ua.id
        ORDER BY e.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>Enforcement notices</h1>
        <p>Formal notices to organisations, optionally linked to non-conformances.</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="non_conformances.php"><i class="ri-error-warning-line"></i> Non-conformances</a>
            <a class="qa-btn" href="enforcement_actions.php"><i class="ri-hammer-line"></i> Enforcement actions</a>
            <a class="qa-btn" href="product_recalls.php"><i class="ri-store-2-line"></i> Product recalls</a>
        </div>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-scales-3-line"></i> Notices</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>Notice</th>
                        <th>Organisation</th>
                        <th>Product</th>
                        <th>NC ref</th>
                        <th>Type</th>
                        <th>Status</th>
                        <th>Issued by</th>
                        <th>Issue date</th>
                        <th>Deadline</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="9"><i class="ri-inbox-line me-2"></i>No notices</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['notice_number']) ?></td>
                            <td><small><?= iscms_h($r['org_name']) ?></small></td>
                            <td><small><?= iscms_val($r['product_name'] ?? null) ?></small></td>
                            <td class="iscms-mono"><small><?= iscms_val($r['nc_number'] ?? null) ?></small></td>
                            <td><small><?= iscms_h(str_replace('_', ' ', (string) $r['notice_type'])) ?></small></td>
                            <td><?= iscms_status_badge((string) ($r['status'] ?? '')) ?></td>
                            <td><small><?= iscms_h(trim((string) ($r['issued_by_name'] ?? ''))) ?></small></td>
                            <td><small class="text-muted"><?= iscms_val($r['issue_date'] ?? null) ?></small></td>
                            <td><small class="text-muted"><?= iscms_val($r['response_deadline'] ?? null) ?></small></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
