<?php
require("header.php");

// ====================== FETCH PLANS ======================
try {
    // Fetch all plans (active + inactive)
    $stmt = $pdo->prepare("SELECT * FROM stocks ORDER BY id ASC");
    $stmt->execute();
    $plans = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    die("Database Error: " . $e->getMessage());
}

$cur_symbol = $cur_symbol ?? '$';
$cur_rate = $cur_rate ?? 1;
$user_bal = $user_bal ?? 0;

// Duration helper
function formatDuration($minutes) {
    if ($minutes < 60) return $minutes . " minutes";
    elseif ($minutes < 1440) return round($minutes / 60) . " hour(s)";
    elseif ($minutes < 10080) return round($minutes / 1440) . " day(s)";
    elseif ($minutes < 43200) return round($minutes / 10080) . " week(s)";
    else return round($minutes / 43200) . " month(s)";
}
?>

<main class="flex-1 px-2 py-2 md:p-4 pb-20 md:pb-6 overflow-hidden">

    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 mt-2 mb-10 md:mb-1 gap-6">
        <?php if (!empty($plans)): ?>
            <?php foreach ($plans as $row): ?>
                <?php
                    $durationText = formatDuration($row['duration']);
                    $minAmt = number_format($row['min_amt'] * $cur_rate, 2);

                    $isActive = ($row['status'] == 1);
                    $badgeText = $isActive ? "Active" : "Not Active";
                    $badgeColor = $isActive ? "bg-green-500/10 text-green-400" : "bg-red-500/10 text-red-400";
                ?>
                <div class="bg-[#0F0A1F] rounded-2xl overflow-hidden">
                    <div class="bg-[#1A1428] p-6">
                        <div class="flex items-center justify-between mb-4">
                            <h3 class="text-xl font-medium"><?= htmlspecialchars($row['plan']) ?></h3>
                            <span class="px-3 py-1 text-xs rounded-full <?= $badgeColor ?>">
                                <?= $badgeText ?>
                            </span>
                        </div>
                        <div class="space-y-1">
                            <div class="text-3xl font-bold text-[#2FE6DE]">
                                <?= $cur_symbol . $minAmt ?>
                            </div>
                            <p class="text-sm text-gray-400">Minimum Investment</p>
                        </div>
                    </div>

                    <div class="p-6 space-y-4">
                        <p class="text-sm text-gray-300 line-clamp-4"><?= htmlspecialchars($row['note']) ?></p>
                        <div class="grid grid-cols-2 gap-4">
                            <div class="bg-[#1A1428] p-4 rounded-xl text-center">
                                <div class="text-sm text-gray-400 mb-1">Duration</div>
                                <div class="font-medium"><?= $durationText ?></div>
                            </div>
                            <div class="bg-[#1A1428] p-4 rounded-xl text-center">
                                <div class="text-sm text-gray-400 mb-1">ROI</div>
                                <div class="font-medium text-[#2FE6DE]"><?= htmlspecialchars($row['percentage']) ?>%</div>
                            </div>
                        </div>

                        <?php if ($isActive): ?>
                            <!-- Active plan: normal Invest button -->
                            <a href="buy_plan_details.php?id=<?= $row['id'] ?>"
                               class="w-full inline-block text-center bg-gradient-to-r from-[#2FE6DE] to-[#2FE6DE]/80 text-black font-medium px-6 py-3 rounded-xl hover:brightness-110 transition-all">
                               Invest
                            </a>
                        <?php else: ?>
                            <!-- Inactive plan: disabled button -->
                            <button disabled
                                class="w-full text-center bg-gray-700 text-gray-400 font-medium px-6 py-3 rounded-xl opacity-60 cursor-not-allowed">
                                Invest (Disabled)
                            </button>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endforeach; ?>
        <?php else: ?>
            <p class="text-gray-400 text-center col-span-3">No investment plans available at the moment.</p>
        <?php endif; ?>
    </div>

</main>

<?php require("footer.php"); ?>
