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

$accountId = $_SESSION['adm_accountId'];

$pageno = 1;

$sql = "SELECT accountId, date FROM users ORDER BY date ASC LIMIT 1";
$sth = $conn->prepare($sql, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
$sth->execute();
$users = $sth->fetchAll();

foreach ($users as $key => $value) {
  $date = $value['date'];
  $dt = new DateTime($date);
  $dateFrom = $dt->format('d/m/Y');
}

$dateTo =  date("d/m/Y");

$rowperpage = 50;


$allcount_query = "SELECT * FROM bot_account_transaction_history";
$allcount_fetch = $conn->prepare($allcount_query, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
$allcount_fetch->execute();
$allcount = $allcount_fetch->rowCount();

$all_history = $allcount_fetch->fetchAll();

$totalPages = ceil($allcount / $rowperpage);

$sql = "SELECT bot_account_transaction_history.*, users.accountId, users.email FROM bot_account_transaction_history INNER JOIN users ON bot_account_transaction_history.accountId = users.accountId ORDER BY bot_account_transaction_history.date DESC LIMIT 0, $rowperpage";
$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>Transactions</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="#">Transactions</a></li>
                            </ol>
                        </nav>
                        <h4 class="mg-b-0 tx-spacing--1">Transactions</h4>
                    </div>

                </div>

                <div class="row row-xs">
                    <div class="col-lg-12">
                        <div class="card">
                            <div class="card-body">

                                <div class="row col-xl-12 col-md-12">
                                    <div class="col-md-2">
                                        <label>Status</label>
                                        <select id="package_status" class="custom-select">
                                            <option selected value="all">All</option>
                                            <option value="pending">Pending</option>
                                            <option value="approved">Approved</option>
                                        </select>
                                    </div>
                                    <div class="col-md-6 row">
                                        <div class="col-md-6">
                                            <label>From</label>

                                            <input type="text" id="dateFrom" class="form-control" placeholder="DD/MM/YYYY" value="<?php echo ($dateFrom) ?>">
                                        </div><!-- col -->
                                        <div class="col-md-6">
                                            <label>To</label>

                                            <input type="text" id="dateTo" class="form-control" placeholder="DD/MM/YYYY" value="<?php echo ($dateTo) ?>">

                                        </div><!-- col -->
                                    </div>
                                    <div class="col-md-4 search-field" style="padding-top: 30px; margin-bottom: 10px">

                                        <input type="text" id="search_field" class="form-control" placeholder="Search account ID, email..." name="search">
                                        <button id="search_button" class="btn" type="button">
                                            <i class="search-icon" data-feather="search"></i></i>
                                        </button>
                                    </div>
                                </div>

                                <div class="table-responsive col-md-12" style="margin-top: 30px">
                                    <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 id="transactions_history">
                                            <?php echo ($transactions_history); ?>
                                        </tbody>
                                    </table>
                                    <input type="hidden" id="history_page" value="1">
                                    <input type="hidden" id="total_pages" value="<?php echo ($totalPages); ?>">
                                    <input type="hidden" id="all_history" value="<?php echo ($allcount); ?>">
                                    <input type="hidden" id="loading_status" value="loaded">

                                    <input type="hidden" id="withdrawal_id" value="loaded">
                                    <input type="hidden" id="withdrawal_account_id" value="loaded">
                                </div>
                                <div class="col-md-12" style="margin-left: auto; margin-right: auto; margin-top: 20px">
                                    <div class="" style="float: right;">
                                        <nav aria-label="Page navigation example">
                                            <ul id="pagination" class="pagination pagination-circle mg-b-0" <?php if ($totalPages < 2) {
                                                                                                                echo "style='display: none'";
                                                                                                            } ?>>
                                                <li id="prev_history" class="page-item <?php if ($pageno <= 1) {
                                                                                            echo 'disabled';
                                                                                        } ?>">
                                                    <a class="page-link page-link-icon" href="javascript:function previous() { return false; }"><i data-feather="chevron-left"></i></a>
                                                </li>
                                                <li id="next_history" class="page-item <?php if ($pageno >= $totalPages) {
                                                                                            echo 'disabled';
                                                                                        } ?>">
                                                    <a class="page-link page-link-icon" href="javascript:function next() { return false; }"><i data-feather="chevron-right"></i></a>
                                                </li>
                                            </ul>
                                        </nav>
                                    </div><!-- df-example -->
                                </div>

                            </div>
                        </div>
                    </div>
                </div>
                <!-- end row -->


            </div><!-- container -->
        </div>
    </div>

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

        });
    </script> -->
    <script src="../js/admin/bot-transactions.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            var classes = $("#mn-bot-account").prop('class');
            $("#mn-bot-account").prop('class', classes + ' active pcoded-trigger');

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