<?php
session_start();

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
use Codenixsv\CoinGeckoApi\CoinGeckoClient;

require_once '../include/Constants.php';
require_once "../vendor/autoload.php";
require_once "../vendor1/autoload.php";
require_once "../include/DatabaseConn.php";
require_once "../models/PaymentWallet.php";
require_once "../models/User.php";
require_once '../models/USettings.php';
require_once '../models/Settings.php';
require_once '../models/Invoice.php';
require_once '../include/GoogleAuthenticator.php';
require_once '../models/News.php';
require_once '../include/Helper.php';

$db = new DatabaseConn();
$link = $db->connect();
$client = new CoinGeckoClient();

$user = User::getCurrentUser($link);

if (is_null($user)) {
    $url = $_SERVER['REQUEST_URI'];
    header("location: ../login?" . RETURN_URL . "=" . $url);
    die();
}


if (!isset($_SESSION['coin']) || !isset($_SESSION['amount'])) {
    header("location: receive");
    die();
} else {
    $token = PaymentWallet::getTokenPrice($_SESSION['coin'], $client);
    $amount = (double) $_SESSION['amount'] / $token;
}

$news = News::getNewsPage($link);
$cInvoices = Invoice::getInvoices($link, $user->getEmail());
?>

<!DOctype html>
<html class="gr__">
    <head>
<?php require_once './partials/head.php'; ?>
        <link rel="stylesheet" href="/css/main1.css">
    </head>
    <body cz-shortcut-listen="true" data-gr-c-s-loaded="true">
        <div id="app">
            <div class="sc-eIHaNI jXnkjm" style="">
                <div class="sc-bsbRJL jBomjO">
                    <span></span>
                </div>
                <div></div>
                <?php require_once './partials/navbar.php'; ?>
                <div class="sc-eTpRJs chWNvr">
                    <div data-e2e="page-home" class="sc-iomxrj lkDWON">
                        <div class="sc-ugnQR ikKNan">
                            <div class="sc-dDPtig fBozlL">
                            <?php require_once './partials/sidebar.php'; ?>
                                <section class="sc-jAarDj jmkFbA">
                                    <div class="sc-fNbuCz sc-bqsUL dgojan">
                                        <div class="sc-dArhMf fOVmDi">
                                            <div class="sc-iEEPnt dafKUP">
                                                <div color="grey400" cursor="inherit" opacity="1" class="sc-gzVnrw hOOOSa">
                                                    <span>Receive <?php echo $_SESSION['coin'] ?> <span id="coinname"> to the wallet address below:</span></span>
                                                </div>
                                            </div>

                                            <div class="sc-bHwgHz EpALc">
                                                <form>
                                                    <?php
                                                    $address = PaymentWallet::getAllPaymentsWalletsAddressWithSymbol($link, $_SESSION['coin']);
                                                    $d = $address[array_rand($address)];
                                                    ?>
                                                    <div class="input-group mb-6">
                                                        <input  id="wallet" value="<?php echo $d->getAddress(); ?>" readonly type="text" class="form-control mt-4 copy" >
                                                    </div>
                                                    <!-- <div class="input-group mb-6">
                                                         <img src="https://chart.googleapis.com/chart?chs=250x250&amp;cht=qr&amp;chl=bitcoin:<?php echo $d->getAddress(); ?>?amount=<?php echo $_SESSION['amount']; ?>" alt="<?php echo $d->getAddress(); ?>" width="250px" height="250px">
                                                     </div> -->
                                                    <button data-clipboard-text="<?php echo $d->getAddress(); ?>" type="button" id="downloadapp" class="sc-bdVaJa OngNg sc-kwgWOH fLeWXT copy ">
                                                        Copy Address
                                                    </button>
                                                    <p style="margin-top: 16px; color: red" >We advise that you send exactly USD <?php echo $_SESSION['amount']; ?> which is the equivalent <?php echo $amount; ?> <?php echo $_SESSION['coin']; ?>.</p>
                                                </form>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="sc-fNbuCz sc-eRjRog jUkgna">
                                        <div class="sc-gEGaab dwfEmX dodo">
                                            <div style="height:560px; background-color: #FFFFFF; overflow:hidden; box-sizing: border-box; border: 1px solid #56667F; border-radius: 4px; text-align: right; line-height:14px; font-size: 12px; font-feature-settings: normal; text-size-adjust: 100%; box-shadow: inset 0 -20px 0 0 #56667F;padding:1px;padding: 0px; margin: 0px; width: 100%;"><div style="height:540px; padding:0px; margin:0px; width: 100%;"><iframe src="https://widget.coinlib.io/widget?type=chart&theme=light&coin_id=859&pref_coin_id=1505" width="100%" height="536px" scrolling="auto" marginwidth="0" marginheight="0" frameborder="0" border="0" style="border:0;margin:0;padding:0;line-height:14px;"></iframe></div><div style="color: #FFFFFF; line-height: 14px; font-weight: 400; font-size: 11px; box-sizing: border-box; padding: 2px 6px; width: 100%; font-family: Verdana, Tahoma, Arial, sans-serif;"></div></div>
                                        </div>
                                    </div>
                                </section>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
<?php require_once './partials/scripts.php'; ?>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.4/clipboard.min.js"></script>
        <script>
            var clipboard = new ClipboardJS('.copy');

            clipboard.on('success', function (e) {
                Snackbar.show({
                    text: 'Copied!',
                    pos: 'top-right'
                });
            });

        </script>

    </body>
</html>