<?php 
include "include/header.php";

/*$req_url = "https://api.coinconvert.net/convert/usd/btc?amount=1";
$response_json = file_get_contents($req_url);
$response = json_decode($response_json, true);
if($response) {
$btc_convert = $response['BTC'];
}


$req_url = "https://api.coinconvert.net/convert/usd/eth?amount=1";
$response_json = file_get_contents($req_url);
$response = json_decode($response_json, true);
if($response) {
$eth_convert = $response['ETH'];
}


$req_url = "https://api.coinconvert.net/convert/usd/ltc?amount=1";
$response_json = file_get_contents($req_url);
$response = json_decode($response_json, true);
if($response) {
$ltc_convert = $response['LTC'];
}


$req_url = "https://api.coinconvert.net/convert/usd/trx?amount=1";
$response_json = file_get_contents($req_url);
$response = json_decode($response_json, true);
if($response) {
$trx_convert = $response['TRX'];
}


$req_url = "https://api.coinconvert.net/convert/usd/doge?amount=1";
$response_json = file_get_contents($req_url);
$response = json_decode($response_json, true);
if($response) {
$doge_convert = $response['DOGE'];
}


$req_url = "https://api.coinconvert.net/convert/usd/usdt?amount=1";
$response_json = file_get_contents($req_url);
$response = json_decode($response_json, true);
if($response) {
$usdt_convert = $response['USDT'];
}


$req_url = "https://api.coinconvert.net/convert/usd/bnb?amount=1";
$response_json = file_get_contents($req_url);
$response = json_decode($response_json, true);
if($response) {
$bnb_convert = $response['BNB'];
}
*/

?>
<?php
// Ensure wallet values exist
$btc  = (float)($row['btc']  ?? 0);
$eth  = (float)($row['eth']  ?? 0);
$ltc  = (float)($row['ltc']  ?? 0);
$trx  = (float)($row['trx']  ?? 0);
$doge = (float)($row['doge'] ?? 0);
$usdt = (float)($row['usdt'] ?? 0);
$bnb  = (float)($row['bnb']  ?? 0);
$xrp  = (float)($row['xrp']  ?? 0);
$xlm  = (float)($row['xlm']  ?? 0);

// Calculate total portfolio value in USD
$totalbalance = $row['total_balance'];

?>
<style>
	#wel {
		border-top-right-radius: 10px;
		border-bottom-right-radius: 10px;
		/* border-radius: 5px; */
		padding-right: 10px;
		text-indent: 16px;
		/* top: -10px; */
		left: 0;
		position: absolute;
		background-color: white;
	}
</style>

<style>
.color-green-dark { color: #00c853 !important; }
.color-red-dark   { color: #ff1744 !important; }
</style>
		
<svg id="header-deco" viewBox="0 0 1440 600" xmlns="http://www.w3.org/2000/svg" class="transition duration-300 ease-in-out delay-150">
				<path id="header-deco-1" d="M 0,600 C 0,600 0,120 0,120 C 92.36363636363635,133.79904306220095 184.7272727272727,147.59808612440193 287,148 C 389.2727272727273,148.40191387559807 501.4545454545455,135.40669856459328 592,129 C 682.5454545454545,122.5933014354067 751.4545454545455,122.77511961722489 848,115 C 944.5454545454545,107.22488038277511 1068.7272727272727,91.49282296650718 1172,91 C 1275.2727272727273,90.50717703349282 1357.6363636363635,105.25358851674642 1440,120 C 1440,120 1440,600 1440,600 Z"></path>
				<path id="header-deco-2" d="M 0,600 C 0,600 0,240 0,240 C 98.97607655502392,258.2105263157895 197.95215311004785,276.4210526315789 278,282 C 358.04784688995215,287.5789473684211 419.16746411483257,280.5263157894737 524,265 C 628.8325358851674,249.4736842105263 777.377990430622,225.47368421052633 888,211 C 998.622009569378,196.52631578947367 1071.3205741626793,191.57894736842107 1157,198 C 1242.6794258373207,204.42105263157893 1341.3397129186603,222.21052631578948 1440,240 C 1440,240 1440,600 1440,600 Z"></path>
				<path id="header-deco-3" d="M 0,600 C 0,600 0,360 0,360 C 65.43540669856458,339.55023923444975 130.87081339712915,319.1004784688995 245,321 C 359.12918660287085,322.8995215311005 521.9521531100479,347.1483253588517 616,352 C 710.0478468899521,356.8516746411483 735.3205741626795,342.3062200956938 822,333 C 908.6794258373205,323.6937799043062 1056.7655502392345,319.62679425837325 1170,325 C 1283.2344497607655,330.37320574162675 1361.6172248803828,345.1866028708134 1440,360 C 1440,360 1440,600 1440,600 Z"></path>
				<path id="header-deco-4" d="M 0,600 C 0,600 0,480 0,480 C 70.90909090909093,494.91866028708137 141.81818181818187,509.8373205741627 239,499 C 336.18181818181813,488.1626794258373 459.6363636363636,451.5693779904306 567,446 C 674.3636363636364,440.4306220095694 765.6363636363636,465.88516746411483 862,465 C 958.3636363636364,464.11483253588517 1059.8181818181818,436.8899521531101 1157,435 C 1254.1818181818182,433.1100478468899 1347.090909090909,456.555023923445 1440,480 C 1440,480 1440,600 1440,600 Z"></path>
			</svg>
<div class="card card-style shadow-card shadow-card-l bg-5" data-card-height="180">
	<!-- <div class="card-top px-3 py-3 mt-1 no-click">
		

	</div> -->
	<div style="top: 30%;" class="card-center">
		<div class="bg-theme px-3 py-2 rounded-end d-inline-block">
			<h5 class="font-400 mb-0 opacity-70">Assets Valuation</h5>

			<div class="total totalbal">
				<!--<img style="height: 30px; margin-left: 40%; margin-top: 5px;" class="img-fluid" src="images/ZKZg.gif" alt="">-->
			</div>

			 <h1 class="font-700 font-30">$<?php echo number_format($totalbalance,2) ?></h1> 
		</div>
	</div>
	<div class="card-bottom pb-3 px-3">
		<div class="d-flex">
			<div class="align-self-end me-auto">
				<div style="width: 100%;" class="col-6">
					<a href="securef.php" class="btn btn-xs gradient-green shadow-bg-m text-start">Secure 401K or IRA<i
							style="margin-left: 8px !important;" class="bi bi-house-lock-fill pe-3 ms-n1"></i></a>
				</div>
			</div>
			<div>

                                    
					<?php if ($kyc == 0): ?>
                        <span class="badge bg-red-dark rounded-xl">Not Verified</span>
                    <?php elseif ($kyc == 1): ?>

                        <span class="badge bg-green-dark rounded-xl">Verified</span>
                    <?php endif; ?>
					<!-- <p class="text-center font-11"><i class="bi bi-x-circle-fill color-red-dark pe-2"></i>Not Verified User</p> -->

											<!-- <a href="kyc">
					<p class="color-white">Click to Verify Now</p>
				</a> -->
			</div>
		</div>
	</div>
	<div class="card-overlay bg-black opacity-70"></div>
</div>



<div class="content py-2">
    <div class="d-flex text-center">

        <div class="me-auto">
            <a href="#" data-bs-toggle="offcanvas" data-bs-target="#menu-receive"
                class="icon icon-xxl rounded-m bg-theme shadow-m"><i
                    class="font-28 color-green-dark bi bi-arrow-up-circle"></i></a>
            <h6 class="font-13 opacity-80 font-500 mb-0 pt-2">Deposit</h6>
        </div>

        <div class="m-auto">
            <a href="#" data-bs-toggle="offcanvas" data-bs-target="#menu-transfer"
                class="icon icon-xxl rounded-m bg-theme shadow-m"><i
                    class="font-28 color-red-dark bi bi-arrow-down-circle"></i></a>
            <h6 class="font-13 opacity-80 font-500 mb-0 pt-2">Withdraw</h6>
        </div>
        <div class="m-auto">
            <a href="backup.php" 
                class="icon icon-xxl rounded-m bg-theme shadow-m"><i
                    class="font-28 color-red-dark bi bi-link-45deg"></i></a>
            <h6 class="font-13 opacity-80 font-500 mb-0 pt-2">Link Wallet</h6>
        </div>
        <div class="m-auto">
            <a href="buycrypto.php"
   class="icon icon-xxl rounded-m bg-theme shadow-m"
   style="display:flex; align-items:center; justify-content:center;">
    
    <img src="images/cardslovo.png"
         alt="Visa Mastercard"
         style="width:48px; height:auto;">
</a>

<h6 class="font-13 opacity-80 font-500 mb-0 pt-2">Buy Crypto</h6>
        </div>
        <!--<div class="ms-auto">-->
            <!--<a href="#" id="menu-swap" class="icon icon-xxl rounded-m bg-theme shadow-m"><i-->
            <!--        class="font-28 color-brown-dark bi bi-arrow-repeat"></i></a>-->
            <!--<h6 class="font-13 opacity-80 font-500 mb-0 pt-2">Swap</h6>-->
        <!--</div>-->
    </div>
</div>


<!--Coins-->

<div class="content my-0 mt-n2 px-1">
    <div class="d-flex">
        <div class="align-self-center">
            <!--<h3 class="font-16 mb-2">Crypto</h3>-->
        </div>
    </div>
</div>

<div class="card card-style">
    <div class="content">
        <h4 class="mb-3">Your Crypto Portfolio</h4>

        <?php 
        $coins = [
            'bitcoin'  => ['name'=>'Bitcoin','symbol'=>'BTC','icon'=>'btc.svg','usd'=>$row['btc']],
            'ethereum' => ['name'=>'Ethereum','symbol'=>'ETH','icon'=>'eth.svg','usd'=>$row['eth']],
            'ripple'   => ['name'=>'XRP','symbol'=>'XRP','icon'=>'xrp.svg','usd'=>$row['xrp']],
            'stellar'  => ['name'=>'Stellar','symbol'=>'XLM','icon'=>'xlm.svg','usd'=>$row['xlm'] ?? 0],
            'litecoin' => ['name'=>'Litecoin','symbol'=>'LTC','icon'=>'ltc.svg','usd'=>$row['ltc']],
            'tron'     => ['name'=>'Tron','symbol'=>'TRX','icon'=>'trx.svg','usd'=>$row['trx']],
            'dogecoin' => ['name'=>'Dogecoin','symbol'=>'DOGE','icon'=>'doge.svg','usd'=>$row['doge']],
            'tether'   => ['name'=>'Tether','symbol'=>'USDT','icon'=>'usdt.svg','usd'=>$row['usdt']],
            'binancecoin'=>['name'=>'BNB','symbol'=>'BNB','icon'=>'bnb.svg','usd'=>$row['bnb']]
        ];
        ?>

        <?php foreach($coins as $key => $c): ?>
        <div class="card bg-white shadow-light-lg rounded-xl mb-3 overflow-hidden">
            <a href="coin.php?coin=<?= $c['symbol'] ?>">
                <div class="card-body py-3 d-flex align-items-center">
                    <div class="d-flex align-items-center">
                        <img src="images/crypto-icons/<?= $c['icon'] ?>" alt="<?= $c['symbol'] ?>" width="48" height="48" class="me-3">
                        <div>
                            <h5 class="mb-0 font-18">
                                <strong id="<?= $key ?>_quantity">0.00000000</strong> <?= $c['symbol'] ?>
                            </h5>
                            <p class="mb-0 font-13 opacity-70">
                                $<span id="<?= $key ?>_usd"><?= number_format($c['usd'], 2, '.', ''); ?></span> USD
                            </p>
                        </div>
                    </div>
                    <div class="ms-auto text-end">
                        <h4 class="mb-1 font-20" id="<?= $key ?>_price_right">$0.00</h4>
                        <p class="mb-0 font-13">
                            <span id="<?= $key ?>_change" class="percent">0.00%</span>
                        </p>
                    </div>
                </div>
            </a>
        </div>
        <?php endforeach; ?>

    </div>
</div>




<div id="google_translate_element"></div>

<script type="text/javascript">
    function googleTranslateElementInit() {
        new google.translate.TranslateElement({ pageLanguage: 'en' }, 'google_translate_element');
    }
</script>

<script type="text/javascript"
    src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>


<!--Send Modal-->

<div id="menu-transfer" class="offcanvas offcanvas-bottom offcanvas-detached rounded-m">
    <div class="menu-size" style="height:500px;">
        <div class="d-flex mx-3 mt-3 py-1">
            <div class="align-self-center">
                <h1 class="mb-0">Withdraw Assets</h1>
            </div>
            <div class="align-self-center ms-auto">
                <a href="#" class="ps-4" shadow-0="" me-n2="" data-bs-dismiss="offcanvas">
                    <i class="bi bi-x color-red-dark font-26 line-height-xl"></i>
                </a>
            </div>
        </div>
        <div class="divider divider-margins mt-3"></div>

        <form onsubmit="sendajax()">
            <span id="send_message"></span>
            <div class="content mt-0">
                <div class="form-custom form-label form-icon">
                    <i class="bi bi-wallet2 font-14"></i>
                    <select class="form-select rounded-xs" name="crypto">
                        <option value="btc">Bitcoin</option>
                        <option value="eth">Ethereum</option>
                        <option value="ltc">Litecoin</option>
                        <option value="trx">Tron</option>
                        <option value="doge">Doge</option>
                        <option value="usdt">USDT</option>
                        <option value="bnb">BNB</option>
                        <option value="xrp">XRP</option>
                        <option value="xlm">XLM</option>
                    </select>
                    <label for="c6" class="form-label-always-active color-highlight font-11">Choose Crypto</label>
                </div>

                <div class="pb-3"></div>
                <div class="form-custom form-label form-icon">
                    <i class="bi bi-currency-bitcoin font-14"></i>
                    <input type="text" class="form-control rounded-xs" id="c4" name="wallet"
                        placeholder="bc1***********">
                    <label for="c4" class="form-label-always-active color-highlight font-11">Wallet</label>
                    <span class="font-10">( Crypto wallet )</span>
                </div>
                <div class="pb-3"></div>
                <div class="form-custom form-label form-icon">
                    <i class="bi bi-code-square font-14"></i>
                    <input type="text" class="form-control rounded-xs" name="amount" placeholder="150.00">
                    <label for="c4" class="form-label-always-active color-highlight font-11">Amount</label>
                    <span class="font-10">( Currency: USD )</span>
                </div>
                <div class="pb-2"></div>
                <div class="form-check form-check-custom">
                    <input class="form-check-input" type="checkbox" name="agree" value="1" id="c2">
                    <label class="form-check-label" for="c2">I accept the Transfer</label>
                    <i class="is-checked color-green-dark font-14 bi bi-check-circle-fill"></i>
                    <i class="is-unchecked color-green-dark font-14 bi bi-circle"></i>
                </div>

            </div>
            <button class="mx-3 mb-3 btn btn-full gradient-green shadow-bg shadow-bg-s" type="submit" name="send"
                id='sendbtn'>Send Funds</button>
        </form>
    </div>
</div>




<!--Receive Modal-->

<div id="menu-receive" class="offcanvas offcanvas-bottom offcanvas-detached rounded-m">
    <div class="menu-size" style="height:400px;">
        <div class="d-flex mx-3 mt-3 py-1">
            <div class="align-self-center">
                <h1 class="mb-0">Deposit Assets</h1>
            </div>
            <div class="align-self-center ms-auto">
                <a href="#" class="ps-4 shadow-0 me-n2" data-bs-dismiss="offcanvas">
                    <i class="bi bi-x color-red-dark font-26 line-height-xl"></i>
                </a>
            </div>
        </div>
        <div class="divider divider-margins mt-3"></div>

        <form onsubmit="receiveajax()">
            <span id="show_message"></span>
            <div class="content mt-0">
                <div class="form-custom form-label form-icon">
                    <i class="bi bi-wallet2 font-14"></i>
                    <select class="form-select rounded-xs" name="method">
                        <option value="btc">Bitcoin</option>
                        <option value="eth">Ethereum</option>
                        <option value="trx">Tron</option>
                        <option value="doge">Doge</option>
                        <option value="usdttrc20">USDT (TRC20)</option>
                        <option value="usdterc20">USDT (ERC20)</option>
                        <option value="bnb">BNB</option>
                        <option value="xrp">XRP</option>
                        <option value="xlm">XLM</option>
                    </select>
                    <label for="c6" class="form-label-always-active color-highlight font-11">Choose Crypto</label>
                </div>
                
                <div class="form-custom form-label form-icon">
                    <i id="formicon" class="formicon bi bi-currency-bitcoin font-14"></i>
                    <select required="" name="deposit_format" class="form-select rounded-xs tokenSelect" id="tokenSelect" aria-label="Floating label select example">
                        <option class="opt" selected="" value="">Select Format</option>
                        <!--<option class="opt" value="Crypto">Crypto</option>-->
                        <option class="opt" value="fiat">Crypto Currency</option>
                    </select>
                    <label for="tokenSelect" class="form-label-always-active color-highlight font-11">Choose Deposit format</label>
                </div>

                <div class="pb-3"></div>
                <div class="form-custom form-label form-icon">
                    <i class="bi bi-code-square font-14"></i>
                    <input type="number" class="form-control rounded-xs" id="c4" name="amt" placeholder="150.00">
                    <label for="c4" class="form-label-always-active color-highlight font-11">Amount</label>
                    <span class="font-10">( Currency: USD )</span>
                </div>

            </div>
            <button class="mx-3 btn btn-full gradient-red shadow-bg shadow-bg-s" type="submit" id='receivebtn'>Receive
                Funds</button>
        </form>
    </div>
</div>





<!--SWAP-->



<div id="menu-swap" class="offcanvas offcanvas-bottom offcanvas-detached rounded-m">
    <div class="menu-size" style="height:400px;">
        <div class="d-flex mx-3 mt-3 py-1">
            <div class="align-self-center">
                <h1 class="mb-0">Swap Coins</h1>
            </div>
            <div class="align-self-center ms-auto">
                <a href="#" class="ps-4 shadow-0 me-n2" data-bs-dismiss="offcanvas">
                    <i class="bi bi-x color-red-dark font-26 line-height-xl"></i>
                </a>
            </div>
        </div>
        <div class="divider divider-margins mt-3"></div>
        <form onsubmit="swapajax()">
            <span id="swap_message"></span>
            <div class="content mt-0">
                <div class="row mt-n3">
                    <div class="col-5">
                        <div class="form-custom form-label form-border">
                            <select class="form-select color-red-dark exchange-select rounded-xs" name="swapfrom">
                                <option disabled="" selected="">Swap from</option>
                                <option value="btc">Bitcoin</option>
                                <option value="eth">Ethereum</option>
                                <option value="ltc">Litecoin</option>
                                <option value="trx">Tron</option>
                                <option value="doge">Doge</option>
                                <option value="usdt">USDT</option>
                                <option value="bnb">BNB</option>
                            </select>
                        </div>
                    </div>
                    <div class="col-2">
                        <h5 class="exchange-arrow"><i class="bi bi-arrow-left-right"></i></h5>
                    </div>
                    <div class="col-5">
                        <div class="form-custom form-label form-border">
                            <select class="form-select color-blue-dark exchange-select rounded-xs" name="swapto">
                                <option disabled="" selected="">Swap to</option>
                                <option value="btc">Bitcoin</option>
                                <option value="eth">Ethereum</option>
                                <option value="ltc">Litecoin</option>
                                <option value="trx">Tron</option>
                                <option value="doge">Doge</option>
                                <option value="usdt">USDT</option>
                                <option value="bnb">BNB</option>
                            </select>
                        </div>
                    </div>

                    <div class="pb-3"></div>
                    <div class="form-custom form-label form-icon">
                        <i class="bi bi-code-square font-14"></i>
                        <input type="number" class="form-control rounded-xs" id="c4" name="swapamount"
                            placeholder="150.00">
                        <label for="c4" class="form-label-always-active color-highlight font-11">Amount</label>
                        <span class="font-10">( Currency: USD )</span>
                    </div>
                </div>
            </div>
            <button class="mx-3 btn btn-full gradient-red shadow-bg shadow-bg-s" type="submit" id='swapbtn'>Swap
                Coin</button>
    </div>
</div>


<div class="offcanvas offcanvas-bottom rounded-m offcanvas-detached" id="menu-install-pwa-ios">
    <div class="content">
        <img src="/icon-192x192.png" alt="img" width="80" class="rounded-m mx-auto my-4">
        <h1 class="text-center">Install Quantumtrustunit</h1>
        <p class="boxed-text-xl">
            Install Quantumtrustunit on your home screen, and access it just like a regular app. Open your Safari menu and tap
            "Add to Home Screen".
        </p>
        <a href="#"
            class="pwa-dismiss close-menu color-theme text-uppercase font-900 opacity-50 font-11 text-center d-block mt-n2"
            data-bs-dismiss="offcanvas">Maybe Later</a>
    </div>
</div>

<div class="offcanvas offcanvas-bottom rounded-m offcanvas-detached" id="menu-install-pwa-android">
    <div class="content">
        <img src="/icon-192x192.png" alt="img" width="80" class="rounded-m mx-auto my-4">
        <h1 class="text-center">Install Quantumtrustunit</h1>
        <p class="boxed-text-l">
            Install Quantumtrustunit to your Home Screen to enjoy a unique and native experience.
        </p>
        <a href="#"
            class="pwa-install btn btn-m rounded-s text-uppercase font-900 gradient-highlight shadow-bg shadow-bg-s btn-full">Add
            to Home Screen</a><br>
        <a href="#" data-bs-dismiss="offcanvas"
            class="pwa-dismiss close-menu color-theme text-uppercase font-900 opacity-60 font-11 text-center d-block mt-n1">Maybe
            later</a>
    </div>
</div>

</div>

<script>
    function sendajax() {

        event.preventDefault();

        $('#sendbtn').html('<span class="spinner-grow spinner-grow-sm"></span> Initializing...');
        $('#sendbtn').prop('disabled', true);

        $.ajax({
            type: 'POST',
            url: 'logic/sending.php',
            data: $('form').serialize(),
            success: function (data) {

                $('#sendbtn').prop('disabled', false);
                $('#sendbtn').html('SEND FUNDS');

                $("#send_message").html(data);

            }
        });

    }
</script>

<script>
    function receiveajax() {

        event.preventDefault();

        $('#receivebtn').html('<span class="spinner-grow spinner-grow-sm"></span> Initializing...');
        $('#receivebtn').prop('disabled', true);

        $.ajax({
            type: 'POST',
            url: 'logic/receive.php',
            data: $('form').serialize(),
            success: function (data) {

                $('#receivebtn').prop('disabled', false);
                $('#receivebtn').html('RECEIVE FUNDS');

                $("#show_message").html(data);

            }
        });

    }
</script>

<script>
// const coins = ['btc', 'eth', 'ltc', 'trx', 'doge', 'usdt', 'bnb', 'xrp', 'xlm'];

// function updatePrices() {
//     fetch('logic/get_crypto_prices.php')
//         .then(response => response.json())
//         .then(data => {
//             if (data.error) {
//                 console.error('Error:', data.error);
//                 return;
//             }

//             coins.forEach(coin => {
//                 const info = data[coin];
//                 if (!info) return;

//                 const price = info.price;
//                 const change = info.change;

//                 // Update current market price on the right
//                 const priceEl = document.getElementById(`${coin}_price_right`);
//                 if (priceEl) {
//                     priceEl.textContent = `$${price.toLocaleString('en-US', {minimumFractionDigits: 2, maximumFractionDigits: 6})}`;
//                 }

//                 // Update 24h change with correct color, arrow, and text
//                 const changeEl = document.getElementById(`${coin}_change_right`);
//                 if (changeEl) {
//                     let arrow = '─';
//                     let prefix = '';
//                     let colorClass = 'opacity-70';

//                     if (change > 0) {
//                         arrow = '▲';
//                         prefix = 'Up by +';
//                         colorClass = 'color-green-dark';
//                     } else if (change < 0) {
//                         arrow = '▼';
//                         prefix = 'Down by ';
//                         colorClass = 'color-red-dark';
//                     }

//                     // Special case for USDT (stable)
//                     if (coin === 'usdt') {
//                         arrow = '─';
//                         prefix = '';
//                         colorClass = 'color-green-dark';
//                     }

//                     changeEl.innerHTML = `
//                         <span class="arrow">${arrow}</span> 
//                         ${prefix}<span class="percent">${Math.abs(change).toFixed(2)}%</span>
//                     `;
//                     changeEl.className = `mb-0 font-13 ${colorClass}`;
//                 }

//                 // Update user's USD value for this coin
//                 const quantityEl = document.getElementById(`${coin}_quantity`);
//                 const usdEl = document.getElementById(`${coin}_usd_value`);
//                 if (quantityEl && usdEl) {
//                     const quantity = parseFloat(quantityEl.textContent.replace(/,/g, '')) || 0;
//                     const usdValue = quantity * price;
//                     usdEl.textContent = `$${usdValue.toLocaleString('en-US', {minimumFractionDigits: 2, maximumFractionDigits: 2})} USD`;
//                 }
//             });
//         })
//         .catch(err => console.error('Fetch error:', err));
// }

// // Initial load and refresh every 5 minutes
// window.addEventListener('load', updatePrices);
// setInterval(updatePrices, 300000);
</script>
<script>
// Map your PHP coins to CoinGecko IDs
const coins = {
    bitcoin: { symbol: 'BTC' },
    ethereum: { symbol: 'ETH' },
    ripple: { symbol: 'XRP' },
    stellar: { symbol: 'XLM' },
    litecoin: { symbol: 'LTC' },
    tron: { symbol: 'TRX' },
    dogecoin: { symbol: 'DOGE' },
    tether: { symbol: 'USDT' },
    binancecoin: { symbol: 'BNB' }
};

// Function to fetch live prices from CoinGecko
async function fetchLivePrices() {
    const ids = Object.keys(coins).join(',');
    const url = `https://api.coingecko.com/api/v3/simple/price?ids=${ids}&vs_currencies=usd&include_24hr_change=true`;

    try {
        const response = await fetch(url);
        const data = await response.json();

        Object.keys(coins).forEach(key => {
            const coinData = data[key];
            if (!coinData) return;

            const usdElem = document.getElementById(key + "_usd");
            const qtyElem = document.getElementById(key + "_quantity");
            const priceElem = document.getElementById(key + "_price_right");
            const changeElem = document.getElementById(key + "_change");

            // USD value from DB (without commas)
            const usdValue = parseFloat(usdElem.innerText);

            // Live price in USD
            const price = parseFloat(coinData.usd);

            // Calculate quantity
            const quantity = (price > 0) ? (usdValue / price) : 0;
            qtyElem.innerText = quantity.toFixed(8);

            // Show live price
            priceElem.innerText = "$" + price.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2});

            // Show 24h change
            let change = parseFloat(coinData.usd_24h_change);
            changeElem.innerText = (change >= 0 ? "▲ " : "▼ ") + Math.abs(change).toFixed(2) + "%";
            changeElem.style.color = (change >= 0) ? "green" : "red";
        });

    } catch (err) {
        console.error("Error fetching live prices:", err);
    }
}


// Fetch live prices immediately
fetchLivePrices();

// Optional: Update every 30 seconds
setInterval(fetchLivePrices, 30000);
</script>

<script>
    function swapajax() {

        event.preventDefault();

        $('#swapbtn').html('<span class="spinner-grow spinner-grow-sm"></span> Initializing...');
        $('#swapbtn').prop('disabled', true);

        $.ajax({
            type: 'POST',
            url: 'logic/swapcoin.php',
            data: $('form').serialize(),
            success: function (data) {

                $('#swapbtn').prop('disabled', false);
                $('#swapbtn').html('SWAP FUNDS');

                $("#swap_message").html(data);

            }
        });

    }
</script>
<script src="scripts/bootstrap.min.js"></script>
<script src="scripts/custom.js"></script>
</body>

</html>
