<?php
/* ============================================================
   my_position.php
   MY POSITION + 1/2/3 LEVEL TEAM
   COMPACT LAYOUT
============================================================ */

error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

session_start();

date_default_timezone_set('Africa/Nairobi');

require_once 'db.php';


/* ============================================================
   LOGIN CHECK
============================================================ */

$user_id = (int)($_SESSION['user_id'] ?? 0);

if ($user_id <= 0) {
    header("Location: login.php");
    exit;
}


/* ============================================================
   HELPERS
============================================================ */

function clean($value)
{
    return htmlspecialchars(
        (string)$value,
        ENT_QUOTES,
        'UTF-8'
    );
}


function maskPhone($phone)
{
    $phone = (string)$phone;

    if (strlen($phone) <= 4) {
        return $phone;
    }

    return '****' . substr($phone, -4);
}


function moneyFormat($amount)
{
    return number_format(
        (float)$amount,
        0,
        '.',
        ','
    );
}


/* ============================================================
   DATE FILTER
============================================================ */

$today = date('Y-m-d');

$start_date = isset($_GET['start_date'])
    ? trim($_GET['start_date'])
    : $today;

$end_date = isset($_GET['end_date'])
    ? trim($_GET['end_date'])
    : $today;


if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $start_date)) {
    $start_date = $today;
}

if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $end_date)) {
    $end_date = $today;
}


if ($start_date > $end_date) {

    $tmp = $start_date;

    $start_date = $end_date;

    $end_date = $tmp;
}


$date_start = $start_date . ' 00:00:00';
$date_end   = $end_date . ' 23:59:59';


/* ============================================================
   GET CURRENT USER
============================================================ */

$user_phone = '';

$stmt = $conn->prepare("
    SELECT
        id,
        phone
    FROM users
    WHERE id = ?
    LIMIT 1
");

$stmt->bind_param(
    "i",
    $user_id
);

$stmt->execute();

$result = $stmt->get_result();

if ($row = $result->fetch_assoc()) {
    $user_phone = $row['phone'] ?? '';
}

$stmt->close();


$masked_phone = maskPhone($user_phone);


/* ============================================================
   LEVEL 1
============================================================ */

$level1 = [];

$stmt = $conn->prepare("
    SELECT
        id,
        phone,
        created_at
    FROM users
    WHERE referred_by = ?
    ORDER BY created_at DESC
");

$stmt->bind_param(
    "i",
    $user_id
);

$stmt->execute();

$result = $stmt->get_result();

while ($row = $result->fetch_assoc()) {
    $level1[] = $row;
}

$stmt->close();


/* ============================================================
   LEVEL 2
============================================================ */

$level2 = [];

if (!empty($level1)) {

    $level1Ids = array_map(
        'intval',
        array_column($level1, 'id')
    );

    $placeholders = implode(
        ',',
        array_fill(
            0,
            count($level1Ids),
            '?'
        )
    );

    $types = str_repeat(
        'i',
        count($level1Ids)
    );

    $sql = "
        SELECT
            id,
            phone,
            created_at
        FROM users
        WHERE referred_by IN ($placeholders)
        ORDER BY created_at DESC
    ";

    $stmt = $conn->prepare($sql);

    $stmt->bind_param(
        $types,
        ...$level1Ids
    );

    $stmt->execute();

    $result = $stmt->get_result();

    while ($row = $result->fetch_assoc()) {
        $level2[] = $row;
    }

    $stmt->close();
}


/* ============================================================
   LEVEL 3
============================================================ */

$level3 = [];

if (!empty($level2)) {

    $level2Ids = array_map(
        'intval',
        array_column($level2, 'id')
    );

    $placeholders = implode(
        ',',
        array_fill(
            0,
            count($level2Ids),
            '?'
        )
    );

    $types = str_repeat(
        'i',
        count($level2Ids)
    );

    $sql = "
        SELECT
            id,
            phone,
            created_at
        FROM users
        WHERE referred_by IN ($placeholders)
        ORDER BY created_at DESC
    ";

    $stmt = $conn->prepare($sql);

    $stmt->bind_param(
        $types,
        ...$level2Ids
    );

    $stmt->execute();

    $result = $stmt->get_result();

    while ($row = $result->fetch_assoc()) {
        $level3[] = $row;
    }

    $stmt->close();
}


/* ============================================================
   COUNTS
============================================================ */

$countLevel1 = count($level1);
$countLevel2 = count($level2);
$countLevel3 = count($level3);

$totalMembers =
    $countLevel1 +
    $countLevel2 +
    $countLevel3;


/* ============================================================
   REFERRAL BONUS
============================================================ */

$referralBonus = 0;

$stmt = $conn->prepare("
    SELECT
        COALESCE(SUM(bonus_amount), 0) AS total
    FROM referral_bonuses
    WHERE user_id = ?
      AND created_at BETWEEN ? AND ?
");

$stmt->bind_param(
    "iss",
    $user_id,
    $date_start,
    $date_end
);

$stmt->execute();

$result = $stmt->get_result();

if ($row = $result->fetch_assoc()) {
    $referralBonus =
        (float)$row['total'];
}

$stmt->close();


/* ============================================================
   TEAM INCOME BY LEVEL
============================================================ */

$levelIncome = [
    1 => 0,
    2 => 0,
    3 => 0
];

$stmt = $conn->prepare("
    SELECT
        level,
        COALESCE(SUM(amount), 0) AS total
    FROM task_bonuses
    WHERE user_id = ?
      AND created_at BETWEEN ? AND ?
      AND level IN (1,2,3)
    GROUP BY level
");

$stmt->bind_param(
    "iss",
    $user_id,
    $date_start,
    $date_end
);

$stmt->execute();

$result = $stmt->get_result();

while ($row = $result->fetch_assoc()) {

    $level = (int)$row['level'];

    if (isset($levelIncome[$level])) {

        $levelIncome[$level] =
            (float)$row['total'];
    }
}

$stmt->close();


/* ============================================================
   LEVEL DATA
============================================================ */

$levels = [

    1 => [
        'members' => $level1,
        'count' => $countLevel1,
        'percentage' => '10%'
    ],

    2 => [
        'members' => $level2,
        'count' => $countLevel2,
        'percentage' => '3%'
    ],

    3 => [
        'members' => $level3,
        'count' => $countLevel3,
        'percentage' => '1%'
    ]

];

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

<head>

<meta charset="UTF-8">

<meta
    name="viewport"
    content="width=device-width,
    initial-scale=1,
    maximum-scale=1,
    user-scalable=no"
>

<title>My Position</title>


<style>

/* ============================================================
   RESET
============================================================ */

*{
    box-sizing:border-box;
    margin:0;
    padding:0;
}

html,
body{
    width:100%;
    min-height:100%;
}

body{
    background:#fff;
    color:#222;
    font-family:Arial,Helvetica,sans-serif;
    overflow-x:hidden;
}


/* ============================================================
   TOP BAR
============================================================ */

.topbar{
    width:100%;
    height:58px;
    background:#08765a;
    display:flex;
    align-items:center;
    justify-content:center;
    position:relative;
}

.top-title{
    color:#fff;
    font-size:19px;
    line-height:1;
    font-weight:700;
}

.back{
    position:absolute;
    left:48px;
    top:21px;
    width:13px;
    height:13px;
    border-left:2px solid #258fe5;
    border-bottom:2px solid #258fe5;
    transform:rotate(45deg);
    cursor:pointer;
}


/* ============================================================
   PAGE
============================================================ */

.page{
    width:100%;
    max-width:900px;
    margin:0 auto;
    padding:20px 48px 35px;
}


/* ============================================================
   POSITION CARD
============================================================ */

.position-card{
    width:100%;
    background:#eef9ef;
    border-radius:18px;
    padding:15px 22px 16px;
    min-height:185px;
}


/* ============================================================
   POSITION TOP
============================================================ */

.position-top{
    width:100%;
    display:flex;
    align-items:center;
    gap:20px;
    min-height:68px;
}


/* ============================================================
   PROFILE
============================================================ */

.profile{
    width:68px;
    height:68px;
    flex-shrink:0;
    border-radius:50%;
    background:#e8e8e8;
    display:flex;
    align-items:center;
    justify-content:center;
    overflow:hidden;
    position:relative;
}

.profile-head{
    width:22px;
    height:22px;
    border-radius:50%;
    background:#999;
    position:absolute;
    top:14px;
}

.profile-body{
    width:43px;
    height:27px;
    border-radius:30px 30px 10px 10px;
    background:#999;
    position:absolute;
    bottom:9px;
}


/* ============================================================
   POSITION TEXT
============================================================ */

.position-text{
    font-size:14px;
    line-height:1.35;
    color:#8a826f;
}

.position-name{
    color:#873e0e;
    font-weight:700;
}


/* ============================================================
   POSITION STATS
============================================================ */

.position-stats{
    display:grid;
    grid-template-columns:1fr 1fr;
    gap:12px;
    margin-top:14px;
}

.position-stat{
    min-height:59px;
    border-radius:11px;
    display:flex;
    flex-direction:column;
    align-items:center;
    justify-content:center;
    text-align:center;
}

.position-stat.members{
    background:#ffe9e9;
}

.position-stat.real{
    background:#e9f4ff;
}

.position-number{
    color:#ee5b62;
    font-size:19px;
    font-weight:700;
    margin-bottom:2px;
}

.position-label{
    font-size:11px;
    color:#666;
}


/* ============================================================
   LEVEL TITLE
============================================================ */

.section-title{
    margin-top:28px;
    display:flex;
    align-items:center;
    gap:8px;
    font-size:18px;
    font-weight:700;
}

.title-line{
    width:4px;
    height:27px;
    background:#f1b06a;
    border-radius:4px;
}


/* ============================================================
   SEARCH / CALENDAR
============================================================ */

.search-area{
    margin-top:25px;
    display:grid;
    grid-template-columns:1fr 1fr 1fr;
    gap:10px;
    align-items:center;
}

.date-input{
    width:100%;
    height:40px;
    border:1px solid #ddd;
    border-radius:7px;
    background:#fff;
    padding:0 7px;
    font-family:Arial,Helvetica,sans-serif;
    font-size:12px;
    color:#333;
    outline:none;
}

.search-btn{
    height:41px;
    border:0;
    border-radius:7px;
    background:#218de5;
    color:#fff;
    font-size:13px;
    cursor:pointer;
}

.search-btn:active{
    transform:scale(.98);
}


/* ============================================================
   LEVEL SECTIONS
============================================================ */

.level-section{
    width:100%;
    margin-top:31px;
}

.level-section:first-of-type{
    margin-top:32px;
}


/* ============================================================
   LEVEL HEADER
============================================================ */

.level-header{
    width:100%;
    display:flex;
    align-items:center;
    justify-content:space-between;
    margin-bottom:15px;
}


/* ============================================================
   LEVEL BUTTON
============================================================ */

.level-btn{
    width:145px;
    height:45px;
    border:0;
    border-radius:24px;
    background:#51b858;
    color:#fff;
    font-size:15px;
    font-weight:700;
    display:flex;
    align-items:center;
    justify-content:center;
}


/* ============================================================
   MEMBER BUTTON
============================================================ */

.member-btn{
    width:145px;
    height:43px;
    padding:0 12px;
    border:0;
    border-radius:24px;
    background:#ffc400;
    color:#9b6914;
    font-size:13px;
    cursor:pointer;
    display:flex;
    align-items:center;
    justify-content:center;
    gap:7px;
}


/* ============================================================
   PEOPLE ICON
============================================================ */

.people-icon{
    width:18px;
    height:15px;
    position:relative;
    display:inline-block;
}

.people-icon:before{
    content:"";
    position:absolute;
    left:1px;
    top:0;
    width:7px;
    height:7px;
    border:1.5px solid #fff;
    border-radius:50%;
}

.people-icon:after{
    content:"";
    position:absolute;
    left:0;
    bottom:0;
    width:12px;
    height:6px;
    border:1.5px solid #fff;
    border-radius:7px 7px 2px 2px;
}


/* ============================================================
   STATS GRID
============================================================ */

.stats-grid{
    width:100%;
    display:grid;
    grid-template-columns:1fr 1fr;
    gap:15px 25px;
}


/* ============================================================
   STAT CARD
============================================================ */

.stat-card{
    height:91px;
    border-radius:13px;
    display:flex;
    flex-direction:column;
    justify-content:center;
    padding:12px 25px;
}

.stat-card.revenue{
    background:#ffe9e9;
}

.stat-card.income{
    background:#edf9ef;
}

.stat-card.people{
    background:#eaf5ff;
}

.stat-card.referral{
    background:#fff9d9;
}


/* ============================================================
   STAT TEXT
============================================================ */

.stat-title{
    color:#666;
    font-size:14px;
    line-height:1.25;
    margin-bottom:5px;
    white-space:nowrap;
}

.stat-value{
    color:#222;
    font-size:17px;
    font-weight:700;
    line-height:1.2;
}


/* ============================================================
   MONEY ICON
============================================================ */

.money-icon{
    display:inline-flex;
    align-items:center;
    justify-content:center;
    width:20px;
    height:20px;
    margin-right:5px;
    font-size:13px;
    vertical-align:middle;
    line-height:1;
}


/* ============================================================
   PEOPLE SMALL
============================================================ */

.people-small{
    display:inline-block;
    width:20px;
    height:13px;
    position:relative;
    margin-right:5px;
    vertical-align:middle;
}

.people-small:before{
    content:"";
    position:absolute;
    width:8px;
    height:8px;
    left:1px;
    top:0;
    border-radius:50%;
    background:#4a486e;
    box-shadow:9px 1px 0 -1px #4a486e;
}

.people-small:after{
    content:"";
    position:absolute;
    width:12px;
    height:7px;
    left:0;
    bottom:0;
    border-radius:7px 7px 2px 2px;
    background:#4a486e;
    box-shadow:9px 0 0 -1px #4a486e;
}


/* ============================================================
   MEMBERS SECTION
============================================================ */

.members-section{
    width:100%;
    margin-top:11px;
    display:none;
    border-radius:10px;
    overflow:hidden;
    background:#fafafa;
    border:1px solid #eee;
}

.members-section.show{
    display:block;
}

.member-item{
    width:100%;
    min-height:46px;
    border-bottom:1px solid #eee;
    display:flex;
    align-items:center;
    justify-content:space-between;
    padding:6px 8px;
}

.member-item:last-child{
    border-bottom:0;
}

.member-left{
    display:flex;
    align-items:center;
    gap:8px;
}

.member-avatar{
    width:30px;
    height:30px;
    border-radius:50%;
    background:#f0f0f0;
    position:relative;
    flex-shrink:0;
}

.member-avatar:before{
    content:"";
    position:absolute;
    width:9px;
    height:9px;
    border-radius:50%;
    background:#aaa;
    top:5px;
    left:10px;
}

.member-avatar:after{
    content:"";
    position:absolute;
    width:18px;
    height:11px;
    border-radius:10px 10px 3px 3px;
    background:#aaa;
    bottom:5px;
    left:6px;
}

.member-phone{
    font-size:11px;
    font-weight:600;
    color:#333;
}

.member-date{
    font-size:9px;
    color:#999;
}

.empty-members{
    text-align:center;
    padding:16px 0;
    color:#bbb;
    font-size:11px;
}


/* ============================================================
   MOBILE
============================================================ */

@media(max-width:600px){

    .topbar{
        height:58px;
    }

    .top-title{
        font-size:19px;
    }

    .back{
        left:48px;
        top:21px;
    }

    .page{
        padding:21px 48px 32px;
    }

    .position-card{
        padding:15px 22px 16px;
        min-height:185px;
        border-radius:18px;
    }

    .position-top{
        gap:20px;
    }

    .profile{
        width:68px;
        height:68px;
    }

    .position-text{
        font-size:14px;
    }

    .position-stat{
        min-height:59px;
    }

    .section-title{
        margin-top:28px;
    }

    .search-area{
        margin-top:25px;
        gap:10px;
    }

    .level-section{
        margin-top:31px;
    }

    .level-btn{
        width:145px;
        height:45px;
        font-size:15px;
    }

    .member-btn{
        width:145px;
        height:43px;
        font-size:13px;
    }

    .stats-grid{
        gap:15px 25px;
    }

    .stat-card{
        height:91px;
        padding:12px 25px;
    }

    .stat-title{
        font-size:14px;
    }

    .stat-value{
        font-size:17px;
    }
}


/* ============================================================
   SMALL PHONES
============================================================ */

@media(max-width:390px){

    .topbar{
        height:54px;
    }

    .top-title{
        font-size:18px;
    }

    .back{
        left:29px;
        top:19px;
        width:12px;
        height:12px;
    }

    .page{
        padding:18px 25px 30px;
    }

    .position-card{
        padding:14px 18px 15px;
        min-height:166px;
        border-radius:17px;
    }

    .position-top{
        gap:16px;
        min-height:62px;
    }

    .profile{
        width:62px;
        height:62px;
    }

    .profile-head{
        width:20px;
        height:20px;
        top:13px;
    }

    .profile-body{
        width:39px;
        height:25px;
        bottom:8px;
    }

    .position-text{
        font-size:12px;
    }

    .position-stats{
        gap:9px;
        margin-top:12px;
    }

    .position-stat{
        min-height:52px;
        border-radius:10px;
    }

    .position-number{
        font-size:17px;
    }

    .position-label{
        font-size:10px;
    }

    .section-title{
        margin-top:25px;
        font-size:17px;
    }

    .title-line{
        height:24px;
    }

    .search-area{
        margin-top:23px;
        gap:6px;
    }

    .date-input{
        height:37px;
        font-size:10px;
        padding:0 4px;
    }

    .search-btn{
        height:38px;
        font-size:12px;
    }

    .level-section{
        margin-top:27px;
    }

    .level-header{
        margin-bottom:12px;
    }

    .level-btn{
        width:125px;
        height:40px;
        font-size:13px;
    }

    .member-btn{
        width:125px;
        height:38px;
        padding:0 8px;
        font-size:11px;
    }

    .people-icon{
        transform:scale(.85);
    }

    .stats-grid{
        gap:9px;
    }

    .stat-card{
        height:74px;
        padding:9px 11px;
        border-radius:10px;
    }

    .stat-title{
        font-size:10px;
        margin-bottom:3px;
    }

    .stat-value{
        font-size:14px;
    }

    .money-icon{
        width:17px;
        height:17px;
        margin-right:2px;
        font-size:11px;
    }

    .people-small{
        transform:scale(.75);
        transform-origin:left center;
        margin-right:0;
    }
}


/* ============================================================
   EXTRA SMALL PHONES
============================================================ */

@media(max-width:340px){

    .page{
        padding-left:18px;
        padding-right:18px;
    }

    .position-card{
        padding-left:14px;
        padding-right:14px;
    }

    .position-top{
        gap:12px;
    }

    .profile{
        width:58px;
        height:58px;
    }

    .position-text{
        font-size:11px;
    }

    .level-btn{
        width:112px;
        height:38px;
        font-size:12px;
    }

    .member-btn{
        width:112px;
        height:37px;
        font-size:10px;
    }

    .stats-grid{
        gap:8px;
    }

    .stat-card{
        height:70px;
        padding:8px;
    }

    .stat-title{
        font-size:9px;
    }

    .stat-value{
        font-size:13px;
    }
}

</style>

</head>


<body>


<!-- ============================================================
     TOP BAR
============================================================ -->

<header class="topbar">

    <div
        class="back"
        onclick="history.back()">
    </div>

    <div class="top-title">
        My Position
    </div>

</header>


<!-- ============================================================
     MAIN PAGE
============================================================ -->

<main class="page">


    <!-- ========================================================
         POSITION CARD
    ========================================================= -->

    <section class="position-card">


        <div class="position-top">


            <div class="profile">

                <div class="profile-head"></div>

                <div class="profile-body"></div>

            </div>


            <div class="position-text">

                Current position:

                <span class="position-name">
                    Funcionário
                    da VLM
                </span>

            </div>


        </div>


        <div class="position-stats">


            <div class="position-stat members">

                <div class="position-number">
                    <?php
                    echo $totalMembers;
                    ?>
                </div>

                <div class="position-label">
                    A total members
                </div>

            </div>


            <div class="position-stat real">

                <div class="position-label">
                    Real name
                </div>

            </div>


        </div>


    </section>


    <!-- ========================================================
         LEVEL TITLE
    ========================================================= -->

    <div class="section-title">

        <div class="title-line"></div>

        <div>
            Level
        </div>

    </div>


    <!-- ========================================================
         CALENDAR / SEARCH
    ========================================================= -->

    <form
        method="GET"
        class="search-area"
    >

        <input
            type="date"
            name="start_date"
            class="date-input"
            value="<?php
                echo clean($start_date);
            ?>"
        >


        <input
            type="date"
            name="end_date"
            class="date-input"
            value="<?php
                echo clean($end_date);
            ?>"
        >


        <button
            type="submit"
            class="search-btn"
        >
            Search
        </button>

    </form>


    <!-- ========================================================
         LEVELS
    ========================================================= -->

    <?php foreach ($levels as $levelNumber => $levelData): ?>


        <?php

        $members =
            $levelData['members'];

        $memberCount =
            (int)$levelData['count'];

        $percentage =
            $levelData['percentage'];

        $teamIncome =
            (float)$levelIncome[$levelNumber];

        /*
         * Keep revenue calculation based on
         * the existing data structure.
         */
        $levelRevenue =
            $teamIncome + $referralBonus;

        ?>


        <!-- ====================================================
             LEVEL SECTION
        ==================================================== -->

        <section
            class="level-section"
            id="level-<?php echo $levelNumber; ?>"
        >


            <!-- =================================================
                 LEVEL HEADER
            ================================================== -->

            <div class="level-header">


                <div class="level-btn">

                    <?php
                    echo $levelNumber;
                    ?>-Level

                </div>


                <button
                    type="button"
                    class="member-btn"
                    onclick="showMembers(
                        <?php
                        echo $levelNumber;
                        ?>
                    )"
                >

                    <span class="people-icon"></span>

                    member list

                </button>


            </div>


            <!-- =================================================
                 FOUR CARDS
            ================================================== -->

            <div class="stats-grid">


                <!-- =============================================
                     TOTAL REVENUE
                ============================================== -->

                <div class="stat-card revenue">

                    <div class="stat-title">
                        Total Revenue
                    </div>

                    <div class="stat-value">

                        <span class="money-icon">
                            💰
                        </span>

                        <?php
                        echo moneyFormat(
                            $levelRevenue
                        );
                        ?>

                    </div>

                </div>


                <!-- =============================================
                     TEAM INCOME
                ============================================== -->

                <div class="stat-card income">

                    <div class="stat-title">
                        Team income
                    </div>

                    <div class="stat-value">

                        <span class="money-icon">
                            💰
                        </span>

                        <?php
                        echo moneyFormat(
                            $teamIncome
                        );
                        ?>

                    </div>

                </div>


                <!-- =============================================
                     TOTAL PEOPLE
                ============================================== -->

                <div class="stat-card people">

                    <div class="stat-title">
                        Total people
                    </div>

                    <div class="stat-value">

                        <span class="people-small"></span>

                        (
                        <?php
                        echo $memberCount;
                        ?>
                        /
                        <?php
                        echo $memberCount;
                        ?>
                        )

                    </div>

                </div>


                <!-- =============================================
                     REFERRAL BONUS
                ============================================== -->

                <div class="stat-card referral">

                    <div class="stat-title">

                        Referral bonus
                        (<?php
                        echo clean($percentage);
                        ?>)

                    </div>

                    <div class="stat-value">

                        <span class="money-icon">
                            💳
                        </span>

                        <?php
                        echo moneyFormat(
                            $referralBonus
                        );
                        ?>

                    </div>

                </div>


            </div>


            <!-- =================================================
                 MEMBER LIST
            ================================================== -->

            <section
                class="members-section"
                id="members-<?php
                    echo $levelNumber;
                ?>"
            >


                <?php if (empty($members)): ?>


                    <div class="empty-members">

                        No members found.

                    </div>


                <?php else: ?>


                    <?php foreach ($members as $member): ?>


                        <div class="member-item">


                            <div class="member-left">


                                <div class="member-avatar"></div>


                                <div>

                                    <div class="member-phone">

                                        <?php

                                        echo clean(
                                            maskPhone(
                                                $member['phone'] ?? ''
                                            )
                                        );

                                        ?>

                                    </div>


                                    <div class="member-date">

                                        <?php

                                        echo clean(
                                            date(
                                                'Y-m-d',
                                                strtotime(
                                                    $member['created_at']
                                                )
                                            )
                                        );

                                        ?>

                                    </div>

                                </div>


                            </div>


                            <div class="member-date">

                                Level
                                <?php
                                echo $levelNumber;
                                ?>

                            </div>


                        </div>


                    <?php endforeach; ?>


                <?php endif; ?>


            </section>


        </section>


    <?php endforeach; ?>


</main>


<script>

/* ============================================================
   MEMBER LIST
============================================================ */

function showMembers(level){

    const box =
        document.getElementById(
            'members-' + level
        );

    if(!box){
        return;
    }


    /*
     * Close all other member lists
     */

    document
        .querySelectorAll('.members-section')
        .forEach(function(item){

            if(item !== box){
                item.classList.remove('show');
            }

        });


    /*
     * Toggle selected member list
     */

    if(box.classList.contains('show')){

        box.classList.remove('show');

    }else{

        box.classList.add('show');

        setTimeout(function(){

            box.scrollIntoView({
                behavior:'smooth',
                block:'nearest'
            });

        },50);

    }

}

</script>


</body>
</html>