<?php

date_default_timezone_set('Africa/Lagos');

include "config/database.php";


/*
|--------------------------------------------------------------------------
| Get Clearance Code
|--------------------------------------------------------------------------
*/

$code = strtoupper(
    trim($_GET['code'] ?? '')
);

$clearance = null;


/*
|--------------------------------------------------------------------------
| Verify Code
|--------------------------------------------------------------------------
*/

if ($code !== '') {

$stmt = $pdo->prepare("
    SELECT

        sc.clearance_code,
        sc.clearance_date,
        sc.status AS clearance_status,

        u.fullname,

        p.plan_name,
        p.frequency,

        s.hands

    FROM scheme_clearances sc

    INNER JOIN users u
        ON sc.user_id = u.id

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

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

    WHERE sc.clearance_code = ?
    AND sc.status = 'completed'

    LIMIT 1
");

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

$clearance = $stmt->fetch(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>Clearance Verification - Complete Bliss</title>

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

<style>

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    padding: 30px 15px;
    background: #f4f7fb;
    font-family: Arial, Helvetica, sans-serif;
    color: #1f2937;
}

.verify-wrapper {
    width: 100%;
    max-width: 650px;
    margin: 0 auto;
}

.verify-card {
    background: #ffffff;
    border-radius: 20px;
    overflow: hidden;
    box-shadow: 0 10px 35px rgba(15, 23, 42, .10);
}

.verify-header {
    padding: 30px 25px;
    text-align: center;
    border-bottom: 1px solid #e5e7eb;
}

.verify-logo {
    width: 75px;
    height: 75px;
    object-fit: contain;
    margin-bottom: 12px;
}

.verify-header h1 {
    margin: 0 0 6px;
    font-size: 26px;
    color: #111827;
}

.verify-header p {
    margin: 0;
    color: #6b7280;
    font-size: 13px;
}

.verify-body {
    padding: 25px;
}


/* VALID */

.verify-valid {
    padding: 16px;
    margin-bottom: 22px;
    border-radius: 13px;
    background: #ecfdf5;
    color: #15803d;
    text-align: center;
    font-size: 17px;
    font-weight: 700;
}

.verify-valid i {
    margin-right: 7px;
}


/* INVALID */

.verify-invalid {
    padding: 35px 20px;
    text-align: center;
}

.verify-invalid-icon {
    width: 65px;
    height: 65px;
    margin: 0 auto 15px;
    border-radius: 50%;
    background: #fef2f2;
    color: #dc2626;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 28px;
}

.verify-invalid h2 {
    margin: 0 0 8px;
    color: #b91c1c;
}

.verify-invalid p {
    margin: 0;
    color: #6b7280;
    font-size: 13px;
}


/* DETAILS */

.verify-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
}

.verify-item {
    padding: 15px;
    border-radius: 11px;
    background: #f8fafc;
    border: 1px solid #edf0f3;
}

.verify-item span {
    display: block;
    margin-bottom: 5px;
    font-size: 10px;
    color: #6b7280;
    text-transform: uppercase;
    letter-spacing: .5px;
}

.verify-item strong {
    display: block;
    font-size: 14px;
    color: #111827;
}


/* CODE */

.verify-code {
    margin-top: 18px;
    padding: 17px;
    border: 2px dashed #16a34a;
    border-radius: 12px;
    background: #ecfdf5;
    text-align: center;
}

.verify-code span {
    display: block;
    margin-bottom: 6px;
    color: #6b7280;
    font-size: 10px;
    text-transform: uppercase;
}

.verify-code strong {
    color: #15803d;
    font-size: 19px;
    letter-spacing: 1px;
}


/* FOOTER */

.verify-footer {
    padding: 20px 25px;
    border-top: 1px solid #e5e7eb;
    text-align: center;
    color: #6b7280;
    font-size: 11px;
    line-height: 1.6;
}


@media (max-width: 600px) {

    body {
        padding: 15px 10px;
    }

    .verify-grid {
        grid-template-columns: 1fr;
    }

    .verify-header h1 {
        font-size: 21px;
    }

}

</style>

</head>


<body>


<div class="verify-wrapper">

<div class="verify-card">


    <div class="verify-header">

        <img
            src="assets/images/logo.png"
            class="verify-logo"
            alt="Complete Bliss"
        >

        <h1>
            Clearance Verification
        </h1>

        <p>
            Complete Bliss Scheme Clearance Verification
        </p>

    </div>


    <div class="verify-body">


    <?php if ($clearance): ?>


        <div class="verify-valid">

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

            VALID CLEARANCE

        </div>


        <div class="verify-grid">


            <div class="verify-item">

                <span>
                    Member
                </span>

                <strong>

                    <?= htmlspecialchars(
                        $clearance['fullname']
                    ); ?>

                </strong>

            </div>


            <div class="verify-item">

                <span>
                    Scheme
                </span>

                <strong>

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

                </strong>

            </div>


            <div class="verify-item">

                <span>
                    Number of Hands
                </span>

                <strong>

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

                </strong>

            </div>


            <div class="verify-item">

                <span>
                    Frequency
                </span>

                <strong>

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

                </strong>

            </div>


            <div class="verify-item">

                <span>
                    Clearance Date
                </span>

                <strong>

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

                </strong>

            </div>


            <div class="verify-item">

                <span>
                    Status
                </span>

                <strong style="color:#15803d;">

                    COMPLETED

                </strong>

            </div>


        </div>


        <div class="verify-code">

            <span>
                Verified Clearance Code
            </span>

            <strong>

                <?= htmlspecialchars(
                    $clearance['clearance_code']
                ); ?>

            </strong>

        </div>


    <?php else: ?>


        <div class="verify-invalid">


            <div class="verify-invalid-icon">

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

            </div>


            <h2>
                Invalid Clearance
            </h2>


            <p>

                No completed Complete Bliss clearance
                record matches this clearance code.

            </p>


        </div>


    <?php endif; ?>


    </div>


    <div class="verify-footer">

        This verification result is generated directly
        from the Complete Bliss clearance database.

    </div>


</div>

</div>


</body>

</html>