<?php
declare(strict_types=1);

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

$conn = iscms_init();
$pageTitle = 'Recent activity';
$activeNav = 'viewed_queue';

$sql = "SELECT a.id, a.application_number, a.application_type, a.status, a.priority,
               a.submitted_at, a.updated_at, a.closed_at,
               o.org_name,
               p.product_name
        FROM applications a
        INNER JOIN organizations o ON a.org_id = o.id
        LEFT JOIN products p ON a.product_id = p.id
        WHERE a.status <> 'draft'
        ORDER BY a.updated_at DESC
        LIMIT 150";
$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>Recent activity</h1>
        <p>Non-draft applications ordered by last update (useful for follow-up and handovers).</p>
    </div>

    <div class="section-card mb-4">
        <div class="section-header">
            <h2><i class="ri-links-line"></i> Quick links</h2>
        </div>
        <div class="quick-actions">
            <a class="qa-btn" href="applications.php"><i class="ri-file-list-3-line"></i> All applications</a>
            <a class="qa-btn" href="queue_dashboard.php"><i class="ri-stack-line"></i> Active queue</a>
        </div>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-history-line"></i> Recently updated</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>Number</th>
                        <th>Type</th>
                        <th>Organisation</th>
                        <th>Product</th>
                        <th>Status</th>
                        <th>Priority</th>
                        <th>Updated</th>
                        <th>Closed</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="8"><i class="ri-inbox-line me-2"></i>No records</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['application_number']) ?></td>
                            <td><small><?= iscms_h(str_replace('_', ' ', (string) $r['application_type'])) ?></small></td>
                            <td><small><?= iscms_h($r['org_name']) ?></small></td>
                            <td><small><?= iscms_val($r['product_name'] ?? null) ?></small></td>
                            <td><?= iscms_status_badge((string) ($r['status'] ?? '')) ?></td>
                            <td><?= iscms_priority_badge((int) ($r['priority'] ?? 3)) ?></td>
                            <td><small class="text-muted"><?= !empty($r['updated_at']) ? iscms_h(substr((string) $r['updated_at'], 0, 16)) : '-' ?></small></td>
                            <td><small class="text-muted"><?= iscms_val($r['closed_at'] ?? null) ?></small></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
