<!-- Main Content -->

<style>
    @media (max-width:1075px) {

        .divblock {

            display: block !important;

        }



        .divblock img {

            height: 400px !important;

        }



        .card-2 {

            margin-top: 20px !important;

        }

    }

    .earnings-container {
        margin-bottom: 3rem;
    }

    .earnings-title {
        font-size: 1.25rem;
        margin-bottom: 0.5rem;
        color: #fff;
    }


    /* Progress Bar Styles */
    .progress-wrapper {
        background-color: #e9ecef;
        border-radius: 50px;
        /* Rounded look */
        height: 20px;
        width: 100%;
        margin: 1rem 0;
        overflow: hidden;
    }

    .progress-fill {
        background-color: #007bff;
        /* Primary Blue */
        height: 100%;
        transition: width 0.4s ease;
    }

    /* Layout Utils */
    .flex-between {
        display: flex;
        justify-content: space-between;
        font-weight: bold;
    }

    .text-muted {
        color: rgba(255, 255, 255, 0.6);
        font-size: 0.875rem;
        margin-top: 0.75rem;
        line-height: 1.4;
    }
</style>
<style>
    .lending-container {
        margin-bottom: 2rem;
    }

    .lending-title {
        font-weight: bold;
        margin-bottom: 1rem;
        color: #fff;
    }

    /* Labels above slider */
    .label-row {
        display: flex;
        justify-content: space-between;
        color: rgba(255, 255, 255, 0.6);
        font-size: 0.9rem;
        margin-bottom: 10px;
    }

    /* Custom Range Input Styling */
    .custom-range {
        -webkit-appearance: none;
        width: 100%;
        height: 8px;
        border-radius: 5px;
        background: #dee2e6;
        outline: none;
    }

    /* The Slider Handle (Thumb) */
    .custom-range::-webkit-slider-thumb {
        -webkit-appearance: none;
        appearance: none;
        width: 20px;
        height: 20px;
        border-radius: 50%;
        background: #007bff;
        cursor: pointer;
        border: 2px solid #fff;
        box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
    }

    .range-text {
        margin-top: 1.5rem;
        font-size: 1.1rem;
        color: #444;
    }

    .highlight {
        font-weight: 700;
    }

    .dashboard-grid {
        display: grid;
        grid-template-columns: 1fr 1fr;
        /* Two equal columns */
        gap: 1.5rem;
    }

    /* Responsive Logic: Mobile First */
    @media (max-width: 768px) {
        .dashboard-grid {
            grid-template-columns: 1fr;
            /* Stack them 100% width on mobile */
        }
    }
</style>
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.0/dist/chart.umd.js"></script>

<div style="padding-top: 140px; padding-bottom: 80px; min-height: 100vh;">

    <div class="container">

        <!-- Header -->

        <div class="text-center"> <?php if ($this->session->flashdata()) {

                                        echo Helpers::showFlash($this->session->flashdata("response"));
                                    } ?></div>

        <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 3rem; flex-wrap: wrap; gap: 1rem;">

            <div>

                <h1 style="font-size: 2.5rem; font-weight: 700; margin-bottom: 0.5rem;">Hello <?= $user['username'] ?></h1>

                <p style="color: rgba(255,255,255,0.7); font-size: 1.25rem;">Track your lending performance</p>

            </div>

            <div style="display: flex; gap: 1rem;">

                <a href="<?= base_url() ?>deposit" class="btn btn-primary">Deposit</a>

                <a href="<?= base_url() ?>withdrawal" class="btn btn-secondary">Withdraw</a>

            </div>

        </div>



        <!-- Stats Grid -->

        <div class="grid grid-4" style="margin-bottom: 3rem;">

            <div class="card" style="position: relative; overflow: hidden;">

                <div style="position: absolute; top: 0; right: 0; width: 128px; height: 128px; background: linear-gradient(135deg, var(--solana-purple), var(--solana-cyan)); opacity: 0.1; filter: blur(40px); border-radius: 50%;"></div>

                <div style="position: relative;">

                    <div style="display: flex; justify-content: space-between; margin-bottom: 1rem;">

                        <div>

                            <p style="color: rgba(255,255,255,0.6); font-size: 0.875rem; margin-bottom: 0.5rem;">Balance</p>

                            <h3 style="font-size: 2rem; font-weight: 700;"><?= MemberHelper::user_wallet(); ?> XRP</h3>

                        </div>

                        <!-- <div style="padding: 0.75rem; background: rgba(255,255,255,0.05); border-radius: 0.75rem; height: fit-content;">

                            

                        </div> -->

                    </div>

                    <!-- <div style="color: #14F195; font-size: 0.875rem; font-weight: 600;">+2.5%</div> -->

                </div>

            </div>



            <div class="card" style="position: relative; overflow: hidden;">

                <div style="position: absolute; top: 0; right: 0; width: 128px; height: 128px; background: linear-gradient(135deg, var(--solana-purple), var(--solana-cyan)); opacity: 0.1; filter: blur(40px); border-radius: 50%;"></div>

                <div style="position: relative;">

                    <div style="display: flex; justify-content: space-between; margin-bottom: 1rem;">

                        <div>

                            <p style="color: rgba(255,255,255,0.6); font-size: 0.875rem; margin-bottom: 0.5rem;">Monthly Earnings</p>

                            <h3 style="font-size: 2rem; font-weight: 700;"><?php $bal = MemberHelper::user_wallet();

                                                                            $earnings = $bal * 0.10;

                                                                            echo $earnings;

                                                                            ?> XRP</h3>

                        </div>

                        <!-- <div style="padding: 0.75rem; background: rgba(255,255,255,0.05); border-radius: 0.75rem; height: fit-content;">

                            

                        </div> -->

                    </div>

                    <!-- <div style="color: #14F195; font-size: 0.875rem; font-weight: 600;">+12.3%</div> -->

                </div>

            </div>



            <div class="card" style="position: relative; overflow: hidden;">

                <div style="position: absolute; top: 0; right: 0; width: 128px; height: 128px; background: linear-gradient(135deg, var(--solana-purple), var(--solana-cyan)); opacity: 0.1; filter: blur(40px); border-radius: 50%;"></div>

                <div style="position: relative;">

                    <div style="display: flex; justify-content: space-between; margin-bottom: 1rem;">

                        <div>

                            <p style="color: rgba(255,255,255,0.6); font-size: 0.875rem; margin-bottom: 0.5rem;">Today Earnings</p>

                            <h3 style="font-size: 2rem; font-weight: 700;"><?= MemberHelper::user_daily_earnings(); ?> XRP</h3>

                        </div>

                        <!-- <div style="padding: 0.75rem; background: rgba(255,255,255,0.05); border-radius: 0.75rem; height: fit-content;">

                            ⚡

                        </div> -->

                    </div>

                    <!-- <div style="color: rgba(255,255,255,0.6); font-size: 0.875rem;">Last 24 hours</div> -->

                </div>

            </div>

        </div>



        <!-- Chart and Activity -->
        <div class="dashboard-grid">
            <div class="earnings-container">
                <div class="card">
                    <h5 class="earnings-title">Earnings</h5>
                    <?php
                    $raw_earnings = MemberHelper::get_user_business();
                    $earnings = round($raw_earnings, 2);
                    $max = 50000;

                    // Logic to prevent the bar from exceeding 100%
                    $percentage = ($earnings / $max) * 100;
                    if ($percentage > 100) $percentage = 100;
                    ?>

                    <div class="progress-wrapper">
                        <div class="progress-fill" style="width: <?= $percentage ?>%"></div>
                    </div>

                    <div class="flex-between">
                        <span><?= $earnings; ?> XRP</span>
                        <span style="color:#fff; font-weight: normal;"><?= $max; ?> XRP</span>
                    </div>

                    <div class="text-muted">
                        Earnings are calculated daily and added to your balance automatically.
                    </div>
                </div>
            </div>


            <div class="lending-container">
                <div class="card">
                    <h5 class="lending-title">Try adjusting your lending amount</h5>

                    <div class="label-row">
                        <span>1,000 XRP</span>
                        <span>3,000,000 XRP</span>
                    </div>

                    <input type="range" class="custom-range" min="1000" max="3000000" step="1000" value="10000" id="lendingSlider">

                    <p class="range-text">
                        You’d earn <span class="highlight gradient-text" id="sliderEarnings">500</span> XRP monthly
                        with <span class="highlight gradient-text" id="sliderValue">10,000</span> XRP
                    </p>
                </div>
            </div>
        </div>
        <div class="divblock" style="display: grid; grid-template-columns: 2fr 1fr; gap: 2rem; margin-bottom: 3rem;">

            <!-- Chart -->

            <!-- <div class="card">

                <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 2rem;">

                    <h2 style="font-size: 1.5rem; font-weight: 700;">Earnings Overview</h2>



                </div>



                <div style="height: 400px; position: relative;">

                    <?php

                    if (count($data['daily_growth']) >= 7) { ?>

                        <canvas id="earningsChart"></canvas>

                    <?php   } else { ?>

                        <img src="<?= base_url() ?>website/img/chart.jpg" style="width:100%;filter:blur(7px)" alt="">

                        <h5 class="text-center  fw-bold" style="    top: 50%;   left: 50%; width:90% !important;   transform: translate(-50%, -50%);  position: absolute;">This chart will be visible once sufficient data is available.</h5>

                    <?php } ?>

                </div>



               

            </div> -->



            <div class="card" style="text-align: center; background: linear-gradient(135deg, rgba(153, 69, 255, 0.1), rgba(0, 212, 255, 0.1)); border: 1px solid rgba(153, 69, 255, 0.3);">

                <div style="font-size: 3.5rem; font-weight: 700; margin-bottom: 0.5rem;" class="gradient-text">2%</div>

                <div style="color: rgba(255,255,255,0.8); margin-bottom: 1rem; font-size: 1.125rem;">Monthly earnings</div>

                <div style="color: rgba(255,255,255,0.6); font-size: 0.875rem;">Your estimated annual percentage yield based on current network rewards</div>

            </div>



            <div class="card card-2">

                <h2 style="font-size: 1.5rem; font-weight: 700; margin-bottom: 1.5rem; color: #fff;">Recent Activity</h2>



                <div style="max-height: 450px; overflow-y: auto; padding-right: 5px;">

                    <?php $i = 1;

                    if (!empty($data['daily_growth'])) {



                        foreach ($data['daily_growth'] as $key => $row) {

                    ?>

                            <div style="display: flex; align-items: center; padding: 1rem; background: rgba(255,255,255,0.05); border-radius: 1rem; margin-bottom: 0.75rem; transition: background 0.3s; border: 1px solid rgba(255,255,255,0.02);" onmouseover="this.style.background='rgba(255,255,255,0.1)'" onmouseout="this.style.background='rgba(255,255,255,0.05)'">



                                <div style="width: 42px; height: 42px; background: rgba(20, 241, 149, 0.1); border-radius: 12px; display: flex; align-items: center; justify-content: center; margin-right: 1rem; flex-shrink: 0;">

                                    <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#14F195" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">

                                        <polyline points="23 6 13.5 15.5 8.5 10.5 1 18"></polyline>

                                        <polyline points="17 6 23 6 23 12"></polyline>

                                    </svg>

                                </div>



                                <div style="flex: 1; min-width: 0;">

                                    <div style="font-size: 0.95rem; font-weight: 500; color: #fff; margin-bottom: 0.2rem;">

                                        You have earned <span style="color: #14F195; font-weight: 600;">+<?= round($row['amount'], 3); ?> XRP</span>

                                    </div>

                                    <div style="font-size: 0.8rem; color: rgba(255,255,255,0.5);">

                                        <?= Helpers::date($row['created_at']); ?>

                                    </div>

                                </div>

                            </div>



                        <?php $i++;
                        }
                    } else { ?>

                        <div style="padding: 2rem; text-align: center; background: rgba(255,255,255,0.03); border-radius: 1rem; color: rgba(255,255,255,0.5); font-size: 0.9rem;">

                            No activity yet — once you make your first deposit, daily earnings will begin showing up within 24 hours.

                        </div>

                    <?php } ?>

                </div>

            </div>

        </div>



        <!-- APY Card -->



    </div>

</div>



<?php

usort($data['daily_growth'], function ($a, $b) {

    return strtotime($a['created_at']) <=> strtotime($b['created_at']);
});

$labels = [];

$amounts = [];



foreach ($data['daily_growth'] as $row) {

    $labels[]  = date('d M Y', strtotime($row['created_at']));

    $amounts[] = $row['amount'];
}

?>



<script>
    // Initialize chart

    const ctx = document.getElementById('earningsChart').getContext('2d');

    const chart = new Chart(ctx, {

        type: 'line',

        data: {

            labels: <?= json_encode($labels) ?>,

            datasets: [{

                label: 'Earnings (XRP)',

                data: <?= json_encode($amounts) ?>,

                borderColor: '#9945FF',

                backgroundColor: 'rgba(153, 69, 255, 0.1)',

                borderWidth: 3,

                fill: true,

                tension: 0.4

            }]

        },

        options: {

            responsive: true,

            maintainAspectRatio: false,

            plugins: {

                legend: {

                    display: false

                }

            },

            scales: {

                y: {

                    beginAtZero: false,

                    grid: {

                        color: 'rgba(255, 255, 255, 0.05)'

                    },

                    ticks: {

                        color: '#999'

                    }

                },

                x: {

                    grid: {

                        color: 'rgba(255, 255, 255, 0.05)'

                    },

                    ticks: {

                        color: '#999'

                    }

                }

            }

        }

    });



    function updateChart(range) {

        // This would update the chart with different data based on range

        showNotification('Chart updated to ' + range + ' view');

    }
</script>
<script>
    const slider = document.getElementById('lendingSlider');
    const earnings = document.getElementById('sliderEarnings');
    const amount = document.getElementById('sliderValue');

    slider.addEventListener('input', () => {
        const value = parseInt(slider.value);
        amount.textContent = value.toLocaleString() + ' XRP';
        earnings.textContent = (value * 0.02).toLocaleString(undefined, {
            maximumFractionDigits: 0
        }) + ' XRP';
    });
</script>
<script>
    // Set the earnings value here
    const earningsXRP = <?= round(MemberHelper::get_user_business(), 2); ?>; // example: 50,000 XRP

    // Calculate and apply progress
    const maxXRP = 100000;
    const percentage = (earningsXRP / maxXRP) * 100;
    document.getElementById('earningsProgress').style.width = percentage + '%';
</script>