<?php
include 'header.php';
require_once 'db/config.php';

$user_id = $_SESSION['user_id'] ?? null;
?>

<style>
    /* Layout Fixes */
    @media (max-width: 991px) {
        .navbar, footer { display: none !important; }
        body { padding-bottom: 65px !important; margin: 0 !important; max-width: 100% !important; }
    }

    .trading-container {
        display: flex;
        background: #0b0e11;
        min-height: calc(100vh - 64px);
        max-width: 100%;
        margin: 0;
        overflow: hidden;
    }
    
    @media (max-width: 991px) {
        .trading-container { min-height: 100vh; flex-direction: column; }
    }
    
    .left-col { width: 280px; flex-shrink: 0; background: #161a1e; display: flex; flex-direction: column; border-right: 1px solid #2b3139; }
    .center-col { flex: 1; min-width: 0; background: #0b0e11; display: flex; flex-direction: column; overflow: hidden; }
    .right-col { width: 320px; flex-shrink: 0; background: #161a1e; display: flex; flex-direction: column; border-left: 1px solid #2b3139; }

    /* Pairs List */
    #pairs-list { flex: 1; overflow-y: auto; }
    .pair-item { display: flex; align-items: center; padding: 12px 15px; cursor: pointer; border-bottom: 1px solid rgba(255,255,255,0.02); transition: all 0.2s; }
    .pair-item.active { background: #1e2329; border-left: 3px solid var(--primary-color); }
    .coin-icon { width: 22px; height: 22px; margin-right: 12px; border-radius: 50%; }

    /* Chart Area */
    .chart-header { padding: 12px 20px; display: flex; align-items: center; gap: 20px; background: #161a1e; border-bottom: 1px solid #2b3139; }
    .chart-box { height: 400px; background: #0b0e11; }
    @media (max-width: 991px) { .chart-box { height: 350px; } }
    
    /* Order Panel */
    .order-box { padding: 20px; background: #161a1e; border-bottom: 1px solid #2b3139; }
    .futures-config { display: flex; gap: 10px; margin-bottom: 15px; }
    .config-btn { flex: 1; background: #2b3139; color: white; padding: 10px; border-radius: 4px; border: 1px solid transparent; cursor: pointer; font-size: 13px; text-align: center; font-weight: 600; }
    .config-btn:hover { border-color: var(--primary-color); }

    .input-group { background: #2b3139; border-radius: 4px; padding: 10px 12px; display: flex; align-items: center; margin-bottom: 10px; border: 1px solid transparent; }
    .input-group:focus-within { border-color: var(--primary-color); }
    .input-group input { background: transparent; border: none; color: white; flex: 1; text-align: right; outline: none; font-size: 14px; width: 100%; }
    .input-group .label { color: #848e9c; font-size: 12px; }
    .input-group .unit { color: #848e9c; margin-left: 8px; font-size: 12px; }

    /* Slider Style */
    .slider-container { margin: 15px 0 25px; padding: 0 5px; }
    .slider-labels { display: flex; justify-content: space-between; margin-top: 8px; font-size: 10px; color: #848e9c; }
    input[type=range] { -webkit-appearance: none; width: 100%; height: 4px; background: #2b3139; border-radius: 2px; outline: none; }
    input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; width: 18px; height: 18px; background: var(--primary-color); border-radius: 50%; cursor: pointer; border: 3px solid #161a1e; }

    .trade-actions { display: flex; gap: 12px; margin-top: 15px; }
    .trade-btn { flex: 1; padding: 14px; border-radius: 4px; font-weight: bold; font-size: 15px; border: none; cursor: pointer; transition: opacity 0.2s; }
    .btn-long { background: #0ecb81; color: white; }
    .btn-short { background: #f6465d; color: white; }

    /* Records */
    .record-tabs { display: flex; background: #161a1e; border-bottom: 1px solid #2b3139; padding: 0 20px; }
    .record-tab { padding: 15px 0; margin-right: 30px; font-size: 13px; color: #848e9c; cursor: pointer; position: relative; }
    .record-tab.active { color: white; border-bottom: 2px solid var(--primary-color); font-weight:bold; }
    .records-content { min-height: 300px; background: #161a1e; }
    @media (max-width: 991px) { .records-content { padding-bottom: 80px; } }

    /* Order Book */
    .ob-header { padding: 10px 15px; font-size: 11px; color: #848e9c; display: flex; justify-content: space-between; border-bottom: 1px solid #2b3139; }
    .ob-row { display: flex; justify-content: space-between; padding: 4px 15px; font-size: 12px; font-family: monospace; position: relative; cursor: pointer; }
    .ob-bar { position: absolute; right: 0; top: 0; bottom: 0; opacity: 0.1; z-index: 0; }
    #mid-price { padding: 12px 15px; font-size: 18px; font-weight: 800; text-align: center; border-top: 1px solid #2b3139; border-bottom: 1px solid #2b3139; }

    /* Mobile Headers */
    .m-trading-header { display: none; background: #161a1e; padding: 12px 15px; border-bottom: 1px solid #2b3139; }
    .m-currency-bar { display: none; overflow-x: auto; background: #161a1e; padding: 10px; gap: 10px; border-bottom: 1px solid #2b3139; scrollbar-width: none; }
    .m-nav-tabs { display: none; background: #161a1e; border-bottom: 1px solid #2b3139; }
    @media (max-width: 991px) { .m-trading-header, .m-currency-bar, .m-nav-tabs { display: flex; } .m-trading-header { flex-direction: column; } }
    .m-coin-item { flex-shrink: 0; background: #2b3139; padding: 6px 12px; border-radius: 4px; display: flex; align-items: center; gap: 6px; border: 1px solid transparent; }
    .m-coin-item.active { border-color: var(--primary-color); background: rgba(0, 82, 255, 0.1); }
    .m-coin-item img { width: 16px; height: 16px; border-radius: 50%; }
    .m-nav-tab { flex:1; text-align:center; padding:12px; font-size:14px; color:#848e9c; border-bottom:2px solid transparent; }
    .m-nav-tab.active { color:white; border-bottom-color:var(--primary-color); font-weight:bold; }

    /* Modal */
    .modal-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.8); z-index: 2000; display: none; align-items: center; justify-content: center; }
    .modal-content { background: #1e2329; width: 90%; max-width: 400px; padding: 25px; border-radius: 12px; }
</style>

<!-- Mobile Trading Header -->
<div class="m-trading-header">
    <div style="display: flex; justify-content: space-between; align-items: center;">
        <div style="display: flex; align-items: center; gap: 10px;" onclick="toggleSidebar()">
            <i class="fas fa-bars" style="font-size: 18px;"></i>
            <div style="font-size: 18px; font-weight: 800; display: flex; align-items: center; gap: 6px;">
                <img id="m-curr-icon" src="" style="width:24px; height:24px; border-radius:50%">
                <span id="m-curr-pair">BTCUSDT Perp</span>
                <i class="fas fa-chevron-down" style="font-size: 10px; opacity: 0.5;"></i>
            </div>
        </div>
        <div style="text-align: right">
            <div id="m-curr-price" style="font-size: 22px; font-weight: 800; color: #0ecb81; line-height: 1">--</div>
            <div id="m-curr-change" style="font-size: 12px; font-weight: 600; margin-top: 4px">--</div>
        </div>
    </div>
    <div style="display: flex; gap: 15px; margin-top: 10px; font-size: 11px; color: #848e9c;">
        <span id="m-24h-high">H: --</span><span id="m-24h-low">L: --</span><span id="m-24h-vol">V: --</span>
    </div>
</div>

<div class="m-currency-bar" id="m-coin-list"></div>

<div class="m-nav-tabs">
    <div class="m-nav-tab active" onclick="switchMobileView('trade')"><?php echo __('nav_trade'); ?></div>
    <div class="m-nav-tab" onclick="switchMobileView('chart')"><?php echo __('chart'); ?></div>
    <div class="m-nav-tab" onclick="switchMobileView('orders')"><?php echo __('positions'); ?></div>
</div>

<div class="trading-container">
    <div class="left-col d-none d-lg-flex">
        <div class="market-header" style="padding:15px; font-weight:bold; border-bottom:1px solid #2b3139"><?php echo __('nav_market'); ?></div>
        <div id="pairs-list"></div>
    </div>

    <div class="center-col m-view active" id="view-trade">
        <div class="chart-header d-none d-lg-flex">
            <div style="display: flex; align-items: center; gap: 12px;">
                <img id="curr-icon" src="" class="coin-icon" style="margin:0; width:28px; height:28px;">
                <span id="curr-pair" style="font-weight: 800; font-size: 18px;">BTCUSDT Perpetual</span>
            </div>
            <div style="flex:1"></div>
            <div style="text-align: right">
                <div id="curr-price" style="font-size: 20px; font-weight: 800; color: #0ecb81;">--</div>
                <div id="curr-change" style="font-size: 12px; font-weight: 600; color: #0ecb81;">--</div>
            </div>
        </div>
        
        <div class="chart-box d-none d-lg-block">
            <div id="tv_chart_container_desk" style="height: 100%;"></div>
        </div>

        <div class="order-box">
            <div class="futures-config">
                <div class="config-btn" id="margin-mode" onclick="toggleMarginMode()"><?php echo __('cross'); ?></div>
                <div class="config-btn" id="leverage-val" onclick="toggleLeverageModal()">20x</div>
            </div>
            <div class="input-group">
                <span class="label"><?php echo __('price'); ?></span>
                <input type="number" id="futures-price" placeholder="Market Price">
                <span class="unit">USDT</span>
            </div>
            <div class="input-group">
                <span class="label"><?php echo __('amount'); ?></span>
                <input type="number" id="futures-amount" placeholder="0.00">
                <span class="unit coin-name">BTC</span>
            </div>
            <div class="slider-container">
                <input type="range" min="0" max="100" value="0" step="1" oninput="setSlider(this.value)">
                <div class="slider-labels"><span>0%</span><span>25%</span><span>50%</span><span>75%</span><span>100%</span></div>
            </div>
            <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 10px;">
                <div class="input-group"><span class="label">TP</span><input type="number" id="futures-tp" placeholder="Price"></div>
                <div class="input-group"><span class="label">SL</span><input type="number" id="futures-sl" placeholder="Price"></div>
            </div>
            <div style="display: flex; justify-content: space-between; font-size: 12px; color: #848e9c; margin-bottom: 12px;">
                <span>Available</span><span id="futures-avail" style="color:white; font-weight:600">0.00 USDT</span>
            </div>
            <div class="trade-actions">
                <button onclick="placeOrder('buy')" class="trade-btn btn-long"><?php echo __('buy_long'); ?></button>
                <button onclick="placeOrder('sell')" class="trade-btn btn-short"><?php echo __('sell_short'); ?></button>
            </div>
        </div>

        <div class="record-tabs">
            <div class="record-tab active" onclick="switchRecords('positions')"><?php echo __('positions'); ?></div>
            <div class="record-tab" onclick="switchRecords('history')"><?php echo __('history_orders'); ?></div>
            <div class="record-tab" onclick="switchRecords('trades')"><?php echo __('settled'); ?></div>
        </div>
        <div id="records-list" class="records-content"></div>
    </div>

    <div class="center-col m-view" id="view-chart" style="display:none">
        <div class="chart-box"><div id="tv_chart_container_mob" style="height: 100%;"></div></div>
    </div>
    <div class="center-col m-view" id="view-orders" style="display:none">
        <div class="record-tabs">
            <div class="record-tab active" onclick="switchRecords('positions')"><?php echo __('positions'); ?></div>
            <div class="record-tab" onclick="switchRecords('history')"><?php echo __('history_orders'); ?></div>
            <div class="record-tab" onclick="switchRecords('trades')"><?php echo __('settled'); ?></div>
        </div>
        <div id="m-records-list" class="records-content"></div>
    </div>

    <div class="right-col d-none d-lg-flex">
        <div class="ob-header"><span>Price</span><span>Amount</span></div>
        <div id="asks-list" style="flex: 1; display: flex; flex-direction: column-reverse; overflow: hidden;"></div>
        <div id="mid-price">--</div>
        <div id="bids-list" style="flex: 1; overflow: hidden;"></div>
    </div>
</div>

<div class="modal-overlay" id="leverage-modal">
    <div class="modal-content">
        <div style="display:flex; justify-content:space-between; margin-bottom:20px;"><span style="font-weight:bold; font-size:16px;">Adjust Leverage</span><i class="fas fa-times" onclick="toggleLeverageModal()" style="cursor:pointer; color:#848e9c"></i></div>
        <div style="text-align:center; font-size:32px; font-weight:800; color:var(--primary-color); margin-bottom:10px;" id="slider-val">20x</div>
        <div class="slider-container">
            <input type="range" id="leverage-range" min="20" max="100" value="20" oninput="updateSliderVal(this.value)">
            <div style="display:flex; justify-content:space-between; font-size:12px; color:#848e9c; margin-top:10px;"><span>20x</span><span>100x</span></div>
        </div>
        <button class="trade-btn btn-long" style="width:100%;" onclick="confirmLeverage()">Confirm</button>
    </div>
</div>

<script type="text/javascript" src="https://s3.tradingview.com/tv.js"></script>
<script>
    let currentPair = 'BTCUSDT'; let currentPrice = 0; let currentLeverage = 20; let marginMode = 'cross'; let currentStatus = 'positions';
    const pairs = ['BTCUSDT', 'ETHUSDT', 'BNBUSDT', 'SOLUSDT', 'XRPUSDT', 'ADAUSDT', 'DOGEUSDT', 'DOTUSDT', 'MATICUSDT', 'LTCUSDT', 'SHIBUSDT', 'TRXUSDT', 'AVAXUSDT', 'LINKUSDT', 'BCHUSDT', 'UNIUSDT', 'ETCUSDT', 'NEARUSDT', 'FILUSDT', 'ALGOUSDT', 'FTMUSDT', 'SANDUSDT', 'MANAUSDT', 'AXSUSDT', 'ATOMUSDT', 'HBARUSDT', 'ICPUSDT', 'VETUSDT'];
    const marketData = {}; let balances = {usdt: 0};

    function getIcon(s) { return `https://cdn.jsdelivr.net/gh/atomiclabs/cryptocurrency-icons@1a72df150d6d8481e2345e691456c6c7d1e84a22/32/color/${s.replace('USDT', '').toLowerCase()}.png`; }
    function initChart(symbol, container) { new TradingView.widget({ "width": "100%", "height": "100%", "symbol": "BINANCE:" + symbol, "interval": "15", "theme": "dark", "style": "1", "locale": "<?php echo $lang == 'zh' ? 'zh_CN' : 'en'; ?>", "container_id": container, "backgroundColor": "#0b0e11", "hide_side_toolbar": true }); }

    const ws = new WebSocket('wss://stream.binance.com:9443/ws/' + pairs.map(p => p.toLowerCase() + '@ticker').join('/'));
    ws.onmessage = (e) => {
        const d = JSON.parse(e.data);
        if (d.s === currentPair) { currentPrice = parseFloat(d.c); updatePriceUI(d); renderOrderBook(); }
        marketData[d.s] = d; renderPairs();
    };

    function updatePriceUI(d) {
        const color = d.P>=0 ? '#0ecb81' : '#f6465d';
        document.getElementById('curr-price').innerText = currentPrice.toLocaleString();
        document.getElementById('m-curr-price').innerText = currentPrice.toLocaleString();
        document.getElementById('curr-price').style.color = color;
        document.getElementById('m-curr-price').style.color = color;
        document.getElementById('curr-change').innerText = (d.P>=0?'+':'')+d.P+'%';
        document.getElementById('m-curr-change').innerText = (d.P>=0?'+':'')+d.P+'%';
        document.getElementById('m-24h-high').innerText = 'H: ' + parseFloat(d.h).toLocaleString();
        document.getElementById('m-24h-low').innerText = 'L: ' + parseFloat(d.l).toLocaleString();
        document.getElementById('m-24h-vol').innerText = 'V: ' + (parseFloat(d.v)/1000).toFixed(1) + 'K';
        document.getElementById('mid-price').innerText = currentPrice.toLocaleString();
        document.getElementById('mid-price').style.color = color;
    }

    function renderPairs() {
        const list = document.getElementById('pairs-list'); const mBar = document.getElementById('m-coin-list');
        let lH = ''; let mH = '';
        pairs.forEach(p => {
            const d = marketData[p]||{c:0,P:0}; const icon = getIcon(p); const active = p === currentPair; const color = d.P >= 0 ? '#0ecb81' : '#f6465d';
            lH += `<div class="pair-item ${active?'active':''}" onclick="switchPair('${p}')"><img src="${icon}" class="coin-icon"> <div style="flex:1"><div style="font-weight:700; font-size:13px">${p}</div></div> <div style="text-align:right"><div style="font-size:13px; font-weight:bold">${parseFloat(d.c).toLocaleString()}</div><div style="font-size:11px; color:${color}">${(d.P>=0?'+':'')+d.P}%</div></div></div>`;
            mH += `<div class="m-coin-item ${active?'active':''}" onclick="switchPair('${p}')"><img src="${icon}"><span>${p.replace('USDT','')}</span></div>`;
        });
        if(list) list.innerHTML = lH; if(mBar) mBar.innerHTML = mH;
    }

    function switchPair(p) {
        currentPair = p; const icon = getIcon(p);
        document.getElementById('curr-pair').innerText = p + ' Perpetual'; document.getElementById('m-curr-pair').innerText = p + ' Perp';
        document.getElementById('curr-icon').src = icon; document.getElementById('m-curr-icon').src = icon;
        document.querySelectorAll('.coin-name').forEach(el => el.innerText = p.replace('USDT',''));
        initChart(p, "tv_chart_container_desk"); initChart(p, "tv_chart_container_mob"); updateBalance();
    }

    function toggleMarginMode() { marginMode = marginMode === 'cross' ? 'isolated' : 'cross'; document.getElementById('margin-mode').innerText = marginMode.toUpperCase(); }
    function toggleLeverageModal() { const modal = document.getElementById('leverage-modal'); modal.style.display = modal.style.display === 'flex' ? 'none' : 'flex'; }
    function updateSliderVal(v) { document.getElementById('slider-val').innerText = v + 'x'; }
    function confirmLeverage() { currentLeverage = document.getElementById('leverage-range').value; document.getElementById('leverage-val').innerText = currentLeverage + 'x'; toggleLeverageModal(); }
    function setSlider(val) { const pct = val / 100; const p = parseFloat(document.getElementById('futures-price').value) || currentPrice; if(p>0) document.getElementById('futures-amount').value = (balances.usdt * pct * currentLeverage / p).toFixed(4); }

    async function placeOrder(side) {
        const p = parseFloat(document.getElementById('futures-price').value) || currentPrice; const a = parseFloat(document.getElementById('futures-amount').value);
        if(!a || a<=0) return alert('Invalid amount');
        const resp = await fetch('api/place_order.php', { method:'POST', body:JSON.stringify({ symbol:currentPair, type:'futures', side:side, order_type:'market', price:p, amount:a, total:p*a, leverage:currentLeverage, tp_price:document.getElementById('futures-tp').value, sl_price:document.getElementById('futures-sl').value }) });
        const res = await resp.json();
        if(res.success) { alert('Order placed'); updateBalance(); fetchOrders(); } else alert(res.error);
    }

    function renderOrderBook() {
        const asks = document.getElementById('asks-list'); const bids = document.getElementById('bids-list');
        let aH = ''; let bH = '';
        for(let i=8; i>0; i--) { const p = currentPrice * (1+i*0.0001); const amt = Math.random()*2; aH += `<div class="ob-row"><div class="ob-bar" style="width:${amt*25}%; background:#f6465d"></div><span style="color:#f6465d">${p.toFixed(1)}</span><span>${amt.toFixed(3)}</span></div>`; }
        for(let i=1; i<=8; i++) { const p = currentPrice * (1-i*0.0001); const amt = Math.random()*2; bH += `<div class="ob-row"><div class="ob-bar" style="width:${amt*25}%; background:#0ecb81"></div><span style="color:#0ecb81">${p.toFixed(1)}</span><span>${amt.toFixed(3)}</span></div>`; }
        if(asks) asks.innerHTML = aH; if(bids) bids.innerHTML = bH;
    }

    async function updateBalance() {
        const resp = await fetch('api/get_assets.php'); const res = await resp.json();
        if(res.success) { const usdt=res.data.find(a=>a.symbol==='USDT'); balances.usdt=usdt?parseFloat(usdt.amount):0; document.getElementById('futures-avail').innerText=balances.usdt.toFixed(2)+' USDT'; }
    }

    async function fetchOrders() {
        const resp = await fetch(`api/get_orders.php?type=futures&status=${currentStatus}`); const res = await resp.json();
        const l = document.getElementById('records-list'); const mL = document.getElementById('m-records-list');
        let h = res.success && res.data.length > 0 ? res.data.map(o => `<div style="padding:15px; border-bottom:1px solid #2b3139; font-size:12px"><div style="display:flex; justify-content:space-between; align-items:center"><span style="color:${o.side==='buy'?'#0ecb81':'#f6465d'}; font-weight:bold; font-size:14px">${o.side==='buy'?'Long':'Short'} ${o.symbol} ${o.leverage}x</span><span style="color:#848e9c">${o.created_at}</span></div><div style="display:grid; grid-template-columns:1fr 1fr 1fr; gap:10px; color:#848e9c; margin-top:10px"><div>Size<br><span style="color:white; font-weight:bold">${o.amount}</span></div><div>Price<br><span style="color:white; font-weight:bold">${parseFloat(o.price).toFixed(2)}</span></div><div>TP/SL<br><span style="color:white; font-weight:bold">${o.tp_price||'--'}/${o.sl_price||'--'}</span></div></div></div>`).join('') : '<div style="padding:40px; text-align:center; color:#848e9c">No records</div>';
        if(l) l.innerHTML = h; if(mL) mL.innerHTML = h;
    }

    function switchRecords(s) { currentStatus = s; document.querySelectorAll('.record-tab').forEach(t => t.classList.remove('active')); event.target.classList.add('active'); fetchOrders(); }
    function switchMobileView(view) { document.querySelectorAll('.m-nav-tab').forEach(n => n.classList.remove('active')); event.target.classList.add('active'); document.querySelectorAll('.m-view').forEach(v => v.style.display = 'none'); document.getElementById('view-' + view).style.display = 'block'; if(view === 'orders') fetchOrders(); }

    switchPair(currentPair); updateBalance(); fetchOrders();
</script>
<?php include 'footer.php'; ?>