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

$accountId = $_SESSION['usr_accountId'];

$sql = "SELECT accountId, date FROM 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) {
    $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_trade_history WHERE accountId = :accountId";
$allcount_fetch = $conn->prepare($allcount_query, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
$allcount_fetch->execute([':accountId' => $accountId]);
$allcount = $allcount_fetch->rowCount();

$all_history = $allcount_fetch->fetchAll();

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


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

$trade_history = "";

if (count($history) > 0) {
    foreach ($history as $key => $value) {
        $id = $value['id'];
        $type = $value['type'];
        $trade_amount = $value['trade_amount'];
        $profit = $value['profit'];
        $recent_profit = $value['recent_profit'];
        $recent_date = $value['recent_date'];
        $status = $value['status'];
        $has_leverage = $value['has_leverage'];
        $leverage = $value['leverage'];
        $margin = $value['margin'];
        $closed_date = $value['closed_date'];
        $date = $value['date'];

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

        if ($recent_date != "" || $recent_date != null) {
            $dtf = new DateTime($recent_date);
            $dis_recent_date = $dtf->format('d/m/Y');
        } else {
            $dis_recent_date = "";
        }

        if ($closed_date != "" || $closed_date != null) {
            $dtf = new DateTime($closed_date);
            $dis_closed_date = $dtf->format('d/m/Y');
        } else {
            $dis_closed_date = "";
        }

        $dis_trade_amount = number_format($trade_amount, 2);
        $dis_profit = number_format($profit, 2);
        $dis_margin = number_format($margin, 2);
        if ($recent_profit >= 0) {
            $dis_recent_profit = "$".number_format($recent_profit, 2);
        } else {
            $dis_recent_profit = "-$".number_format( abs($recent_profit), 2);
        }

        if ($status == "1") {
            $status_action = "<span class='badge badge-pill badge-soft-success font-size-12'>Active</span>";
        } else {
            $status_action = "<span class='badge badge-pill badge-soft-danger font-size-12'>Closed</span>";
        }

        $trade_history .= "
            <tr>

            <td>$type</td>
            <td>$$dis_trade_amount</td>
            <td>$leverage</td>
            <td>$$dis_margin</td>
            <td>$$dis_profit</td>
            <td>$dis_recent_profit</td>
            <td>$dis_recent_date</td>

            <td>
            $status_action
            </td>
            <td>$dis_date</td>
            <td>$dis_closed_date</td>
            
            </tr>
            ";
    }
} else {
    $trade_history = "<tr><td colspan='10' class='tx-color-03 tx-normal text-center'>No data available</td></tr>";
}


// for active bot
$bot = "";
$bot_purchase_id = "";

$sql = "SELECT * FROM purchased_bots WHERE accountId=? AND type =? AND status=?  ORDER BY date DESC LIMIT 1";
$sth = $conn->prepare($sql, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
$sth->execute([$accountId, 'Community Bot', 1]);
$history4 = $sth->fetchAll();

if (count($history4) > 0) {
    $bot = $history4[0]['type'];
    $bot_purchase_id = $history4[0]['bot_purchase_id'];
}
?>
<!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 Trading History</title>
    <link rel="stylesheet" href="../css/bootstrap-select-country.min.css" />
    <link rel="stylesheet" href="../build/css/intlTelInput.css">

    <?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>
        <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="account">Dashboard</a></li>
                            </ol>
                        </nav>
                        <h4 class="mg-b-0 tx-spacing--1">Community Bot Trading History</h4>
                        <div style="margin-top: 20px;color: #034c03;">
                            <span><?php echo $bot; ?></span><br>
                            <span>Bot ID: </span><span><?php echo $bot_purchase_id; ?></span>
                        </div>
                    </div>
                    <div class="d-block d-md-block">
                        <a href="bot-trading" class="btn btn-sm pd-x-15 btn-white btn-uppercase"><i
                                data-feather="layers" class="wd-10 mg-r-5"></i> Chart</a>
                        <a href="bot-trading-bot-purchase"
                            class="btn btn-sm pd-x-15 btn-white btn-uppercase mg-l-5"><i data-feather="bar-chart"
                                class="wd-10 mg-r-5"></i> Trade</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="d-sm-flex align-items-center justify-content-end mg-b-20 mg-lg-b-25 mg-xl-b-30">
                    <div>
                        <a href="./bot-trading-tansections-history" class="btn btn-xs btn-primary">Trading
                            Transactionss History</a>
                    </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">
                                    <div class="col-md-3">
                                        <label>Status</label>
                                        <select id="package_status" class="js-example-basic-single form-control">
                                            <option selected value="all">All</option>
                                            <option value="open">Open</option>
                                            <option value="closed">Closed</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>

                                <div class="table-responsive" style="margin-top: 30px">
                                    <table class="table table-centered table-nowrap mb-0">
                                        <thead class="thead-light">
                                            <tr>
                                                <th>Type</th>
                                                <th>Trade Amount</th>
                                                <th>Leverage</th>
                                                <th>Margin</th>
                                                <th>Total Profit</th>
                                                <th>Recent Profit</th>
                                                <th>Recent Profit Date</th>
                                                <th>Status</th>
                                                <th>Open Date</th>
                                                <th>Closed Date</th>
                                            </tr>
                                        </thead>
                                        <tbody id="trade_history">
                                            <?php echo ($trade_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="cancel_id" value="loaded">
                                </div>
                                <div class="col-md-10" 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>

            </div>
        </div><!-- content-body -->
    </div><!-- content -->

    <?php require_once("includes/footer.php"); ?>
    <script src="../build/js/intlTelInput.min.js"></script>
    <script src="../build/js/intlTelInput-jquery.min.js"></script>
    <script src="../js/bootstrap-select-country.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
        var classes = $("#mn-trading").prop('class');
        $("#mn-trading").prop('class', classes + ' active pcoded-trigger');

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

    });
    </script>
    <script src="../js/bot-trading-history.js?ver=<?php echo (rand()); ?>"></script>

    </body>

</html>