<?php
require("../includes/db_connect.php");
require_once("includes/admin_session_token.php");

$accountId = $_SESSION['adm_accountId'];

$pageno = 1;

$sql = "SELECT * FROM admin_users WHERE accountId = :accountId LIMIT 1";
$sth = $conn->prepare($sql, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
$sth->execute([':accountId' => $accountId]);
$users = $sth->fetchAll();

foreach ($users as $key => $value) {
  $accountId = $value['accountId'];

  $_SESSION['usr_accountId'] = $accountId;
}


$total_bonus_sql = "SELECT SUM(total_bonus) from bonus_account_balances";
$get_total_total_bonus = $conn->prepare($total_bonus_sql);
$get_total_total_bonus->execute();
$total_bonus_balance = $get_total_total_bonus->fetch(PDO::FETCH_NUM);
$total_bonus = $total_bonus_balance[0];
$total_total_bonus = number_format($total_bonus, 2);


$withdrawn_sql = "SELECT SUM(withdrawn) from bonus_account_balances";
$get_total_withdrawn = $conn->prepare($withdrawn_sql);
$get_total_withdrawn->execute();
$withdrawn_balance = $get_total_withdrawn->fetch(PDO::FETCH_NUM);
$withdrawn = $withdrawn_balance[0];
$total_withdrawn = number_format($withdrawn, 2);


$balance_calc = $total_bonus - $withdrawn;

$total_balance  = number_format($balance_calc, 2);


$url = "https://blockchain.info/ticker";
$jsonData = json_decode(file_get_contents($url), true);
$exchangeRate = $jsonData['USD']['last'] ?? 1;

$btc_total_bonus = round(($total_bonus / $exchangeRate), 8);
$btc_withdrawn = round(($withdrawn / $exchangeRate), 8);
$btc_balance_calc = round(($balance_calc / $exchangeRate), 8);



$sql = "SELECT bonus_account_transaction_history.*, users.accountId, users.email FROM bonus_account_transaction_history INNER JOIN users ON bonus_account_transaction_history.accountId = users.accountId ORDER BY bonus_account_transaction_history.date DESC LIMIT 10";
$sth = $conn->prepare($sql, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
$sth->execute();
$history = $sth->fetchAll();

$transactions_history = "";

if (count($history) > 0) {
  foreach ($history as $key => $value) {
    $id = $value['id'];
    $accountId = $value['accountId'];
    $email = $value['email'];
    $name = $value['name'];
    $type = $value['type'];
    $amount = $value['amount'];
    $status = $value['status'];
    $date = $value['date'];

    $dtf = new DateTime($date);
    $dis_date = $dtf->format('d/m/Y');

    $dis_name = ucfirst($name);
    $dis_action = ucfirst($type);

    if ($type == "ROI") {
      $dis_amount = round($amount, 2).'%';
    }else if ($type == "Cancelled") {
      $dis_amount = '&#36;'.number_format($amount, 2);
    }else {

      if ($status == 1){

        $dis_amount = '+'.'&#36;'.number_format($amount, 2);
      }else {

        $dis_amount = '-'.'&#36;'.number_format($amount, 2);
      }

    }

    if ($status == 1) {

      $transactions_history.="
      <tr>

      <td>$accountId</td>
      <td>$email</td>
      <td>$dis_name</td>

      <td>
      <span class='primary-color'>$dis_amount</span>
      </td>

      <td>$dis_action</td>
      <td>$dis_date</td>

      </tr>
      ";

    }else {
      $transactions_history.="
      <tr>

      <td>$accountId</td>
      <td>$email</td>
      <td>$dis_name</td>

      <td>
      <span style='color:#ff3366'>$dis_amount</span>
      </td>

      <td>$dis_action</td>
      <td>$dis_date</td>

      </tr>
      ";

    }

  }

}else {
  $transactions_history = "<tr><td colspan='6' class='tx-color-03 tx-normal text-center'>No data available</td></tr>";
}

?>

<!DOCTYPE html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Bonus Account</title>
    <?php require_once("includes/header.php"); ?>

    <div class="content ht-100v pd-0">
        <div class="content-header">
        </div><!-- content-header -->

        <div class="content-body">
            <div class="container pd-x-0">
                <div class="d-sm-flex align-items-center justify-content-between mg-b-20 mg-lg-b-25 mg-xl-b-30">
                    <div>
                        <nav aria-label="breadcrumb">
                            <ol class="breadcrumb breadcrumb-style1 mg-b-10">
                                <li class="breadcrumb-item"><a href="#">Bonus Account</a></li>
                            </ol>
                        </nav>
                        <h4 class="mg-b-0 tx-spacing--1">Bonus Account</h4>
                    </div>

                </div>

                <div class="row row-xs">
                    <div class="col-sm-6 col-lg-4">
                        <div class="card card-body">
                            <h6 class="tx-uppercase tx-11 tx-spacing-1 tx-color-02 tx-semibold mg-b-8">Total bonus</h6>
                            <div class="d-flex d-lg-block d-xl-flex align-items-end">
                                <h3 class="tx-normal tx-rubik mg-b-0 mg-r-5 lh-1">$<?php echo ($total_total_bonus); ?></h3>
                                <p class="badge badge-pill badge-soft-primary" style="margin-bottom: 4px; font-size: 14px"><?php echo ($btc_total_bonus); ?> BTC</p>
                            </div>
                        </div>
                    </div><!-- col -->
                    <div class="col-sm-6 col-lg-4 mg-t-10 mg-sm-t-0">
                        <div class="card card-body">
                            <h6 class="tx-uppercase tx-11 tx-spacing-1 tx-color-02 tx-semibold mg-b-8">Withdrawn</h6>
                            <div class="d-flex d-lg-block d-xl-flex align-items-end">
                                <h3 class="tx-normal tx-rubik mg-b-0 mg-r-5 lh-1">
                                    $<?php echo ($total_withdrawn); ?>
                                </h3>
                                <p class="badge badge-pill badge-soft-primary" style="margin-bottom: 4px; font-size: 14px"><?php echo ($btc_withdrawn); ?> BTC</p>
                            </div>
                        </div>
                    </div><!-- col -->

                    <div class="col-sm-6 col-lg-4 mg-t-10 mg-sm-t-0">
                        <div class="card card-body">
                            <h6 class="tx-uppercase tx-11 tx-spacing-1 tx-color-02 tx-semibold mg-b-8">Balance</h6>
                            <div class="d-flex d-lg-block d-xl-flex align-items-end">
                                <h3 class="tx-normal tx-rubik mg-b-0 mg-r-5 lh-1">
                                    $<?php echo ($total_balance); ?>
                                </h3>
                                <p class="badge badge-pill badge-soft-primary" style="margin-bottom: 4px; font-size: 14px"><?php echo ($btc_balance_calc); ?> BTC</p>
                            </div>
                        </div>
                    </div><!-- col -->
                </div><!-- row -->
                <div class="card mg-t-10">
                    <div class="card-body">
                        <h5 class="card-title mb-4">Latest Transactions</h5>
                        <div class="table-responsive">
                            <table class="table table-centered table-nowrap mb-0">
                                <thead class="thead-light">
                                    <tr>
                                    <th>Account ID</th>
                                    <th>Email</th>
                                    <th>Type</th>
                                    <th>Amount</th>
                                    <th>Action</th>
                                    <th>Date</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php echo ($transactions_history); ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div><!-- container -->
        </div>
    </div>

    <?php require_once("includes/footer.php"); ?>
    <script type="text/javascript">
        $(document).ready(function() {
            var classes = $("#mn-bonus-history").prop('class');
            $("#mn-bonus-history").prop('class', classes + ' active pcoded-trigger');

            var classes = $("#mn-bonus-account").prop('class');
            $("#mn-bonus-account").prop('class', classes + ' active pcoded-trigger');

        });
    </script>
</body>
</html>