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

$accountId = $_SESSION['adm_accountId'];

if (isset($_GET['receipt-id']) && isset($_GET['accountId'])) {
    $receipt_id = $_GET['receipt-id'];
    $accountId = $_GET['accountId'];

    $sql = "SELECT * FROM charges_invoices WHERE id =? AND accountId=? LIMIT 1";

    $sth = $conn->prepare($sql, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
    $sth->execute([$receipt_id, $accountId]);
    $coin_invoice = $sth->fetchAll();

    if (count($coin_invoice) > 0) {
        foreach ($coin_invoice as $key => $value) {

            $id = $value['id'];
            $method = $value['method'];
            $usd_amount = $value['usd_amount'];
            $coin_amount = $value['coin_amount'];
            $address = $value['address'];
            $receipt = $value['receipt'];
            $status = $value['status'];
            $date = $value['date'];

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

            if ($status != 1) {

                $sql = "SELECT * 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) {
                    $fullName = $value['fullname'];
                    $email = $value['email'];
                }

                $_SESSION['charges_approve_receipt'] = $receipt_id;
                $_SESSION['charges_approve_accountId'] = $accountId;
            } else {
                redirect();
            }
        }
    } else {
        redirect();
    }
} else {
    redirect();
}


function redirect()
{
    header('Location: charges');
    exit();
}

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

                </div>

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

                                <div style="display: none;" id="errorPrompt"></div>
                                <p>
                                    <a class="btn btn-xs btn-primary flex-fill" href="../<?php echo ($receipt) ?>"
                                        target="_blank"><b>View Receipt</b></a>
                                </p>

                                <div class="col-md-12">
                                    <label>Account ID</label>
                                    <input type="text" class="form-control" placeholder="" disabled="true"
                                        value="<?php echo ($accountId) ?>">
                                </div>

                                <div class="col-md-12" style="margin-top: 20px">
                                    <label>Full Name</label>
                                    <input type="text" class="form-control" placeholder="" disabled="true"
                                        value="<?php echo ($fullName) ?>">
                                </div>

                                <div class="col-md-12" style="margin-top: 20px">
                                    <label>Email Address</label>
                                    <input type="text" class="form-control" placeholder="" disabled="true"
                                        value="<?php echo ($email) ?>">
                                </div>

                                <div class="col-md-12" style="margin-top: 20px">
                                    <label>Coin Type</label>
                                    <input type="text" class="form-control" placeholder="" disabled="true"
                                        value="<?php echo ($method) ?>">
                                </div>

                                <div class="col-md-12" style="margin-top: 20px">
                                    <label>Coin Amount</label>
                                    <input type="text" class="form-control" placeholder="" disabled="true"
                                        value="<?php echo ($coin_amount) ?>">
                                </div>

                                <div class="col-md-12" style="margin-top: 20px">
                                    <label>USD</label>
                                    <input type="text" class="form-control" placeholder="" disabled="true"
                                        value="<?php echo ($usd_amount) ?>">
                                </div>

                                <div class="col-md-12" style="margin-top: 20px">
                                    <label>Received Coin Address</label>
                                    <input type="text" class="form-control" placeholder="" disabled="true"
                                        value="<?php echo ($address) ?>">

                                </div>

                                <div class="col-md-12" style="margin-top: 20px">
                                    <label>Date</label>
                                    <input type="text" class="form-control" placeholder="" disabled="true"
                                        value="<?php echo ($dis_date) ?>">
                                </div>

                                <div class="col-md-12" style="margin-top: 20px">
                                    <label>Amount Received (USD)</label>
                                    <input type="text" id="amount_received" class="form-control" placeholder="">
                                    <div class="invalid-feedback amount_received_feedback"></div>
                                </div>

                                <div class="col-md-12" style="margin-top: 20px; margin-bottom: 30px;">
                                    <button id="approve_investment" class="btn btn-xs btn-primary flex-fill"
                                        style="font-size: .8125rem; font-weight: 600;">Approve
                                        <span style="display: none;"
                                            class="button_spinner spinner-border spinner-border-sm" role="status"
                                            aria-hidden="false"></span>
                                        <span class="sr-only">Please wait...</span>
                                    </button>
                                </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-charges").prop('class');
        $("#mn-charges").prop('class', classes + ' active pcoded-trigger');

    });
    </script>
    <script src="../js/admin/review-charges-invoice.js"></script>

    </body>

</html>