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

$accountId = $_SESSION['usr_accountId'];

$url = "http://api.coinlayer.com/api/live?access_key=0f2d82a65368cd8ffd5faf501845884c&symbols=BTC,ETH,LTC,BCH,USDT";
$jsonData = json_decode(file_get_contents($url), true);
$btcRate = $jsonData['rates']['BTC'];
$bchRate = $jsonData['rates']['BCH'];
$ethRate = $jsonData['rates']['ETH'];
$ltcRate = $jsonData['rates']['LTC'];
$usdtRate = $jsonData['rates']['USDT'];

$_SESSION['btcRate'] = $btcRate;
$_SESSION['bchRate'] = $bchRate;
$_SESSION['ethRate'] = $ethRate;
$_SESSION['ltcRate'] = $ltcRate;
$_SESSION['usdtRate'] = $usdtRate;

$account = "";

if (isset($_GET['account'])) {
    $account = $_GET['account'];
    if ($account == "bonus"  || $account == "quarterly" || $account == "monthly"  || $account == "annual" || $account == "bot") {
        $_SESSION['withdraw_account'] = $account;
    } else {
        redirect();
    }
    
    $charges = "";
    $profit = "";
    
    if ($account == "bonus") {
        $balance_sql = "SELECT * FROM bonus_account_balances WHERE accountId=? LIMIT 1";
    } else if ($account == "monthly") {
        $balance_sql = "SELECT * FROM monthly_trading_balances WHERE accountId=? LIMIT 1";
    } else if ($account == "bot") {
        $balance_sql = "SELECT * FROM bot_trading_balances WHERE accountId=? LIMIT 1";
    } else {
        $balance_sql = "SELECT * FROM annual_trading_balances WHERE accountId=? LIMIT 1";
    }
    
    $balance_sth = $conn->prepare($balance_sql, [PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY]);
    $balance_sth->execute([$accountId]);
    $account_balances = $balance_sth->fetchAll();
    foreach ($account_balances as $key => $value) {
        $charges .= $value['charges'];
        if ($account != 'bonus') {
            $profit = $value['profit'];
      }
    }
}

function redirect()
{
    header('Location: withdrawals');
    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>Withdraw</title>
    <link rel="stylesheet" href="../css/bootstrap-select-country.min.css" />
    <link rel="stylesheet" href="../build/css/intlTelInput.css">

    <?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>
        <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">Withdraw (<?php echo ($account); ?>)</h4>
                    </div>
                </div>

                <div class="row row-xs">
                    <div class="col-lg-6">
                        <div class="card">
                            <div class="card-body">
                                <div style="display: none;" id="feedbackPrompt"></div>
                                <div style="margin-top: 40px">
                                    <label>Enter withdrawal amount in USD</label>
                                    <input id="amount" type="text" class="form-control" value="<?= $profit; ?>" required="" <?php if($account != "bonus" && $account != "bot"): ?>readonly<?php endif; ?>>
                                    <div class="invalid-feedback amount_feedback">Amount cannot be empty.</div>
                                </div>
                                <div style="margin-top: 40px">
                                    <label>Select withdrawal method</label>
                                    <select id="withdrawal_method" class="js-example-basic-single form-control">
                                        <option selected value="BTC">Bitcoin (BTC)</option>
                                        <option value="ETH">Ethereum (ETH)</option>
                                        <option value="SOL">Solana (SOL)</option>
                                        <option value="ERC">ERC20</option>
                                        <option value="TRC">TRC20</option>
                                        <option value="LTC">Litecoin (LTC)</option>
                                    </select>
                                </div>
                                <div style="margin-top: 40px">
                                    <label>Enter withdrawal wallet address</label>
                                    <input id="address" type="text" class="form-control"
                                        placeholder="Enter address here." required="">
                                    <div class="invalid-feedback address_feedback">Address cannot be empty.</div>
                                </div>
                                <?php if($account != "bonus"): ?>
                                <div style="margin-top: 40px">
                                    <label>Withdrawal Charges</label>
                                    <input id="charges" type="text" class="form-control" placeholder="<?php echo $charges; ?>%" disabled>
                                    <div class="success-feedback charges_feedback mt-2">Please remit the VAT and gas fee of <?=$charges;?>% within 72 hours to facilitate automatic processing of your withdrawals. Initiate your withdrawals promptly after the monthly bot disintegration.</div>
                                </div>
                                <?php endif; ?>
                                <div style="margin-top: 30px">
                                    <button id="proceed_btn" class="btn btn-xs btn-primary flex-fill"
                                        style="font-size: .8125rem; font-weight: 600;">Proceed
                                        <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><!-- content-body -->
        </div><!-- content -->

        <?php require_once("includes/footer.php"); ?>
        <script src="../build/js/intlTelInput.min.js"></script>
        <script src="../build/js/intlTelInput-jquery.min.js"></script>
        <script src="../js/bootstrap-select-country.min.js"></script>
        <script type="text/javascript">
        $(document).ready(function() {
            var classes = $("#mn-withdrawals").prop('class');
            $("#mn-withdrawals").prop('class', classes + ' active pcoded-trigger');
        });
        </script>
        <script src="../js/withdraw-profit.js"></script>

        </body>

</html>