<?php
declare(strict_types=1);

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

$conn = iscms_init();
$pageTitle = 'Recall follow-ups';
$activeNav = 'market_surveillance';

$sql = "SELECT f.id, f.recall_id, f.follow_up_date, f.observations, f.units_recovered,
               f.issues_found, f.status, f.next_follow_up,
               pr.recall_number,
               o.org_name,
               CONCAT(TRIM(u.first_name),' ',TRIM(u.last_name)) AS officer_name
        FROM recall_follow_ups f
        INNER JOIN product_recalls pr ON f.recall_id = pr.id
        INNER JOIN organizations o ON pr.org_id = o.id
        INNER JOIN users u ON f.officer_id = u.id
        ORDER BY f.follow_up_date DESC, f.created_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>Recall follow-ups</h1>
        <p>Field follow-up visits and recovery progress against recalls.</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="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-calendar-check-line"></i> Follow-ups</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>Recall</th>
                        <th>Organisation</th>
                        <th>Officer</th>
                        <th>Date</th>
                        <th>Status</th>
                        <th>Units recovered</th>
                        <th>Next visit</th>
                        <th>Observations (excerpt)</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="8"><i class="ri-inbox-line me-2"></i>No follow-ups</td></tr>
                    <?php else: ?>
                        <?php foreach ($rows as $r): ?>
                        <?php
                            $ob = (string) ($r['observations'] ?? '');
                            $obDisp = strlen($ob) > 100 ? substr($ob, 0, 97) . '...' : $ob;
                        ?>
                        <tr>
                            <td class="iscms-mono" style="font-weight:600; color:var(--ssbs-blue)"><?= iscms_h($r['recall_number']) ?></td>
                            <td><small><?= iscms_h($r['org_name']) ?></small></td>
                            <td><small><?= iscms_h(trim((string) ($r['officer_name'] ?? ''))) ?></small></td>
                            <td><small class="text-muted"><?= iscms_val($r['follow_up_date'] ?? null) ?></small></td>
                            <td><?= iscms_status_badge((string) ($r['status'] ?? '')) ?></td>
                            <td><small><?= (int) ($r['units_recovered'] ?? 0) ?></small></td>
                            <td><small class="text-muted"><?= iscms_val($r['next_follow_up'] ?? null) ?></small></td>
                            <td><small><?= iscms_h($obDisp) ?></small></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
