<?php
    session_start();
    if( !isset($_SESSION['chap']) ){
        header("Location:../index.html#login-area");
        exit();
    }
    else{
        $chap = $_SESSION['chap'];
        include "../connect/db.php";
        $sql = mysqli_query($mysqli,"SELECT * FROM users WHERE email = '$chap' ");
        $row = mysqli_fetch_assoc($sql);
        if( $row['status'] == "suspended" ){
            header("Location:account.php");
            exit();
        }
        else if( $row['status'] == "terminated" ){
            header("Location:terminate.php");
            exit();
        }
    }
    



       if($row['kyc_status'] == "pending" )


{
   header("Location:kyc.php");
   exit();


}
    if($row['code_level'] != "nothing"){

if($row['lock_status'] == "active" )


{
   header("Location:auth.php");
   exit();


}
else{
    
}



}
else{
    
}
    



?>
<!DOCTYPE html>
<html lang="en-US" class="js">
    <head>
    <meta charset="utf-8">
    <meta name="author" content="Smart">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="Aspire Securities is a trademark of Aspire Financial Corporation. Aspire Securities is a trade name for the corporate and investment banking services of Aspire Financial Corporation and its subsidiaries. All rights reserved. Securities and strategic advisory services are provided by Aspire Securities, Inc., member FINRA, link opens in new tab and SIPC, link opens in new tab. Lending, financial risk management, and treasury management and payment services are offered by Aspire Bank. Deposit products are offered by Aspire Bank.">
    <!-- Fav Icon  -->
    <link rel="shortcut icon" href="../images/favicon.png">
    <!-- Page Title  -->
    <title><?=$row['firstname']?> <?=$row['middlename']?> <?=$row['lastname']?>  |  <?= htmlspecialchars(getSetting('site_name', 'Deutsche Bank Holdings Delaware')) ?>   Online banking</title>
    <!-- StyleSheets  -->
    <link rel="stylesheet" href="../scss/sweetalert.css">
    <link rel="stylesheet" href="assets/css/dashlite.css?ver=2.4.0">
    <link id="skin-default" rel="stylesheet" href="assets/css/theme.css?ver=2.4.0">
     <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">
     <link href="../css/toastr.css" rel="stylesheet"/>
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

   </head>
    <link rel="stylesheet" href="https://www.jqueryscript.net/demo/jQuery-International-Telephone-Input-With-Flags-Dial-Codes/build/css/intlTelInput.css">
   <script type="text/javascript">
function googleTranslateElementInit() {
  new google.translate.TranslateElement({pageLanguage: 'en', layout: google.translate.TranslateElement.InlineLayout.SIMPLE}, 'google_translate_element');
}
</script>
<script type="text/javascript" src="https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
    <style>
.goog-te-gadget-simple {
border:none;
}
.goog-te-gadget-simple a {
color:#000;
}
</style>
<style type="text/css">
    .btn-primary{
        background-color: #033d75;
    }
    .btn-secondary{
        background-color: #d13636;
    }
    .btn-secondary:hover{opacity: 0.6;}
    .btn-primary:hover{opacity: 0.6;}
</style>
<body class="nk-body npc-crypto bg-white has-sidebar ">
    <div class="nk-app-root">
        <!-- main @s -->
        <div class="nk-main ">
            <!-- sidebar @s -->
            <div class="nk-sidebar nk-sidebar-fixed " data-content="sidebarMenu">
                <div class="nk-sidebar-element nk-sidebar-head"  style="border-bottom: solid #fe473a;">
                    <div class="nk-sidebar-brand" >
                        <a href="dashboard" class="logo-link nk-sidebar-logo">
                            <img class="logo-light logo-img" src="../logo.png" srcset="../logo.png 2x" alt="logo">
                            <img class="logo-dark logo-img" src="../logo.png" srcset="../logo.png 2x" alt="logo-dark">
                            
                        </a>
                    </div>
                    <div class="nk-menu-trigger mr-n2">
                        <a href="#" class="nk-nav-toggle nk-quick-nav-icon d-xl-none" data-target="sidebarMenu"><em class="icon ni ni-arrow-left"></em></a>
                    </div>
                </div><!-- .nk-sidebar-element -->
                <?php include 'includes/sidebar.php'?>

                <!-- main header @e -->
    <div class="nk-content nk-content-fluid">
       <div class="container-xl wide-lg">
        <div class="nk-content-body">
        <div class="nk-block-head">
           <div class="nk-block-between-md g-4 card-bordered">
             <div class="nk-block-head-content">
              <h4 class="nk-block-title fw-normal">Make a Crypto Deposit</h4>
                <div class="nk-block-des">
                  <p>Select a cryptocurrency and enter the amount you wish to deposit.</p>
                </div>
              </div>
              <div class="nk-block-head-content">
                <ul class="nk-block-tools gx-3">
                  <li><a href="crypto" class="btn btn-outline-primary"><em class="icon ni ni-bitcoin"></em><span> Digital Assets</span></a></li>
                </ul>
              </div>
            </div>
          </div>
        </div>

        <div class="buysell wide-xs m-auto">
                     
                                <div class="buysell-title text-center">
                                    <h2 class="title">Which Crypto Asset will you like to Deposit? </h2>
                                </div><!-- .buysell-title -->
                                <div class="buysell-block">
                                    <form >
                                        <div class="buysell-field form-group">
                                            <div class="form-label-group">
                                                <label class="form-label">Choose your preffered asset</label>
                                            </div>
                                            <div class="form-control-group">
                                                <select type="number" class="form-control form-control-lg form-control-number" id="coin" name="coin">
                                                <div class="form-dropdown">
                                                    <div class="text"><?=currencySymbol($row['usercurrency'])?><span></span></div>
                                                </div>
                                                <option selected value="">Choose Crypto Asset</option>
                                                <option value="stellar" data-image="https://assets.coingecko.com/coins/images/12/large/stellar.png">Stellar</option>
                <option value="tether" data-image="https://assets.coingecko.com/coins/images/325/large/Tether-logo.png">Tether</option>
                <option value="bitcoin" data-image="https://assets.coingecko.com/coins/images/1/large/bitcoin.png">Bitcoin</option>
                <option value="bitcoin-cash" data-image="https://assets.coingecko.com/coins/images/144/large/bitcoin_cash.png">Bitcoin Cash</option>
                <option value="litecoin" data-image="https://assets.coingecko.com/coins/images/2/large/litecoin.png">Litecoin</option>
                <option value="ripple" data-image="https://assets.coingecko.com/coins/images/44/large/ripple.png">Ripple</option>
                <option value="tron" data-image="https://assets.coingecko.com/coins/images/109/large/tron.png">Tron</option>
                                            	                                            </select>
                                            </div>
                                        </div>

                                        <div class="buysell-field form-group">
                                            <div class="form-label-group">
                                                <label class="form-label" for="buysell-amount">Amount to Deposit</label>
                                            </div>
                                            <p></p>
                                            <div class="form-control-group">
                                                <input type="number" class="form-control form-control-lg form-control-number" id="amount" name="amount" placeholder="150.00">
                                                <div class="form-dropdown">
                                                    <div class="text"><?=currencySymbol($row['usercurrency'])?><span></span></div>
                                                </div>
                                            </div>
                                            <div id="buy_result"></div>
                                            <div class="form-note-group">
                                                <span class="buysell-min form-note-alt">Minimum: 10.00 <?=currencySymbol($row['usercurrency'])?></span>
                                            </div>
                                        </div><!-- .buysell-field -->
                                        <div class="buysell-field form-action">
                                            <button type="submit" class="btn btn-lg btn-block btn-primary stepTwoFormnew" id="btn2">Continue</button>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- content @e -->
                <script type="text/javascript">
$(document).ready(function () {
    $('.stepTwoFormnew').click(function (e) {
        e.preventDefault();

        var amount = $('#amount').val();
        var coin = $('#coin').val();

        // Validate that the variables are not null or empty
        if (!amount || amount.trim() === '') {
            Swal.fire({
                icon: 'error',
                title: 'Error',
                text: 'Amount cannot be empty',
                showConfirmButton: false,
                timer: 2000
            });
            return;
        }

        if (!coin || coin.trim() === '') {
            Swal.fire({
                icon: 'error',
                title: 'Error',
                text: 'Please select a cryptocurrency',
                showConfirmButton: false,
                timer: 2000
            });
            return;
        }

        if (amount < 10) {
            Swal.fire({
                icon: 'info',
                title: 'Notice',
                text: 'You can only deposit a minimum of 10 <?=$row['usercurrency']?>',
                showConfirmButton: false,
                timer: 2000
            });
            return;
        }

        // Define API endpoint for CoinGecko
        var apiUrl = 'https://api.coingecko.com/api/v3/simple/price?ids=' + coin + '&vs_currencies=usd';





        // Fetch the conversion rate from CoinGecko
        $.ajax({
            url: apiUrl,
            method: 'GET',
            success: function (response) {
                // Extract the conversion rate
                var rate = response[coin].usd;

                // Calculate the converted value
                var convertedValue = amount / rate;
        Swal.showLoading();

                // Send the amount and the converted value via AJAX
                $.ajax({
                    type: "POST",
                    url: "../apps/user/deposit-crypto.php",
                    data: {
                        "amount": amount,
                        "coin": coin,
                        "convertedValue": convertedValue
                    },
                    success: function (data) {
                        Swal.close();
                        document.getElementById("btn2").disabled = false;

                        // Append the modal HTML to the body
                        $('body').append(data);

                        // Show the modal
                        $('#detailsModal').modal('show');
                    },
                    error: function () {
                        Swal.fire({
                            icon: 'error',
                            title: 'Error',
                            text: 'Failed to process your request. Please try again.',
                            showConfirmButton: false,
                            timer: 2000
                        });
                    }
                });
            },
            error: function () {
                console.error('Error fetching conversion rate.');
                document.getElementById("btn2").disabled = false;
            }
        });

        return false; // Ensure the form does not submit and reload the page
    });
});
</script>

                </div>
            </div>
        </div>
    </div>

    <?php include 'includes/footer.php'; ?>