<?php
declare(strict_types=1);

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

$conn = iscms_init();
$pageTitle = 'Product recalls';
$activeNav = 'market_surveillance';

$sql = "SELECT pr.id, pr.recall_number, pr.recall_type, pr.severity, pr.status,
               pr.initiated_date, pr.completion_date, pr.estimated_units, pr.units_recovered,
               pr.recovery_pct, pr.public_notice_issued,
               LEFT(pr.reason, 80) AS reason_snip,
               o.org_name,
               p.product_name,
               en.notice_number,
               CONCAT(TRIM(u.first_name),' ',TRIM(u.last_name)) AS initiated_by_name
        FROM product_recalls pr
        INNER JOIN organizations o ON pr.org_id = o.id
        INNER JOIN products p ON pr.product_id = p.id
        LEFT JOIN enforcement_notices en ON pr.enforcement_notice_id = en.id
        INNER JOIN users u ON pr.initiated_by = u.id
        ORDER BY pr.updated_at DESC
        LIMIT 300";
$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>Product recalls</h1>
        <p>Voluntary and mandatory recalls with recovery tracking.</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="recall_follow_ups.php"><i class="ri-calendar-check-line"></i> Recall follow-ups</a>
            <a class="qa-btn" href="enforcement_notices.php"><i class="ri-scales-3-line"></i> Enforcement notices</a>
            <a class="qa-btn" href="products.php"><i class="ri-box-3-line"></i> Products</a>
        </div>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-store-2-line"></i> Recalls</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>Product</th>
                        <th>Notice</th>
                        <th>Type</th>
                        <th>Severity</th>
                        <th>Status</th>
                        <th>Initiated</th>
                        <th>Est. units</th>
                        <th>Recovered</th>
                        <th>Recovery %</th>
                        <th>Public notice</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="12"><i class="ri-inbox-line me-2"></i>No recalls</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['recall_number']) ?></td>
                            <td><small><?= iscms_h($r['org_name']) ?></small></td>
                            <td><small><?= iscms_h($r['product_name']) ?></small></td>
                            <td class="iscms-mono"><small><?= iscms_val($r['notice_number'] ?? null) ?></small></td>
                            <td><small><?= iscms_h(str_replace('_', ' ', (string) $r['recall_type'])) ?></small></td>
                            <td><?= iscms_status_badge((string) ($r['severity'] ?? '')) ?></td>
                            <td><?= iscms_status_badge((string) ($r['status'] ?? '')) ?></td>
                            <td><small class="text-muted"><?= iscms_val($r['initiated_date'] ?? null) ?></small></td>
                            <td><small><?= isset($r['estimated_units']) ? (int) $r['estimated_units'] : '-' ?></small></td>
                            <td><small><?= isset($r['units_recovered']) ? (int) $r['units_recovered'] : '0' ?></small></td>
                            <td class="iscms-mono"><small><?= isset($r['recovery_pct']) ? iscms_h(number_format((float) $r['recovery_pct'], 1)) : '-' ?></small></td>
                            <td><?= !empty($r['public_notice_issued']) ? '<span class="badge bg-success-subtle text-success" style="font-size:.65rem">Yes</span>' : '<span class="badge bg-secondary-subtle text-secondary" style="font-size:.65rem">No</span>' ?></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
