// Global variables let currentMarketData = {}; function getLang() { return document.documentElement.lang || 'en'; } // Market Data Fetching async function fetchMarketData() { try { const resp = await fetch('api/market_api.php'); if (!resp.ok) throw new Error('Network response was not ok'); const result = await resp.json(); if (result.success) { currentMarketData = result.data; updateUI(); } } catch (e) { console.error('Market API error', e); } } function updateUI() { // Update Home Page Market List (Market Trends Table) const homeList = document.getElementById('market-trends'); if (homeList) { let html = ''; const symbols = ['BTC', 'ETH', 'BNB', 'SOL', 'XRP', 'DOGE', 'ADA', 'TRX']; symbols.forEach(symbol => { const coin = currentMarketData[symbol]; if (coin) { const changeClass = coin.change >= 0 ? 'text-success' : 'text-danger'; const changeSign = coin.change >= 0 ? '+' : ''; const iconClass = coin.change >= 0 ? 'fa-arrow-trend-up' : 'fa-arrow-trend-down'; html += `
${symbol}
${coin.name}
$${coin.price.toLocaleString(undefined, {minimumFractionDigits: 2})} ${changeSign}${coin.change.toFixed(2)}% ${typeof t === 'function' ? t('Go to Trade') : 'Trade'} `; } }); if (html) homeList.innerHTML = html; } // Update Hero Ticker (Horizontal Bar) - Improved with premium gradient & glow const heroTicker = document.getElementById('hero-market-ticker'); if (heroTicker) { let html = ''; const symbols = ['BTC', 'ETH', 'SOL', 'BNB', 'XRP']; symbols.forEach(symbol => { const coin = currentMarketData[symbol]; if (coin) { const changeClass = coin.change >= 0 ? 'text-success' : 'text-danger'; // Using a premium gradient instead of a "white box" const gradient = coin.change >= 0 ? 'linear-gradient(135deg, rgba(14, 203, 129, 0.15) 0%, rgba(0, 70, 255, 0.05) 100%)' : 'linear-gradient(135deg, rgba(246, 70, 93, 0.15) 0%, rgba(0, 70, 255, 0.05) 100%)'; const borderColor = coin.change >= 0 ? 'rgba(14, 203, 129, 0.2)' : 'rgba(246, 70, 93, 0.2)'; html += `
${symbol}/USDT ${coin.change >= 0 ? '+' : ''}${coin.change.toFixed(2)}%
$${coin.price.toLocaleString(undefined, {minimumFractionDigits: 2})}
`; } }); if (html) heroTicker.innerHTML = html; } // Update Ticker const ticker = document.getElementById('ticker-wrap'); if (ticker) { let html = ''; Object.keys(currentMarketData).forEach(symbol => { const coin = currentMarketData[symbol]; const changeClass = coin.change >= 0 ? 'text-success' : 'text-danger'; html += `
${symbol}/USDT ${coin.price.toLocaleString(undefined, {minimumFractionDigits: 2})} ${coin.change >= 0 ? '+' : ''}${coin.change.toFixed(2)}%
`; }); ticker.innerHTML = html + html; } // Update Trade Page if on it if (document.getElementById('crypto-list-container')) { updateTradePage(); } } function updateTradePage() { const listContainer = document.getElementById('crypto-list-container'); const search = document.getElementById('market-search')?.value.toLowerCase() || ''; const currentSymbol = document.getElementById('current-symbol')?.value; let html = ''; Object.keys(currentMarketData).forEach(symbol => { if (symbol.toLowerCase().includes(search) || currentMarketData[symbol].name.toLowerCase().includes(search)) { const coin = currentMarketData[symbol]; const active = symbol === currentSymbol ? 'active' : ''; const changeClass = coin.change >= 0 ? 'text-success' : 'text-danger'; html += `
${symbol}
${coin.name}
$${coin.price.toLocaleString(undefined, {minimumFractionDigits: 2})}
${coin.change >= 0 ? '+' : ''}${coin.change.toFixed(2)}%
`; } }); listContainer.innerHTML = html; // Update header info if (currentSymbol && currentMarketData[currentSymbol]) { const coin = currentMarketData[currentSymbol]; const lastPriceEl = document.getElementById('last-price'); if (lastPriceEl) { const oldPrice = parseFloat(lastPriceEl.innerText.replace(/[$,]/g, '')) || 0; lastPriceEl.innerText = '$' + coin.price.toLocaleString(undefined, {minimumFractionDigits: 2}); lastPriceEl.className = coin.price >= oldPrice ? 'fw-bold text-success fs-5' : 'fw-bold text-danger fs-5'; const bookPriceEl = document.getElementById('book-price'); if (bookPriceEl) bookPriceEl.innerText = coin.price.toLocaleString(undefined, {minimumFractionDigits: 2}); // If Market tab is active and price field is empty, update it const marketTab = document.getElementById('tab-market'); if (marketTab && marketTab.classList.contains('active')) { const priceInput = document.getElementById('order-price'); if (priceInput && !priceInput.value) { priceInput.value = coin.price; } } } const changeEl = document.getElementById('24h-change'); if (changeEl) { changeEl.innerText = (coin.change >= 0 ? '+' : '') + coin.change.toFixed(2) + '%'; changeEl.className = 'fw-bold ' + (coin.change >= 0 ? 'text-success' : 'text-danger'); } if (document.getElementById('24h-high')) document.getElementById('24h-high').innerText = '$' + (coin.price * 1.05).toLocaleString(undefined, {minimumFractionDigits: 2}); if (document.getElementById('24h-low')) document.getElementById('24h-low').innerText = '$' + (coin.price * 0.95).toLocaleString(undefined, {minimumFractionDigits: 2}); if (document.getElementById('price-fiat')) { const isZh = document.documentElement.lang === 'zh'; const rate = isZh ? 7.2 : 1.0; const symbol = isZh ? '¥' : '$'; document.getElementById('price-fiat').innerText = '≈ ' + symbol + (coin.price * rate).toLocaleString(undefined, {minimumFractionDigits: 2}); } } simulateOrderBook(); } function simulateOrderBook() { const symbol = document.getElementById('current-symbol')?.value; if (!symbol || !currentMarketData[symbol]) return; const basePrice = currentMarketData[symbol].price; const askContainer = document.getElementById('order-book-asks'); const bidContainer = document.getElementById('order-book-bids'); if (!askContainer || !bidContainer) return; let askHtml = ''; let bidHtml = ''; for (let i = 5; i > 0; i--) { const price = basePrice * (1 + (i * 0.0002)); const amount = Math.random() * 2 + 0.1; const depth = Math.random() * 80 + 10; askHtml += `
${price.toFixed(2)} ${amount.toFixed(4)}
`; } for (let i = 1; i <= 5; i++) { const price = basePrice * (1 - (i * 0.0002)); const amount = Math.random() * 2 + 0.1; const depth = Math.random() * 80 + 10; bidHtml += `
${price.toFixed(2)} ${amount.toFixed(4)}
`; } askContainer.innerHTML = askHtml; bidContainer.innerHTML = bidHtml; } // Initialization document.addEventListener('DOMContentLoaded', () => { fetchMarketData(); setInterval(fetchMarketData, 3000); const searchInput = document.getElementById('market-search'); if (searchInput) { searchInput.addEventListener('input', updateUI); } });