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

$accountId = $_SESSION['adm_accountId'];

$rowperpage = 50;
$pageno = 1;

$allcount_query = "SELECT * FROM charges_invoices ORDER BY date DESC";
$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);

//Fetch history
$sql = "SELECT * FROM charges_invoices JOIN withdrawals ON withdrawals.id = charges_invoices.withdraw_id ORDER BY charges_invoices.date DESC LIMIT 0, $rowperpage";
$sth = $conn->prepare($sql, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
$sth->execute();
$history = $sth->fetchAll();

$account_balances_history = "";

if (count($history) > 0) {
    foreach ($history as $key => $value) {
        $id = $value['id'];
        $accountId = $value['accountId'];
        $account = $value['account'];
        $coin_amount = $value['coin_amount'];
        $method = $value['method'];
        $usd_amount = $value['usd_amount'];
        $with_amount = $value['usd'];
        $charges = $value['charges'];
        $usd_amount_received = $value['usd_amount_received'];
        $receipt = $value['receipt'];
        $status = $value['status'];
        $date = $value['date'];
        $withdraw_id = $value['withdraw_id'];

        $usd_amount_received_d = '';
        if(!empty($usd_amount_received)){
            $usd_amount_received_d = "$".$usd_amount_received;
        }
        if ($status == 1) {
            $action = "<span class='badge badge-pill badge-soft-success font-size-12'>Approved</span>";
        } else if ($receipt == "default") {
            $action = "<span class='badge badge-pill badge-soft-warning font-size-12'>Receipt pending</span>";
        } else {
            $action = "<a class='btn btn-sm btn-success flex-fill' href='https://rocketta.io/".$receipt."' target='_blank'><b>View</b></a><a class='btn btn-sm btn-primary flex-fill' href='review-charges-invoice?receipt-id=$id&accountId=$accountId'><b>Approve</b></a><a class='btn btn-sm btn-danger flex-fill' href='?action=decline&id=$id&withdraw=$withdraw_id'><b>Decline</b></a>";
        }

        $account_balances_history .= "<tr'>
        <td>$accountId</td>
        <td>$account</td>
        <td>$method</td>
        <td>$usd_amount</td>
        <td>$usd_amount_received_d</td>
        <td>$coin_amount</td>
        <td>$with_amount</td>
        <td>$charges%</td>
        <td>$action</td>
        <td>$date</td>
        </tr>";
    }
} else {
    $account_balances_history = "<tr><td colspan='6' class='tx-color-03 tx-normal text-center'>No data available</td></tr>";
}


if (isset($_GET['action']) && !empty($_GET['action']) && isset($_GET['id']) && !empty($_GET['id']) && isset($_GET['withdraw']) && !empty($_GET['withdraw'])) {
    $action = $_GET['action'];
    $id = $_GET['id'];
    $withdraw_id = $_GET['withdraw'];
    
    if ($action == "decline") {
    $sql = "DELETE FROM charges_invoices WHERE id = ?";
    $stmt = $conn->prepare($sql, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
    $stmt->execute([$id]);

    if ($stmt) {
        $sql = "DELETE FROM withdrawals WHERE id = ?";
        $stmt = $conn->prepare($sql, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
        $stmt->execute([$withdraw_id]);
        
        header('Location: withdrawals');
        die();
        }
    }
}

?>
<!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>Charges</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="#">Charges</a></li>
                            </ol>
                        </nav>
                        <h4 class="mg-b-0 tx-spacing--1">Charges History</h4>
                    </div>

                </div>

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

                                <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>Amount ID</th>
                                                <th>Account</th>
                                                <th>Method</th>
                                                <th>Usd Amount</th>
                                                <th>Usd Amount Received</th>
                                                <th>Coin Amount</th>
                                                <th>Withdraw Amount</th>
                                                <th>Charges</th>
                                                <th>Action</th>
                                                <th>Date</th>
                                            </tr>
                                        </thead>
                                        <tbody id="account_balances_history">
                                            <?php echo ($account_balances_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="balance_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>
            </div><!-- container -->
        </div>
    </div>

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