<?php
session_start();
include '../config/db.php';
include '../config/config.php';
include '../config/functions.php';

if(isset($_SESSION['USERLOGIN'])){
    $userLogin = $_SESSION['USERLOGIN'];
    $query = mysqli_query($link, "SELECT * FROM users WHERE refcode = '$userLogin' ");
    if(mysqli_num_rows($query)>0){
        $row = mysqli_fetch_assoc($query);
        $userId = $row['id'];
        $fname = $row['fname'];
        $lname = $row['lname'];
        $password = $row['password'];
        $email = $row['email'];
        $phone = $row['phone'];
        $gender = $row['gender'];
        $country = $row['country'];
        $address = $row['address'];
        $occupation = $row['occupation'];
        $refcode = $row['refcode'];
        $ref_balance = $row['ref_balance'];
        $profit = $row['profit'];
        $balance = $row['balance'];
        $bonus = $row['bonus'];
        $id_photo = $row['Id_photo'];
        $profile_photo = $row['profile_photo'];
        $pin = $row['pin'];
        $referral = $row['referral'];
        $date = $row['date'];
        $status = $row['status'];

    }
    
}else{
    echo "<script>window.location.href = 'login.php' </script>";
}


function getCryptoValueByShortCode($cryptoShortCode, $amountInUSD) {

    $apiUrl = "https://min-api.cryptocompare.com/data/pricemulti?fsyms=$cryptoShortCode&tsyms=usd";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $apiUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $response = curl_exec($ch);
    curl_close($ch);
    $data = json_decode($response, true);

    if (isset($data[$cryptoShortCode]["usd"])) {
        $cryptoPrice = $data[$cryptoShortCode]["usd"];
        return $amountInUSD / $cryptoPrice; // Convert USD to crypto amount
    } else {
        return 0;
    }
}

$sql_qry = "SELECT SUM(amount) AS counter FROM deposits WHERE userID = '$userId' ";
if($duration = $link->query($sql_qry)){
    while($record = $duration->fetch_array()){
        $total_deposit = $record['counter'];
    }
}else{
    $total_deposit = 0  ;
}

?>