<?php
declare(strict_types=1);

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

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

$sql = "SELECT a.id, a.notice_id, a.action_type, a.status, a.action_date, a.effective_date,
               a.penalty_amount, a.currency,
               LEFT(a.description, 100) AS desc_snip,
               en.notice_number,
               o.org_name,
               CONCAT(TRIM(u.first_name),' ',TRIM(u.last_name)) AS actioned_by_name
        FROM enforcement_actions a
        INNER JOIN enforcement_notices en ON a.notice_id = en.id
        INNER JOIN organizations o ON en.org_id = o.id
        INNER JOIN users u ON a.actioned_by = u.id
        ORDER BY a.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 actions</h1>
        <p>Actions taken under enforcement notices (fines, seizures, recalls, and so on).</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> Notices</a>
            <a class="qa-btn" href="product_recalls.php"><i class="ri-store-2-line"></i> Recalls</a>
        </div>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-hammer-line"></i> Actions</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>Action type</th>
                        <th>Status</th>
                        <th>Actioned by</th>
                        <th>Penalty</th>
                        <th>Action date</th>
                        <th>Effective</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="8"><i class="ri-inbox-line me-2"></i>No actions</td></tr>
                    <?php else: ?>
                        <?php foreach ($rows as $r): ?>
                        <?php
                            $amt = $r['penalty_amount'] ?? null;
                            $cur = (string) ($r['currency'] ?? 'KES');
                            $pen = ($amt !== null && $amt !== '' && (float) $amt > 0)
                                ? $cur . ' ' . number_format((float) $amt, 2)
                                : '-';
                        ?>
                        <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_h(str_replace('_', ' ', (string) $r['action_type'])) ?></small></td>
                            <td><?= iscms_status_badge((string) ($r['status'] ?? '')) ?></td>
                            <td><small><?= iscms_h(trim((string) ($r['actioned_by_name'] ?? ''))) ?></small></td>
                            <td class="iscms-mono"><small><?= iscms_h($pen) ?></small></td>
                            <td><small class="text-muted"><?= iscms_val($r['action_date'] ?? null) ?></small></td>
                            <td><small class="text-muted"><?= iscms_val($r['effective_date'] ?? null) ?></small></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
