<?php include "includes/header_logged.php" ;
ini_set('allow_url_include', 1);
        // echo "Sorry Site Under Maintenance";
        // return false;
?>

<?php extract($_GET);
$page  = isset($page) ? $page : 'dashboard';


// $account = Account::find_by_query("SELECT * FROM account WHERE  user_id = $user->id LIMIT 1");
$account = array();
// $account = Account::find_by_query("SELECT * FROM account WHERE  user_id = $user->id LIMIT 1");
// $account = array_shift($account);

$RequestRef = rand(0000000000000, 9999999999999);
$trackingReference = rand(00000000000, 99999999999);
$middle_name = explode(' ', $user->last_name)[1] ?? '';

if (!$account) {
    try {
        $curl = curl_init();

        curl_setopt_array($curl, array(
            // CURLOPT_URL => 'https://kuda-openapi.kuda.com/v2/Account/GetToken',
            CURLOPT_URL => 'https://kuda-openapi.kuda.com/v2.1/Account/GetToken',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => '{
        "email": "yamala49@gmail.com",
        "apiKey": "t9Qovs8XJj3rq4wexGi"
    }',
            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/json'
            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        // print_r($response);

        try {

            $curl = curl_init();

            curl_setopt_array($curl, array(
                CURLOPT_URL => 'https://kuda-openapi.kuda.com/v2.1',
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => '{"ServiceType": "ADMIN_CREATE_VIRTUAL_ACCOUNT",
                                        "RequestRef":"' . $RequestRef . '",
                                        "Data":{
                                            "email":"' . $user->email . '",
                                            "phoneNumber":"' . $user->phone . '",
                                            "lastName":"' . explode(' ', $user->last_name)[0]  . '",
                                            "firstName":"' . explode(' ', $user->first_name)[0]  . '",
                                            "middleName":"' .  $middle_name . '",
                                            "businessName":"VerifyAll - ' . $user->business_name . '",
                                            "trackingReference":"' . $trackingReference . '"}
                                    }',
                CURLOPT_HTTPHEADER => array(
                    'Content-Type: application/json',
                    'Authorization: Bearer ' . $response,
                    'Accept: application/json'
                ),
            ));

            $response2 = json_decode(curl_exec($curl));

            curl_close($curl);
            // print_r($response2);

            if (isset($response2->status) && $response2->status) {


                $new_account = new Account;
                $new_account->user_id = $user->id;
                $new_account->trackingReference = $trackingReference;
                $new_account->accountName = "VerifyAll - $user->business_name";
                $new_account->accountNumber = $response2->data->accountNumber;

                $new_account->save();
            }
        } catch (\Throwable $th) {
            //throw $th;
            print_r($th);
        }
    } catch (\Throwable $th) {
        print_r($th);
    };
} else {
    // print_r($account);
}

// print_r($_GET);
?>
<script>
    function wallet_balance() {
        var balance = 0;


        $.ajax({
            async: false,
            method: "GET",
            url: "<?=ROOT_URL?>/main_includes/components/get_wallet_bal.php",
            success: function(response, balance) {
                // console.log(response)
                $("#wallet_balance").text("N " + response)
                // console.log(parseInt(response))

            }
        }).done(function(data) {
            // console.log(parseInt(data))

            balance = parseInt(data)
        })

        // console.log(balance)
        return balance

    }
    $(document).ready(function() {

        wallet_balance()
    })
</script>

<body class="bg-white">

    <!-- sidenav  -->
    <?php include "includes/sidenav.php" ?>

    <!-- end sidenav -->
    <!-- <main class="ease-soft-in-out xl:ml-68.5 relative h-full max-h-screen rounded-xl transition-all duration-200"> -->

        <?php  include "includes/topnav.php"?>

        <?php
       
        if (isset($end_service) && $end_service != '') {
            file_exists("includes/pages_content/$end_service.php") && isset($end_service) ?  include "includes/pages_content/$end_service.php" :  include "includes/pages_content/$sub_service.php";
        } else if (isset($sub_service) && $sub_service != '') {
            file_exists("includes/pages_content/$sub_service.php") && isset($sub_service) ?  include "includes/pages_content/$sub_service.php" :  include "includes/pages_content/$page.php";
        } else {

            file_exists("includes/pages_content/$page.php") && isset($page) ?  include "includes/pages_content/$page.php" :  include "includes/pages_content/dashboard.php";
        }
        ?>
        <?php include "includes/footer.php" ?>
        <script src="<?=ROOT_URL?>/assets/datatables.min.js"></script>