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

$accountId = $_SESSION['usr_accountId'];

$total_bonus = "";

$balance_sql = "SELECT * FROM bot_account_balances WHERE accountId=? LIMIT 1";
$balance_sth = $conn->prepare($balance_sql, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
$balance_sth->execute([$accountId]);
$account_balances = $balance_sth->fetchAll();


foreach ($account_balances as $key => $value) {
  $usr_total_bonus = $value['total_bonus'];
  $usr_total_profit = $value['profit'];
  $usr_withdrawn = $value['withdrawn'];

  $total_bonus = number_format($usr_total_bonus, 2);
  $total_profit = number_format($usr_total_profit, 2);
  $withdrawn = number_format($usr_withdrawn, 2);

  $balance_calc = $usr_total_bonus - $usr_withdrawn;

  $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(($usr_total_bonus / $exchangeRate), 8);
  $btc_total_profit = round(($usr_total_profit / $exchangeRate), 8);
  $btc_withdrawn = round(($usr_withdrawn / $exchangeRate), 8);
  $btc_balance_calc = round(($balance_calc / $exchangeRate), 8);
}


$sql = "SELECT * FROM bot_account_transaction_history WHERE accountId = :accountId ORDER BY date DESC LIMIT 10";
$sth = $conn->prepare($sql, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
$sth->execute([':accountId' => $accountId]);
$history = $sth->fetchAll();

$transactions_history = "";

if (count($history) > 0) {
    foreach ($history as $key => $value) {
        $id = $value['id'];
        $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>$dis_name</td>

            <td>
            <span class='badge badge-pill badge-soft-success font-size-12'>$dis_amount</span>
            </td>

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

            </tr>
            ";

        }else {
            $transactions_history.="
            <tr>

            <td>$dis_name</td>

            <td>
            <span class='badge badge-pill badge-soft-danger  font-size-12'>$dis_amount</span>
            </td>

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

            </tr>
            ";

        }

    }

}else {
    $transactions_history = "<tr><td colspan='4' 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>Community Bot Account</title>
    <?php require_once("includes/header.php"); ?>

    <div class="content ht-100v pd-0">
        <div class="content-header center">
            <img src="../img/logo-white.png" class="aside-logo" style="width: 160px;" />
        </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="#">Community Bot Account</a></li>
                            </ol>
                        </nav>
                        <h4 class="mg-b-0 tx-spacing--1">Overview</h4>
                    </div>
                    <div class="d-block d-md-block">
                        <a href="bot-deposit"
                            class="btn btn-sm pd-x-15 btn-white btn-uppercase mg-l-5"><i data-feather="dollar-sign"
                                class="wd-10 mg-r-5"></i> Deposit</a>
                        <a href="withdraw-profit?account=bot"
                            class="btn btn-sm pd-x-15 btn-white btn-uppercase mg-l-5"><i data-feather="dollar-sign"
                                class="wd-10 mg-r-5"></i> Withdraw</a>
                    </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">Trade Amount</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_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">Profits</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_profit); ?>
                                </h3>
                                <p class="badge badge-pill badge-soft-primary"
                                    style="margin-bottom: 4px; font-size: 14px"><?php echo ($btc_total_profit); ?> 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 ($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><!-- 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>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-bot-account").prop('class');
        $("#mn-bot-account").prop('class', classes + ' active pcoded-trigger');

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

    });
    </script>

    </body>

</html>