<?php session_start();
require "../database/connect.php";
$user_id = $_SESSION['user_id'];
$db_sql = "SELECT * FROM users WHERE user_id = '$user_id'";
$db_result = $connection->query($db_sql);
while ($row = $db_result->fetch_assoc()) {
    $role = $row['role'];
    $full_name = $row['full_name'];
    $username = $row['username'];
    $bonus = $row['bonus'];
    $profit = $row['profit'];
    $btc = $row['btc'];
    $eth = $row['eth'];
    $usdt = $row['usdt'];
    $ltc = $row['ltc'];
    $refferal = $row['refferal'];
    $email = $row['email'];
    $country = $row['country'];
    $phone = $row['phone'];
    $address = $row['address'];
    $city = $row['city'];
    $zip = $row['zip'];
    $currency = $row['currency'];
    $password = $row['password'];
    $image = $row['image'];
    $status = $row['status'];
    $reg_date = $row['reg_date'];
    $kyc_status = $row['kyc_status'];
    
    $upgrade = $row['upgrade'];
    $Withdrawal_Pins = $row['Withdrawal_Pin'];

    $balance = $btc + $eth + $usdt + $ltc + $bonus + $profit;

    $d_btc = number_format("$btc",2);
    $d_eth = number_format("$eth",2);
    $d_usdt = number_format("$usdt",2);
    $d_ltc = number_format("$ltc",2);
    $dbonus = number_format("$bonus",2);
    $dprofit = number_format("$profit",2);
    $dbalance = number_format("$balance",2);
}

// Extract the first name
$fname = explode(' ', $full_name)[0];
// Extract the last name
$lname = explode(' ', $full_name)[1];


  if ($currency == "USD") {
      $currency_code = "$";
  }elseif ($currency == "GBP") {
      $currency_code = "£";
  }elseif ($currency == "EUR") {
      $currency_code = "€";
  }elseif ($currency == "YEN") {
      $currency_code = "¥";
  }elseif ($currency == "INR") {
      $currency_code = "₹";
  }


  // Get the user's IP address
  $userIP = $_SERVER['REMOTE_ADDR'];

  //TOTAL DEPOSIT
  $sqldeposit = "SELECT SUM(transaction_amount) AS count FROM transaction WHERE transaction_type='deposit'  AND transaction_status='success' AND transaction_user_id='$user_id'";
  $duration_dep = $connection->query($sqldeposit);
  while ($record_dep = $duration_dep->fetch_assoc()) {
      $total_dep = $record_dep[ 'count' ];
      	if ( empty( $total_dep ) ) {
      $total_deposit = '0.00';
  } else {
      $total_deposit = number_format( "$total_dep", 2 );
  }
  }

  //TOTAL Active Invested
  $sqldep = "SELECT SUM(amount) AS count FROM invest_plan WHERE status='active' AND invest_user_id='$user_id'";
  $inv_result = $connection->query($sqldep);
  while ($inv_record = $inv_result->fetch_assoc()) {
      $active_invested = $inv_record[ 'count' ];

  }

  //TOTAL Completed Invested
  $sqlinvest = "SELECT SUM(amount) AS count FROM invest_plan WHERE status='completed' AND invest_user_id='$user_id'";
  $inv_complete = $connection->query($sqlinvest);
  while ($complete_record = $inv_complete->fetch_assoc()) {
      $complete_invested = $complete_record[ 'count' ];

  }

  $total_invested = $complete_invested + $active_invested;




  if ($status == "pending") {
      echo "<script>
              alert('Sorry your account info has not been approved. Please check your mail to verify your account')
              window.location.href = '.logout.php'
          </script>";
  }
  if ($status == 'suspend') {
      echo "<script>
              alert('Sorry your account has been suspended. Please contact our customer service for more info')
              window.location.href = 'logout.php'
          </script>";
  }
  if ($status == 'inactive') {
      echo "<script>
              alert('Sorry your account is inactive at the moment. Please contact our customer service for more info about this error. Thank you')
              window.location.href = 'logout.php'
          </script>";
  }
