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

$bal_sql = "SELECT SUM(trade_amount) from demo_trading_balances";
$bal_sql = $conn->prepare($bal_sql);
$bal_sql->execute();
$bal_results = $bal_sql->fetch(PDO::FETCH_NUM);
$results_value = $bal_results[0];
$demo_trade_amount = number_format($results_value, 2);

$bal_sql = "SELECT SUM(trade_amount) from monthly_trading_balances";
$bal_sql = $conn->prepare($bal_sql);
$bal_sql->execute();
$bal_results = $bal_sql->fetch(PDO::FETCH_NUM);
$results_value = $bal_results[0];
$monthly_trade_amount = number_format($results_value, 2);

$bal_sql = "SELECT SUM(trade_amount) from annual_trading_balances";
$bal_sql = $conn->prepare($bal_sql);
$bal_sql->execute();
$bal_results = $bal_sql->fetch(PDO::FETCH_NUM);
$results_value = $bal_results[0];
$annual_trade_amount = number_format($results_value, 2);

$bal_sql = "SELECT SUM(profit) from monthly_trading_balances";
$bal_sql = $conn->prepare($bal_sql);
$bal_sql->execute();
$bal_results = $bal_sql->fetch(PDO::FETCH_NUM);
$results_value = $bal_results[0];
$monthly_trade_balance = number_format($results_value, 2);

$bal_sql = "SELECT SUM(profit) from annual_trading_balances";
$bal_sql = $conn->prepare($bal_sql);
$bal_sql->execute();
$bal_results = $bal_sql->fetch(PDO::FETCH_NUM);
$results_value = $bal_results[0];
$annual_trade_balance = number_format($results_value, 2);

$sql = "SELECT transaction_history.*, users.accountId, users.email FROM transaction_history INNER JOIN users ON transaction_history.accountId = users.accountId ORDER BY 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);
        $dis_amount = number_format($amount, 2);

        if ($status == 1) {
            $dis_amount_data = "<span class='badge badge-pill badge-success'>+ &#36;$dis_amount</span>";
        } else if ($status == 0) {
            $dis_amount_data = "<span class='badge badge-pill badge-danger'>- &#36;$dis_amount</span>";
        } else {
            $dis_amount_data = "&#36;$dis_amount";
        }
        
        if($dis_name == "Ninja 2.1 (Monthly)") {
            $dis_name = "Ninja 3.6 (Monthly)";
        } 
        
        if ($dis_name == "Subzero 3.2 (Monthly)") {
            $dis_name = "Subzero 5.2 (Monthly)";
        } 
        
        if ($dis_name == "Rocket 4.1 (Monthly)") {
            $dis_name = "Rocket 7.1 (Monthly)";
        }
        
        if($dis_name == "Ninja 2.1 (Quarterly)") {
            $dis_name = "Ninja 3.6 (Quarterly)";
        } 
        
        if ($dis_name == "Subzero 3.2 (Quarterly)") {
            $dis_name = "Subzero 5.2 (Quarterly)";
        } 
        
        if ($dis_name == "Rocket 4.1 (Quarterly)") {
            $dis_name = "Rocket 7.1 (Quarterly)";
        }
        
        if($dis_name == "Ninja 2.1 (Annually)") {
            $dis_name = "Ninja 3.6 (Annually)";
        } 
        
        if ($dis_name == "Subzero 3.2 (Annually)") {
            $dis_name = "Subzero 5.2 (Annually)";
        } 
        
        if ($dis_name == "Rocket 4.1 (Annually)") {
            $dis_name = "Rocket 7.1 (Annually)";
        }

        $transactions_history .= "
        <tr>
        <td>$accountId</td>
        <td>$email</td>
        <td>$dis_name</td>
        <td>$dis_amount_data</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>Dashboard</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="#">Dashboard</a></li>
                            </ol>
                        </nav>
                        <h4 class="mg-b-0 tx-spacing--1">Dashboard</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">Demo 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 ($demo_trade_amount); ?>
                                </h3>
                            </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">Monthly 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 ($monthly_trade_amount); ?></h3>
                            </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">Annual 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 ($annual_trade_amount); ?>
                                </h3>
                            </div>
                        </div>
                    </div><!-- col -->
                    <div class="col-sm-6 col-lg-6 mg-t-10 mg-sm-t-0 mg-lg-t-10">
                        <div class="card card-body">
                            <h6 class="tx-uppercase tx-11 tx-spacing-1 tx-color-02 tx-semibold mg-b-8">Monthly
                                Trade 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 ($monthly_trade_balance); ?>
                                </h3>
                            </div>
                        </div>
                    </div><!-- col -->
                    <div class="col-sm-6 col-lg-6 mg-t-10 mg-sm-t-0 mg-lg-t-10">
                        <div class="card card-body">
                            <h6 class="tx-uppercase tx-11 tx-spacing-1 tx-color-02 tx-semibold mg-b-8">Annual Trade
                                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 ($annual_trade_balance); ?>
                                </h3>
                            </div>
                        </div>
                    </div><!-- col -->
                </div><!-- row -->
                <div class="card mg-t-10">
                    <div class="card-body">
                        <h5 class="card-title mb-4">Recent 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-dashboard").prop('class');
        $("#mn-dashboard").prop('class', classes + ' active pcoded-trigger');

    });
    </script>

    </body>

</html>