<?php
$page_title = 'Payment';
require __DIR__ . '/_guard.php';

// Give the ITN a moment to process
$attempts = 0;
$is_active = false;
while ($attempts < 3) {
    $fresh = db_row('SELECT status FROM members WHERE id=:id', ['id'=>$member['id']]);
    if ($fresh && $fresh['status'] === 'active') {
        $is_active = true;
        break;
    }
    if ($attempts < 2) sleep(1);
    $attempts++;
}
?>

<section class="section">
    <div class="container" style="max-width:600px;text-align:center;">

        <?php if ($is_active): ?>
            <div style="font-size:3.5rem;margin-bottom:1rem;">🎉</div>
            <h1>Payment confirmed!</h1>
            <p>
                Your <strong><?= htmlspecialchars($member['tier']) ?></strong> membership
                is now active. Let's get your business listed.
            </p>
            <div class="card mt-3" style="text-align:left;padding:1.5rem;">
                <h3 style="margin:0 0 .5rem;">What happens next</h3>
                <ol style="margin:0;padding-left:1.25rem;line-height:2;">
                    <li>Complete your company profile below</li>
                    <li>Upload your logo and business details</li>
                    <li>Your listing goes live immediately</li>
                </ol>
            </div>
            <a href="complete-profile.php" class="btn mt-3" style="width:100%;">
                Complete my listing →
            </a>

        <?php else: ?>
            <div style="font-size:3rem;margin-bottom:1rem;">⏳</div>
            <h1>Payment processing…</h1>
            <p class="muted">
                Your payment is being confirmed. This usually takes a few seconds.
                The page will refresh automatically.
            </p>
            <div class="alert alert-info mt-3">
                If you completed payment on PayFast, your account will activate momentarily.
                If nothing happens after 30 seconds,
                <a href="payment-return.php">click here to check again</a>.
            </div>
            <script>setTimeout(() => location.reload(), 5000);</script>
        <?php endif; ?>

    </div>
</section>

<?php require __DIR__ . '/_footer.php'; ?>