<?php

include "../includes/member_auth.php";
include "../config/database.php";

$user_id = $_SESSION['user_id'];

/*
|--------------------------------------------------------------------------
| Get Active Schemes
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT *
    FROM plans
    WHERE status = 'active'
    ORDER BY id DESC
");

$stmt->execute();

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

?>

<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta
        name="viewport"
        content="width=device-width, initial-scale=1.0"
    >

    <title>Available Schemes</title>


    <!-- Existing website styles -->
    <link
        rel="stylesheet"
        href="../assets/css/admin.css"
    >

    <link
        rel="stylesheet"
        href="../assets/css/responsive.css"
    >

    <link
        rel="stylesheet"
        href="../assets/css/member.css"
    >


    <!-- New Schemes page styles -->
    <link
        rel="stylesheet"
        href="../assets/css/member-schemes.css"
    >

    <link
        rel="stylesheet"
        href="../assets/css/member-schemes-responsive.css"
    >


    <!-- Font Awesome -->
    <link
        rel="stylesheet"
        href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css"
    >

</head>


<body>


<?php include "../includes/member_sidebar.php"; ?>

<div class="sidebar-overlay"></div>

<?php include "../includes/member_topbar.php"; ?>

<main class="main-content">
<div class="member-schemes-page">


    <!-- =====================================================
         PAGE HEADER
    ====================================================== -->

    <section class="member-schemes-header">

        <div class="member-schemes-header-icon">

            <i class="fas fa-layer-group"></i>

        </div>

        <div>

            <h1>
                Available Schemes
            </h1>

            <p>
                Explore our contribution schemes and choose
                one that suits your needs.
            </p>

        </div>

    </section>



    <!-- =====================================================
         SCHEME LIST
    ====================================================== -->

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


        <section class="member-schemes-grid">


            <?php foreach ($schemes as $scheme): ?>


                <?php

                /*
                |--------------------------------------------------------------------------
                | Check If This Member Has Joined The Scheme
                |--------------------------------------------------------------------------
                */

                $subscriptionStmt = $pdo->prepare("
                    SELECT id, hands
                    FROM subscriptions
                    WHERE user_id = ?
                    AND plan_id = ?
                    AND status = 'active'
                    LIMIT 1
                ");

                $subscriptionStmt->execute([
                    $user_id,
                    $scheme['id']
                ]);

                $subscription = $subscriptionStmt->fetch(
                    PDO::FETCH_ASSOC
                );


                /*
                |--------------------------------------------------------------------------
                | Scheme Period
                |--------------------------------------------------------------------------
                */

                $schemePeriod = "Not specified";

                if (
                    !empty($scheme['start_month']) &&
                    !empty($scheme['start_year']) &&
                    !empty($scheme['duration'])
                ) {

                    try {

                        $startDate = new DateTime(
                            $scheme['start_year']
                            . '-'
                            . str_pad(
                                $scheme['start_month'],
                                2,
                                '0',
                                STR_PAD_LEFT
                            )
                            . '-01'
                        );

                        $endDate = clone $startDate;

                        $endDate->modify(
                            '+' . ((int)$scheme['duration'] - 1) . ' months'
                        );

                        $schemePeriod =
                            $startDate->format('F Y')
                            . ' – '
                            . $endDate->format('F Y');

                    } catch (Exception $e) {

                        $schemePeriod = "Not specified";

                    }

                }


                /*
                |--------------------------------------------------------------------------
                | Package Items
                |--------------------------------------------------------------------------
                */

                $packageItems = [];

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

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

                }


                /*
                |--------------------------------------------------------------------------
                | Scheme Image
                |--------------------------------------------------------------------------
                */

                $schemeImage = !empty($scheme['plan_image'])
                    ? "../assets/uploads/plans/" . $scheme['plan_image']
                    : null;

                ?>


                <!-- =================================================
                     SCHEME CARD
                ================================================== -->

                <article class="member-scheme-card">


                    <!-- IMAGE -->

                    <div class="member-scheme-image">

                        <?php if ($schemeImage): ?>

                            <img
                                src="<?= htmlspecialchars($schemeImage); ?>"
                                alt="<?= htmlspecialchars($scheme['plan_name']); ?>"
                            >

                        <?php else: ?>

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

                                <i class="fas fa-image"></i>

                                <span>
                                    No Image Available
                                </span>

                            </div>

                        <?php endif; ?>


                        <!-- IMAGE BADGES -->

                        <div class="member-scheme-image-badges">


                            <span class="member-scheme-type">

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

                            </span>


                            <?php if ($subscription): ?>

                                <span class="member-scheme-status joined">

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

                                    Joined

                                </span>

                            <?php else: ?>

                                <span class="member-scheme-status available">

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

                                    Available

                                </span>

                            <?php endif; ?>


                        </div>

                    </div>



                    <!-- CARD CONTENT -->

                    <div class="member-scheme-content">


                        <!-- NAME -->

                        <h2 class="member-scheme-name">

                            <?= htmlspecialchars(
                                $scheme['plan_name']
                            ); ?>

                        </h2>


                        <!-- DESCRIPTION -->

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

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

                                <?= htmlspecialchars(
                                    $scheme['description']
                                ); ?>

                            </p>

                        <?php endif; ?>



                        <!-- DETAILS -->

                        <div class="member-scheme-details">


                            <!-- CONTRIBUTION -->

                            <div class="member-scheme-detail">

                                <div class="member-scheme-detail-icon">

                                    <i class="fas fa-money-bill-wave"></i>

                                </div>

                                <div>

                                    <span>
                                        Contribution
                                    </span>

                                    <strong>
                                        ₦<?= number_format(
                                            $scheme['amount'],
                                            2
                                        ); ?>
                                    </strong>

                                    <small>
                                        Per Hand
                                    </small>

                                </div>

                            </div>



                            <!-- FREQUENCY -->

                            <div class="member-scheme-detail">

                                <div class="member-scheme-detail-icon">

                                    <i class="fas fa-repeat"></i>

                                </div>

                                <div>

                                    <span>
                                        Frequency
                                    </span>

                                    <strong>
                                        <?= htmlspecialchars(
                                            $scheme['frequency']
                                        ); ?>
                                    </strong>

                                </div>

                            </div>



                            <!-- DURATION -->

                            <div class="member-scheme-detail">

                                <div class="member-scheme-detail-icon">

                                    <i class="fas fa-hourglass-half"></i>

                                </div>

                                <div>

                                    <span>
                                        Duration
                                    </span>

                                    <strong>

                                        <?= !empty($scheme['duration'])
                                            ? (int)$scheme['duration'] . ' Months'
                                            : 'Not specified';
                                        ?>

                                    </strong>

                                </div>

                            </div>



                            <!-- PERIOD -->

                            <div class="member-scheme-detail">

                                <div class="member-scheme-detail-icon">

                                    <i class="fas fa-calendar-days"></i>

                                </div>

                                <div>

                                    <span>
                                        Scheme Period
                                    </span>

                                    <strong>
                                        <?= htmlspecialchars(
                                            $schemePeriod
                                        ); ?>
                                    </strong>

                                </div>

                            </div>


                        </div>



                        <!-- WHAT YOU WILL GET -->

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

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

                                <h3>

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

                                    What You Will Get

                                </h3>


                                <ul>

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

                                        <?php

                                        $item = trim($item);

                                        if ($item === '') {
                                            continue;
                                        }

                                        ?>

                                        <li>

                                            <i class="fas fa-check"></i>

                                            <span>
                                                <?= htmlspecialchars($item); ?>
                                            </span>

                                        </li>

                                    <?php endforeach; ?>

                                </ul>

                            </div>

                        <?php endif; ?>



                        <!-- ACTION -->

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


                            <?php if ($subscription): ?>


                                <div class="member-scheme-joined">

                                    <div>

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

                                        <strong>
                                            Already Joined
                                        </strong>

                                    </div>


                                    <span>

                                        <?= (int)$subscription['hands']; ?>

                                        Hand<?= (
                                            (int)$subscription['hands'] > 1
                                        ) ? 's' : ''; ?>

                                    </span>

                                </div>


                            <?php else: ?>


                                <a
                                    href="join_plan.php?id=<?= (int)$scheme['id']; ?>"
                                    class="member-scheme-join-button"
                                >

                                    <span>
                                        Join Scheme
                                    </span>

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

                                </a>


                            <?php endif; ?>


                        </div>


                    </div>

                </article>


            <?php endforeach; ?>


        </section>


    <?php else: ?>


        <!-- =================================================
             EMPTY STATE
        ================================================== -->

        <section class="member-schemes-empty">

            <div class="member-schemes-empty-icon">

                <i class="fas fa-layer-group"></i>

            </div>

            <h2>
                No Schemes Available
            </h2>

            <p>
                There are currently no active contribution schemes.
                Please check again later.
            </p>

        </section>


    <?php endif; ?>

    </div>
</main>


<script src="../assets/js/admin.js"></script>

</body>

</html>