<?php

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

date_default_timezone_set('Africa/Lagos');

$userId = (int)$_SESSION['user_id'];


/*
|--------------------------------------------------------------------------
| Default Summary
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT

        COUNT(*) AS total_records,

        COALESCE(SUM(amount), 0) AS total_defaults,

        COALESCE(
            SUM(
                CASE
                    WHEN status = 'unpaid'
                    THEN amount
                    ELSE 0
                END
            ),
            0
        ) AS unpaid_defaults,

        COALESCE(
            SUM(
                CASE
                    WHEN status = 'paid'
                    THEN amount
                    ELSE 0
                END
            ),
            0
        ) AS paid_defaults,

        SUM(
            CASE
                WHEN status = 'unpaid'
                THEN 1
                ELSE 0
            END
        ) AS unpaid_count

    FROM subscription_defaults

    WHERE user_id = ?
");

$stmt->execute([
    $userId
]);

$summary =
    $stmt->fetch(PDO::FETCH_ASSOC);


/*
|--------------------------------------------------------------------------
| Get Member Defaults
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT

        d.id AS default_id,

        d.amount AS default_amount,

        d.default_date,

        d.reason,

        d.status AS default_status,

        d.paid_at,

        d.created_at,

        c.id AS contribution_id,

        c.contribution_no,

        c.amount AS contribution_amount,

        c.due_date,

        c.paid_date,

        c.status AS contribution_status,

        p.plan_name,

        p.frequency

    FROM subscription_defaults d

    INNER JOIN contributions c
        ON d.contribution_id = c.id

    INNER JOIN subscriptions s
        ON d.subscription_id = s.id

    INNER JOIN plans p
        ON d.plan_id = p.id

    WHERE d.user_id = ?

    ORDER BY d.created_at DESC
");

$stmt->execute([
    $userId
]);

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


/*
|--------------------------------------------------------------------------
| Payment Period Helper
|--------------------------------------------------------------------------
*/

function getPaymentPeriod(
    $frequency,
    $dueDate
) {

    $frequency =
        strtolower(
            trim($frequency)
        );

    $date =
        new DateTime($dueDate);


    /*
    | Monthly:
    |
    | Feb 5 deadline belongs to January contribution.
    */

    if ($frequency === 'monthly') {

        $date->modify('-1 month');

        return $date->format('F Y');

    }


    /*
    | Daily
    */

    if ($frequency === 'daily') {

        return $date->format('d M Y');

    }


    /*
    | Weekly
    */

    if ($frequency === 'weekly') {

        return 'Week ending '
            . $date->format('d M Y');

    }


    /*
    | Yearly
    */

    if ($frequency === 'yearly') {

        return $date->format('Y');

    }


    return $date->format('d M Y');

}

?>

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

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

<title>My Defaults - Complete Bliss</title>

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

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

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

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

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

<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"; ?>


<div class="main-content">


<main class="member-defaults-page">


    <!-- PAGE HEADER -->

    <div class="defaults-page-header">

        <div>

            <h1>
                My Defaults
            </h1>

            <p>
                View your missed contribution penalties
                and their current status.
            </p>

        </div>

        <div class="defaults-header-icon">

            <i class="fas fa-triangle-exclamation"></i>

        </div>

    </div>


    <!-- SUMMARY CARDS -->

    <div class="defaults-summary-grid">


        <!-- OUTSTANDING -->

        <div class="default-summary-card">

            <div class="default-summary-icon outstanding">

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

            </div>

            <div>

                <span>
                    Outstanding
                </span>

                <h2>

                    ₦<?= number_format(
                        (float)$summary['unpaid_defaults'],
                        2
                    ); ?>

                </h2>

            </div>

        </div>


        <!-- MISSED PAYMENTS -->

        <div class="default-summary-card">

            <div class="default-summary-icon missed">

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

            </div>

            <div>

                <span>
                    Unpaid Defaults
                </span>

                <h2>

                    <?= (int)$summary['unpaid_count']; ?>

                </h2>

            </div>

        </div>


        <!-- TOTAL DEFAULTS -->

        <div class="default-summary-card">

            <div class="default-summary-icon total">

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

            </div>

            <div>

                <span>
                    Total Default Records
                </span>

                <h2>

                    <?= (int)$summary['total_records']; ?>

                </h2>

            </div>

        </div>


        <!-- CLEARED -->

        <div class="default-summary-card">

            <div class="default-summary-icon cleared">

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

            </div>

            <div>

                <span>
                    Cleared
                </span>

                <h2>

                    ₦<?= number_format(
                        (float)$summary['paid_defaults'],
                        2
                    ); ?>

                </h2>

            </div>

        </div>


    </div>


    <!-- DEFAULT HISTORY -->

    <section class="defaults-history-card">


        <div class="defaults-history-header">

            <div>

                <h2>
                    Default History
                </h2>

                <p>
                    Complete record of your contribution defaults.
                </p>

            </div>

        </div>


        <?php if (count($defaults) > 0): ?>


        <div class="defaults-table-wrap">


            <table class="defaults-table">


                <thead>

                    <tr>

                        <th>Scheme</th>

                        <th>Payment Period</th>

                        <th>Contribution</th>

                        <th>Due Date</th>

                        <th>Reason</th>

                        <th>Default</th>

                        <th>Status</th>

                        <th>Default Date</th>

                    </tr>

                </thead>


                <tbody>


                <?php foreach ($defaults as $default): ?>


                    <tr>


                        <!-- SCHEME -->

                        <td>

                            <div class="default-scheme">

                                <div class="default-scheme-icon">

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

                                </div>

                                <div>

                                    <strong>

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

                                    </strong>

                                    <small>

                                        <?= htmlspecialchars(
                                            ucfirst(
                                                $default['frequency']
                                            )
                                        ); ?>

                                    </small>

                                </div>

                            </div>

                        </td>


                        <!-- PAYMENT PERIOD -->

                        <td>

                            <strong>

                                <?= htmlspecialchars(
                                    getPaymentPeriod(
                                        $default['frequency'],
                                        $default['due_date']
                                    )
                                ); ?>

                            </strong>

                            <small class="default-table-subtext">

                                Contribution
                                #<?= (int)$default['contribution_no']; ?>

                            </small>

                        </td>


                        <!-- CONTRIBUTION AMOUNT -->

                        <td>

                            ₦<?= number_format(
                                (float)$default['contribution_amount'],
                                2
                            ); ?>

                        </td>


                        <!-- DUE DATE -->

                        <td>

                            <?= date(
                                'd M Y',
                                strtotime(
                                    $default['due_date']
                                )
                            ); ?>

                        </td>


                        <!-- REASON -->

                        <td>

                            <span class="default-reason">

                                <?= htmlspecialchars(
                                    $default['reason']
                                ); ?>

                            </span>

                        </td>


                        <!-- PENALTY -->

                        <td>

                            <strong class="default-amount">

                                ₦<?= number_format(
                                    (float)$default['default_amount'],
                                    2
                                ); ?>

                            </strong>

                        </td>


                        <!-- STATUS -->

                        <td>

                            <?php if (
                                $default['default_status']
                                === 'paid'
                            ): ?>

                                <span class="default-status paid">

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

                                    Cleared

                                </span>

                            <?php else: ?>

                                <span class="default-status unpaid">

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

                                    Unpaid

                                </span>

                            <?php endif; ?>

                        </td>


                        <!-- DEFAULT DATE -->

                        <td>

                            <?= date(
                                'd M Y',
                                strtotime(
                                    $default['default_date']
                                )
                            ); ?>

                        </td>


                    </tr>


                <?php endforeach; ?>


                </tbody>


            </table>


        </div>


        <?php else: ?>


            <!-- EMPTY STATE -->

            <div class="defaults-empty-state">

                <div class="defaults-empty-icon">

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

                </div>

                <h3>
                    No Defaults
                </h3>

                <p>
                    You currently have no contribution defaults.
                </p>

            </div>


        <?php endif; ?>


    </section>


</main>


</div>


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


</body>

</html>