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

$accountId = $_SESSION['usr_accountId'];

$pageno = 1;

$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_invoices 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);

$sql = "SELECT * FROM bot_invoices 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();

$invoices_history = "";

if (count($history) > 0) {
    foreach ($history as $key => $value) {
        $id = $value['id'];
        $package = $value['package'];
        $method = $value['method'];
        $usd_amount = $value['usd_amount'];
        $coin_amount = $value['coin_amount'];
        $address = $value['address'];
        $status = $value['status'];
        $receipt = $value['receipt'];
        $date = $value['date'];

        $dtf = new DateTime($date);
        $dis_date = $dtf->format('d/m/Y');
        $dis_amount = number_format($usd_amount, 2);

        if ($status == 1) {
            $action = "<span class='badge badge-pill badge-soft-success font-size-12'>Approved</span>";
        } else if ($receipt == "default") {
            $action = "<a class='btn btn-xs btn-primary flex-fill' href='bot-invoice-details?receipt-id=$id&accountId=$accountId&account=bot'><b>Upload Receipt</b></a>";
        } else {
            $action = "<span class='badge badge-pill badge-soft-warning font-size-12'>Receipt uploaded</span>";
        }

        $invoices_history .= "
    <tr>
    <td>$package</td>
    <td>$method</td>
    <td>$coin_amount</td>
    <td>$dis_amount</td>
    <td>$address</td>
    <td>$action</td>
    <td>$dis_date</td>
    </tr>
    ";
    }
} else {
    $invoices_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>Invoices</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="account">Dashboard</a></li>
                            </ol>
                        </nav>
                        <h4 class="mg-b-0 tx-spacing--1">Community Bot Invoices</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">
                                    <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>Package</th>
                                                <th>Coin Type</th>
                                                <th>Coin Amount</th>
                                                <th>USD</th>
                                                <th>Address</th>
                                                <th>Status</th>
                                                <th>Date</th>
                                            </tr>
                                        </thead>
                                        <tbody id="invoices_history">
                                            <?php echo ($invoices_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">
                                </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><!-- row -->
            </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');

    });
    </script>
    <script src="../js/invoices.js"></script>

    </body>

</html>