<?php

require_once __DIR__ . '/../../config/database.php';

$publicPlans = [];

try {

    $stmt = $pdo->query("
        SELECT
            id,
            plan_name,
            plan_type,
            amount,
            frequency,
            duration,
            description,
            plan_image,
            package_items
        FROM plans
        WHERE status = 'active'
        ORDER BY created_at DESC
        LIMIT 3
    ");

    $publicPlans = $stmt->fetchAll(PDO::FETCH_ASSOC);

} catch (PDOException $e) {

    $publicPlans = [];

}

?>


<section class="cbx-schemes" id="schemes">

    <div class="cbx-container">


        <!-- =====================================
             SECTION HEADING
        ====================================== -->

        <div class="cbx-schemes-heading">

            <div class="cbx-schemes-tag">

                <i class="fa-solid fa-basket-shopping"></i>

                <span>
                    AVAILABLE SCHEMES
                </span>

            </div>


            <h2>
                Start with something
                <span>worth looking forward to.</span>
            </h2>


            <p>
                Explore contribution schemes created around
                everyday needs, family goals and things you
                genuinely want to achieve.
            </p>

        </div>



        <?php if (!empty($publicPlans)): ?>


            <div class="cbx-scheme-grid">


                <?php foreach ($publicPlans as $plan): ?>


                    <?php

                    $packageItems = [];

                    if (!empty($plan['package_items'])) {

                        $packageItems = preg_split(
                            '/\r\n|\r|\n/',
                            trim($plan['package_items'])
                        );

                        $packageItems = array_filter(
                            array_map(
                                'trim',
                                $packageItems
                            )
                        );

                    }

                    $packageItems =
                        array_slice(
                            $packageItems,
                            0,
                            3
                        );

                    ?>


                    <article class="cbx-public-scheme-card">


                        <!-- =================================
                             IMAGE
                        ================================== -->

                        <div class="cbx-public-scheme-image">


                            <?php if (!empty($plan['plan_image'])): ?>

                                <img
                                    src="assets/uploads/plans/<?= htmlspecialchars($plan['plan_image']) ?>"
                                    alt="<?= htmlspecialchars($plan['plan_name']) ?>"
                                    loading="lazy"
                                >

                            <?php else: ?>

                                <div class="cbx-scheme-image-placeholder">

                                    <i class="fa-solid fa-box-open"></i>

                                    <span>
                                        Complete Bliss Scheme
                                    </span>

                                </div>

                            <?php endif; ?>


                            <!-- TYPE BADGE -->

                            <div class="cbx-scheme-type">

                                <?= htmlspecialchars($plan['plan_type']) ?>

                            </div>


                            <!-- FREQUENCY -->

                            <div class="cbx-scheme-frequency">

                                <i class="fa-regular fa-calendar"></i>

                                <?= htmlspecialchars($plan['frequency']) ?>

                            </div>


                        </div>



                        <!-- =================================
                             BODY
                        ================================== -->

                        <div class="cbx-public-scheme-body">


                            <div class="cbx-scheme-title-row">


                                <div>

                                    <span class="cbx-scheme-small-title">
                                        CONTRIBUTION SCHEME
                                    </span>


                                    <h3>
                                        <?= htmlspecialchars($plan['plan_name']) ?>
                                    </h3>

                                </div>


                            </div>



                            <!-- DESCRIPTION -->

                            <?php if (!empty($plan['description'])): ?>

                                <p class="cbx-scheme-description">

                                    <?= htmlspecialchars(
                                        mb_strimwidth(
                                            $plan['description'],
                                            0,
                                            145,
                                            '...'
                                        )
                                    ) ?>

                                </p>

                            <?php endif; ?>



                            <!-- PRICE -->

                            <div class="cbx-scheme-price-box">


                                <div>

                                    <small>
                                        Contribution
                                    </small>


                                    <strong>

                                        ₦<?= number_format(
                                            (float)$plan['amount'],
                                            0
                                        ) ?>

                                    </strong>

                                </div>


                                <div class="cbx-scheme-duration">

                                    <span>
                                        <?= (int)$plan['duration'] ?>
                                    </span>

                                    <small>
                                        Months
                                    </small>

                                </div>


                            </div>



                            <!-- PACKAGE ITEMS -->

                            <?php if (!empty($packageItems)): ?>

                                <div class="cbx-scheme-package">


                                    <span class="cbx-scheme-package-title">

                                        <i class="fa-solid fa-gift"></i>

                                        What this scheme includes

                                    </span>


                                    <div class="cbx-scheme-package-list">


                                        <?php foreach ($packageItems as $item): ?>

                                            <span>

                                                <i class="fa-solid fa-circle-check"></i>

                                                <?= htmlspecialchars($item) ?>

                                            </span>

                                        <?php endforeach; ?>


                                    </div>


                                </div>

                            <?php endif; ?>



                            <!-- ACTION -->

                            <div class="cbx-scheme-action">


                                <a
                                     href="scheme.php?id=<?= (int) $plan['id'] ?>"
                                    class="cbx-scheme-join"
                                >

                                    View Scheme Details

                                    <i class="fa-solid fa-arrow-right"></i>

                                </a>


                            </div>


                        </div>


                    </article>


                <?php endforeach; ?>


            </div>


            <!-- BOTTOM CTA -->

            <div class="cbx-schemes-bottom">

                <div>

                    <i class="fa-solid fa-circle-info"></i>

                    <span>
                        Create an account to view full scheme details
                        and choose the number of hands you want.
                    </span>

                </div>


                <a href="register.php">

                    Get Started

                    <i class="fa-solid fa-arrow-right"></i>

                </a>

            </div>


        <?php else: ?>


            <!-- NO ACTIVE SCHEME -->

            <div class="cbx-no-schemes">

                <div class="cbx-no-schemes-icon">

                    <i class="fa-solid fa-basket-shopping"></i>

                </div>


                <h3>
                    New Schemes Are Coming
                </h3>


                <p>
                    Our next set of contribution opportunities
                    will appear here once they are available.
                </p>


                <a href="register.php">
                    Become a Member
                </a>

            </div>


        <?php endif; ?>


    </div>



    <div class="cbx-home-schemes-view-all">

    <a href="schemes.php">

        View All Schemes

        <i class="fa-solid fa-arrow-right"></i>

    </a>

</div>

</section>

