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


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

    $accountId = $_GET['accountId'];
    $receipt_id = $_GET['receipt-id'];

    $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) {

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

            if ($status != 1) {
                $_SESSION['charges_receipt_id'] = $receipt_id;
            } else {
                redirect();
            }
        }
    } else {
        redirect();
    }
} else {
    redirect();
}


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


?>

<!DOCTYPE html>
<html lang="en">

<head>
    <title>Invoice Details</title>

    <?php require("includes/header.php"); ?>

    <!-- [ Main Content ] start -->
    <div class="pcoded-main-container" id="top_area">
        <div class="pcoded-content">
            <!-- [ breadcrumb ] start -->
            <div class="page-header">
                <div class="page-block">
                    <div class="row align-items-center">
                        <div class="col-md-12">
                            <div class="page-header-title">
                                <h5 class="m-b-10">Invoice Details</h5>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- [ breadcrumb ] end -->

            <!-- [ Main Content ] start -->
            <div class="row">
                <div class="col-sm-12">
                    <div class="card">
                        <div class="card-body">

                            <div class="col-md-6 tx-center align-items-center"
                                style="margin-left: auto; margin-right: auto; text-align: center; ">

                                <div style="display: none;" id="errorPrompt"></div>

                                <h4 class="mb-0 font-size-18">Payment <br /><br /></h4>
                                <p><b>Amount: <?php echo (number_format($usd_amount, 2)) ?> USD = <?php echo ($coin_amount) ?> <?php echo ($method) ?></b> <br /></p>
                                <p>Kindly remit payment to the address below to complete your deposit and start
                                    trading.</p>

                                <p>
                                    <img style="width: 250px; height:250px"
                                        src="https://qrcode.tec-it.com/API/QRCode?data=<?php echo ($address) ?>" />
                                </p> 

                                <p class="col-md-12" style="padding: 10px; background: #fff; color: #4680ff">
                                    <strong><?php echo ($address) ?></strong>
                                </p>

                                <h4 class="mb-0 font-size-18" style="margin-top: 30px">Upload Payment Receipt
                                    <br /><br />
                                </h4>
                                <p>Kindly select and upload the payment receipt of your transaction below.<br /></p>


                                <div class="custom-file">
                                    <input type="file" class="custom-file-input" id="receipt_file">
                                    <label class="custom-file-label" for="receipt_file">Choose file</label>
                                    <div class="invalid-feedback receipt_feedback"></div>
                                </div>

                                <button id="upload_receipt" class="btn btn-xs btn-primary flex-fill"
                                    style="font-size: .8125rem; font-weight: 600; margin-top: 20px; margin-bottom: 30px; width: 100%">Upload
                                    <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>


        </div>
    </div>

    <?php require("includes/footer.php"); ?>

    <script src="../js/charges-invoice-details.js"></script>

    <script>
    $('#receipt_file').on('change', function(e) {
        var fileName = e.target.files[0].name;
        $(this).next('.custom-file-label').html(fileName);
    });

    var classes = $("#mn-charges").prop('class');
    $("#mn-charges").prop('class', classes + ' active');
    </script>

    </body>

</html>