<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Assuming you have a Database class managing the connection

$id = md5($user->id);

if (isset($_GET[$id]) && $_GET[$id] != '') {
    $amount = intval(base64_decode($_GET[$id]));
    echo $amount;
    if (is_int($amount)) {
        # code...
        echo $amount;
    }
    $message = notification('success', "Your Wallet was Successfuly Credited with <strong>N" . number_format($amount) . "</strong>");
}

?>

 <?php
class Database2
{
    private $host = '67.225.140.44';
    private $username = 'magadata_Aayareemah';
    private $password = '_mkR^x0?*vAR';
    private $database = 'magadata_Magadata';
    private $conn;

    public function __construct()
    {
        $this->conn = new mysqli($this->host, $this->username, $this->password, $this->database);

        if ($this->conn->connect_error) {
            die("Connection failed: " . $this->conn->connect_error);
        }
    }

    public function getConnection()
    {
        return $this->conn;
    }
}


// Create an instance of the Database class
$db = new Database2();

// Get the database connection from the instance
$conn = $db->getConnection();

// Fetch user's virtual accounts from the database
$user_id = isset($user_id) ? $user_id : $_SESSION['user_id'];

$virtual_accounts_query = "SELECT * FROM account WHERE user_id = ?";
$virtual_accounts_stmt = $conn->prepare($virtual_accounts_query);


if (!$virtual_accounts_stmt) {
    die("Error preparing virtual accounts statement: " . $conn->error);
}

$virtual_accounts_stmt->bind_param("i", $user_id);
$virtual_accounts_stmt->execute();

$virtual_accounts_result = $virtual_accounts_stmt->get_result();

if ($virtual_accounts_stmt->error) {
    die("Error executing virtual accounts statement: " . $virtual_accounts_stmt->error);
} 

// ... Rest of the code

// Fetch and decode all virtual account details
$virtual_accounts = array();
while ($row = $virtual_accounts_result->fetch_assoc()) {
    $account_detail_json = $row['account_detail'];

    $account_detail = json_decode($account_detail_json, true);

    if (empty($account_detail_json)) {
        echo "Error: The 'account_detail' column is empty<br>";
    } else {
        // Attempt to decode JSON data with error handling
        $account_detail = json_decode($account_detail_json, true);

        if (json_last_error() !== JSON_ERROR_NONE) {
            // Print JSON decoding error
            echo "Error decoding JSON data: " . json_last_error_msg() . "<br>";
        } else {
            // Check if decoding is successful and 'requestSuccessful' is true
            if ($account_detail && $account_detail['requestSuccessful']) {
                $virtual_accounts[] = $account_detail['responseBody'];
            }
        }
    }
}

$virtual_accounts_stmt->close();

?>

<style>
.warning {
    border: 1px solid red !important;
    background-color: #ffaabb;
    color: #fff;
}
</style>
<section id="main-content">
    <section class="wrapper">
        <div class="row">
            <div class="col-lg-12 main-chart">
                <!--CUSTOM CHART START -->
                <div class="border-head">
                    <h3>FUND WALLET</h3>
                </div>
                <?php

                // print_r($_SESSION);

                ?>

                <div class="row mt-1">
                    <div class="col-lg-3">

                    </div>
                    <div class="col-lg-6">
                        <div
                            class="py-4 px-4 rounded-xl shadow-soft-lg backdrop-saturate-[200%] backdrop-blur-[30px] bg-[hsla(0,0%,100%,0.8)] shadow-blur">
                            <h2 class="mb-2"><i class="fa fa-angle-right"></i> Fund Your Wallet</h2>
                            <span class="msg"><?= isset($message) ? $message : "" ?></span>




                            <div class="flex-auto p-2 pt-0 text-center">
                                <h3 class="mb-3 text-center">Generate Virtual acc. for better transaction</h3>
                                <?= notification("warning", "BVN updated successfully. Click the button below to Generate your virtual acc.") ?>

                                <hr
                                    class="h-px my-3 bg-transparent bg-gradient-to-r from-transparent via-black/40 to-transparent" />
                            
                                <button id="virtual_btn"
                                    class="mb-2 mx-2 inline-block px-6 py-4 font-bold text-center text-white uppercase align-middle transition-all bg-transparent rounded-lg cursor-pointer leading-pro ease-soft-in shadow-soft-md bg-150 bg-gradient-to-tl from-gray-900 to-slate-800 hover:shadow-soft-xs active:opacity-85 hover:scale-102 tracking-tight-soft bg-x-25"> 
                                    <i class="fa fa-wallet"></i>&nbsp;&nbsp;Generate Virtual Account</button>
                        
                                <hr
                                    class="h-px my-4 bg-transparent bg-gradient-to-r from-transparent via-black/40 to-transparent" />
                            </div>

                                                     <br>
                            <div style="display: none;" id="transfer">
                                <?=
                                notification("light", "<h3 class='text-info'>You can pay directly to our Account</h3>
                                <br>
                                <br>
                                <b>KudaBank</b> <br> 
                                Account Name: <b>Yamala And Brothers Global Ventures</b><br>
                                Account Number: <b>3000428366</b>
                                <hr>
                                <br>
                                <b>Opay</b> <br> 
                                Account Name: <b>Yamala Mohammed Umar</b><br>
                                Account Number: <b>6111166252
                                <hr>
                                <span class='text-danger'> Send proof of payment with email address for instant funding to Whatsapp Group.</span>
                                 ")

                                ?>


                            </div>


                            <div style="display: none;" id="virtual" class="flex-auto p-3 pt-4 text-center ">
                                <h3 class='text-info'>Transfer The Amount you want to fund your wallet with, to your
                                    provided
                                    virtual Account.</h3>
                                <hr
                                    class=" my-4 bg-transparent bg-gradient-to-r from-transparent via-black/40 to-transparent" />
                                <h4 class=" mb-0"><span class="opacity-80">Accounts Name: </span>
                                    YAMALA AND
                                BROTHERS GLOBAL VENTURES</h4>
                                <hr>

                                <?php if (!empty($virtual_accounts)) : ?>
        <?php foreach ($virtual_accounts as $account) : ?>
            <h4 class="mb-0"><span class="opacity-80">Account Name: </span><?= $account['accountName'] ?></h4>
            <h4 class="mb-0"><span class="opacity-80">Bank Name: </span><?= $account['bankName'] ?></h4>
            <h4 class="mb-0"><span class="opacity-80">Account Number: </span><?= $account['accountNumber'] ?></h4>
            <hr>
        <?php endforeach; ?>
    <?php else : ?>
        <p class="text-red-500">No virtual account details available.</p>
    <?php endif; ?>
                            </div>

                        </div>
                        <!-- /form-panel -->
                    </div>
                    <div class="col-lg-3"></div>

                </div>

            </div>

        </div>
        <!-- /row -->
    </section>
</section>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<script>
$("#virtual_btn").click(function() {
    $.ajax({
        url: "../../payment/monnify.php",
        type: "GET",
        success: function(response) {
            // Handle the response if needed
            console.log(response);
        },
        error: function(error) {
            // Handle errors if needed
            console.error(error);
        }
    });
});
</script>

<script>
$("#online_btn").click(function() {
    $("#online").slideToggle()
    $("#transfer").slideUp()
    $("#virtual").slideUp()
})
$("#transfer_btn").click(function() {
    $("#transfer").slideToggle()
    $("#online").slideUp()
    $("#virtual").slideUp()
})
$("#virtual_btn").click(function() {
    $("#virtual").slideToggle()
    $("#online").slideUp()
    $("#transfer").slideUp()
})

$("#amount").on("change keyup paste click", function() {

    var amount = parseInt($('#amount').val())
    // console.log(amount)
    if (!isNaN(amount)) {
        if (amount < 500) {
            $(this).addClass('warning')
            $('#pay_btn').prop('disabled', true)
        } else {
            $('#pay_btn').prop('disabled', false)
            $(this).removeClass('warning')
        }
    } else {
        $(this).addClass('warning')
        $('#pay_btn').prop('disabled', true)
    }
})
</script>