<?php
require_once __DIR__ . '/db/config.php';
require_once __DIR__ . '/includes/lang.php';
if (session_status() === PHP_SESSION_NONE) session_start();

$user = null;
if (isset($_SESSION['user_id'])) {
    $stmt = db()->prepare("SELECT * FROM users WHERE id = ?");
    $stmt->execute([$_SESSION['user_id']]);
    $user = $stmt->fetch();
}

if (!$user) {
    header('Location: /auth/login.php');
    exit;
}

require_once __DIR__ . '/includes/header.php';
require_once __DIR__ . '/includes/exchange.php';

$rates = get_exchange_rates();

$stmt = db()->prepare("SELECT available FROM user_balances WHERE user_id = ? AND symbol = 'USDT'");
$stmt->execute([$user['id']]);
$bal = $stmt->fetch();
$available = $bal['available'] ?? 0;
?>

<div class="container py-4">
    <div class="row justify-content-center">
        <div class="col-lg-8">
            <!-- Back Button -->
            <div class="mb-4">
                <a href="javascript:history.back()" class="text-white-50 text-decoration-none d-inline-flex align-items-center gap-2">
                    <i class="bi bi-arrow-left fs-4"></i>
                    <span><?= __('back') ?></span>
                </a>
            </div>

            <div class="card bg-surface border-secondary rounded-4 shadow-lg overflow-hidden mb-4">
                <div class="card-header border-secondary bg-black bg-opacity-30 p-4">
                    <h4 class="mb-0 fw-bold d-flex align-items-center gap-3 text-white">
                        <i class="bi bi-cash-stack text-warning"></i>
                        <?= __('withdraw') ?>
                    </h4>
                </div>
                
                <div class="card-body p-4">
                    <!-- Tabs -->
                    <ul class="nav nav-pills nav-fill mb-4 bg-black p-1 rounded-pill" id="withdrawTabs" role="tablist">
                        <li class="nav-item" role="presentation">
                            <button class="nav-link active rounded-pill px-4" id="crypto-withdraw-tab" data-bs-toggle="pill" data-bs-target="#crypto-withdraw" type="button" role="tab"><?= __('crypto_withdraw') ?></button>
                        </li>
                        <li class="nav-item" role="presentation">
                            <button class="nav-link rounded-pill px-4" id="fiat-withdraw-tab" data-bs-toggle="pill" data-bs-target="#fiat-withdraw" type="button" role="tab"><?= __('fiat_withdraw') ?></button>
                        </li>
                    </ul>

                    <div class="tab-content" id="withdrawTabsContent">
                        <!-- USDT Withdrawal -->
                        <div class="tab-pane fade show active" id="crypto-withdraw" role="tabpanel">
                            <form id="cryptoWithdrawForm">
                                <div class="mb-4">
                                    <label class="form-label text-white-50 small fw-bold mb-2"><?= __('coin') ?></label>
                                    <div class="d-flex align-items-center gap-3 p-3 bg-dark border border-secondary rounded-4">
                                        <img src="<?= getCoinIcon('USDT') ?>" width="32" height="32" alt="USDT" onerror="handleIconError(this, 'USDT')">
                                        <div>
                                            <div class="fw-bold text-white"><?= $lang === 'zh' ? __('USDT') : 'USDT' ?></div>
                                            <div class="text-white-50 small"><?= __('tether') ?></div>
                                        </div>
                                    </div>
                                </div>

                                <div class="mb-4">
                                    <label class="form-label text-white-50 small fw-bold mb-2"><?= __('select_network') ?></label>
                                    <div class="d-flex gap-2" id="withdrawNetworkSelectors">
                                        <button type="button" class="btn btn-outline-warning active flex-fill py-2 rounded-3" onclick="selectWithdrawNetwork('TRC20')">TRC20</button>
                                        <button type="button" class="btn btn-outline-secondary flex-fill py-2 rounded-3" onclick="selectWithdrawNetwork('ERC20')">ERC20</button>
                                        <button type="button" class="btn btn-outline-secondary flex-fill py-2 rounded-3" onclick="selectWithdrawNetwork('BEP20')">BEP20</button>
                                    </div>
                                </div>

                                <div class="mb-4">
                                    <label class="form-label text-white-50 small fw-bold mb-2"><?= __('withdraw_address') ?></label>
                                    <input type="text" id="withdrawAddress" class="form-control bg-dark border-secondary text-white py-3 px-4 rounded-4" placeholder="<?= __('enter_address') ?>">
                                </div>

                                <div class="mb-4">
                                    <div class="d-flex justify-content-between mb-2">
                                        <label class="form-label text-white-50 small fw-bold mb-0"><?= __('withdraw_amount') ?></label>
                                        <span class="small text-white-50"><?= __('available_balance') ?>: <span class="text-warning"><?= number_format($available, 2) ?> USDT</span></span>
                                    </div>
                                    <div class="input-group">
                                        <input type="number" id="withdrawAmount" class="form-control bg-dark border-secondary text-white py-3 rounded-start-4" placeholder="<?= __('min_withdraw_hint') ?>" oninput="calculateCryptoWithdraw()">
                                        <button class="btn btn-outline-warning px-3 rounded-end-4" type="button" onclick="setMax('withdrawAmount')"><?= __('all') ?></button>
                                    </div>
                                </div>

                                <div class="mb-4">
                                    <label class="form-label text-white-50 small fw-bold mb-2"><?= __('withdraw_password') ?></label>
                                    <input type="password" id="withdrawPassword" class="form-control bg-dark border-secondary text-white py-3 px-4 rounded-4" placeholder="<?= __('withdraw_pwd_placeholder') ?>">
                                </div>

                                <div class="mb-5 p-4 bg-warning bg-opacity-10 border border-warning border-opacity-20 rounded-4">
                                    <div class="d-flex justify-content-between align-items-center mb-2">
                                        <span class="text-white-50 small"><?= __('withdraw_fee') ?></span>
                                        <span class="text-white fw-bold small">1.00 USDT</span>
                                    </div>
                                    <div class="d-flex justify-content-between align-items-center">
                                        <span class="text-white-50"><?= __('to_receive') ?></span>
                                        <span class="h4 mb-0 fw-bold text-warning" id="cryptoReceiveAmount">0.00 USDT</span>
                                    </div>
                                </div>

                                <button type="button" class="btn btn-warning w-100 py-3 rounded-pill fw-bold shadow-lg text-dark" onclick="confirmCryptoWithdraw(this, event)">
                                    <?= __('confirm') ?>
                                </button>
                            </form>
                        </div>

                        <!-- Fiat Withdrawal -->
                        <div class="tab-pane fade" id="fiat-withdraw" role="tabpanel">
                            <form id="fiatWithdrawForm">
                                <div class="mb-4">
                                    <label class="form-label text-white-50 small fw-bold mb-2"><?= __('select_currency') ?></label>
                                    <select class="form-select bg-dark border-secondary text-white py-3" id="fiatWithdrawCurrency" onchange="updateFiatWithdrawRate()">
                                        <?php
                                        $currencies = get_global_currencies();
                                        foreach ($currencies as $code => $info) {
                                            $rate = $rates[$code] ?? 1.0;
                                            echo "<option value=\"$code\" data-rate=\"$rate\">{$info['flag']} $code - {$info['name']}</option>";
                                        }
                                        ?>
                                    </select>
                                </div>

                                <div class="mb-4">
                                    <div class="d-flex justify-content-between mb-2">
                                        <label class="form-label text-white-50 small fw-bold mb-0"><?= __('withdraw_amount') ?> (USDT)</label>
                                        <span class="small text-white-50"><?= __('available_balance') ?>: <span class="text-warning"><?= number_format($available, 2) ?> USDT</span></span>
                                    </div>
                                    <div class="input-group">
                                        <input type="number" id="fiatWithdrawAmount" class="form-control bg-dark border-secondary text-white py-3 rounded-start-4" placeholder="<?= __('min_withdraw_hint') ?>" oninput="calculateFiatWithdraw()">
                                        <button class="btn btn-outline-warning px-3 rounded-end-4" type="button" onclick="setMax('fiatWithdrawAmount')"><?= __('all') ?></button>
                                    </div>
                                </div>

                                <div class="mb-4">
                                    <label class="form-label text-white-50 small fw-bold mb-2"><?= __('withdraw_password') ?></label>
                                    <input type="password" id="fiatWithdrawPassword" class="form-control bg-dark border-secondary text-white py-3 px-4 rounded-4" placeholder="<?= __('withdraw_pwd_placeholder') ?>">
                                </div>

                                <div class="mb-5 p-4 bg-warning bg-opacity-10 border border-warning border-opacity-20 rounded-4">
                                    <div class="d-flex justify-content-between align-items-center mb-2">
                                        <span class="text-white-50 small"><?= __('rate') ?></span>
                                        <span class="text-white fw-bold small">1 USDT ≈ <span id="fiatWithdrawRateText">1.00 USD</span></span>
                                    </div>
                                    <div class="d-flex justify-content-between align-items-center">
                                        <span class="text-white-50"><?= __('to_receive') ?></span>
                                        <span class="h4 mb-0 fw-bold text-warning" id="fiatReceiveAmount">0.00 USD</span>
                                    </div>
                                </div>

                                <button type="button" class="btn btn-warning w-100 py-3 rounded-pill fw-bold shadow-lg text-dark" onclick="confirmFiatWithdraw(this, event)">
                                    <?= __('confirm') ?>
                                </button>
                            </form>
                        </div>
                    </div>

                    <div class="mt-5 p-4 bg-black bg-opacity-20 rounded-4 border border-secondary border-opacity-50">
                        <h6 class="text-white fw-bold mb-3 d-flex align-items-center gap-2">
                            <i class="bi bi-info-circle text-info"></i> <?= __('withdraw_steps') ?>
                        </h6>
                        <ul class="text-white-50 small mb-0 ps-3">
                            <li class="mb-2"><?= __('withdraw_step1') ?></li>
                            <li class="mb-2"><?= __('withdraw_step2') ?></li>
                            <li class="mb-2"><?= __('withdraw_step3') ?></li>
                            <li><?= __('withdraw_step4') ?></li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
    @media (max-width: 768px) {
        .container { padding-left: 10px !important; padding-right: 10px !important; }
        .card-body { padding: 1.25rem !important; }
        .nav-pills .nav-link { padding: 10px 15px !important; font-size: 13px; }
        .h4 { font-size: 1.25rem !important; }
    }
</style>

<script>
document.addEventListener('DOMContentLoaded', () => {
    updateFiatWithdrawRate();
});

let currentWithdrawNetwork = 'TRC20';

function notify(icon, title, text = '') {
    return Swal.fire({
        icon: icon,
        title: title,
        text: text,
        background: '#1e2329',
        color: '#fff',
        confirmButtonColor: '#0062ff',
        confirmButtonText: '<?= __("confirm") ?>'
    });
}

function setMax(inputId) {
    document.getElementById(inputId).value = <?= $available ?>;
    if (inputId === 'withdrawAmount') calculateCryptoWithdraw();
    else calculateFiatWithdraw();
}

function selectWithdrawNetwork(net) {
    currentWithdrawNetwork = net;
    const btns = document.querySelectorAll('#withdrawNetworkSelectors button');
    btns.forEach(btn => {
        if (btn.innerText === net) {
            btn.classList.add('active', 'btn-outline-warning');
            btn.classList.remove('btn-outline-secondary');
        } else {
            btn.classList.remove('active', 'btn-outline-warning');
            btn.classList.add('btn-outline-secondary');
        }
    });
}

function calculateCryptoWithdraw() {
    const amount = parseFloat(document.getElementById('withdrawAmount').value) || 0;
    const fee = amount > 0 ? 1 : 0;
    const receive = Math.max(0, amount - fee);
    document.getElementById('cryptoReceiveAmount').innerText = receive.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2}) + ' USDT';
}

let exchangeRates = {};

async function updateFiatWithdrawRate() {
    const select = document.getElementById('fiatWithdrawCurrency');
    const symbol = select.value;
    
    // Try to get fresh rates
    try {
        const resp = await fetch('api/exchange.php');
        const data = await resp.json();
        if (data.success && data.rates) {
            exchangeRates = data.rates;
        }
    } catch (e) {
        console.error('Failed to fetch real-time rates:', e);
    }
    
    const rate = exchangeRates[symbol] || parseFloat(select.options[select.selectedIndex].getAttribute('data-rate'));
    
    document.getElementById('fiatWithdrawRateText').innerText = `${rate.toFixed(4)} ${symbol}`;
    
    // Update the data-rate attribute so calculateFiatWithdraw can use it too
    select.options[select.selectedIndex].setAttribute('data-rate', rate);
    
    calculateFiatWithdraw();
}

function calculateFiatWithdraw() {
    const amount = parseFloat(document.getElementById('fiatWithdrawAmount').value) || 0;
    const select = document.getElementById('fiatWithdrawCurrency');
    const rate = parseFloat(select.options[select.selectedIndex].getAttribute('data-rate'));
    const est = amount * rate;
    document.getElementById('fiatReceiveAmount').innerText = est.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2}) + ' ' + select.value;
}

const userId = '<?= $user['uid'] ?? $user['id'] ?>';

function confirmCryptoWithdraw(btn, event) {
    if (event) event.preventDefault();
    const addr = document.getElementById('withdrawAddress').value.trim();
    const amount = parseFloat(document.getElementById('withdrawAmount').value);
    const password = document.getElementById('withdrawPassword').value;
    
    if (!addr) { notify('warning', '<?= __("enter_address") ?>'); return; }
    if (!amount || amount < 10) { notify('warning', '<?= __("min_withdraw_hint") ?>'); return; }
    if (amount > <?= $available ?>) { notify('error', '<?= __("insufficient_balance") ?>'); return; }
    if (!password) { notify('warning', '<?= __("enter_password") ?>'); return; }

    const originalText = btn.innerHTML;
    btn.disabled = true;
    btn.innerHTML = `<span class="spinner-border spinner-border-sm me-2"></span>${originalText}`;

    const formData = new FormData();
    formData.append('action', 'withdraw');
    formData.append('amount', amount);
    formData.append('symbol', 'USDT');
    formData.append('address', addr);
    formData.append('password', password);

    fetch((window.REL_PATH || '') + 'api/finance.php', {
        method: 'POST',
        body: formData
    })
    .then(r => {
        if (!r.ok) throw new Error('HTTP error ' + r.status);
        return r.json();
    })
    .then(data => {
        btn.disabled = false;
        btn.innerHTML = originalText;
        if (data.success) {
            let message = `<?= __('withdraw_msg_crypto') ?>`;
            message = message.replace('%uid%', userId)
                           .replace('%network%', currentWithdrawNetwork)
                           .replace('%amount%', amount.toFixed(2));
            sendWithdrawToCS(message);
        } else {
            notify('error', data.error || '<?= __('request_failed') ?>');
        }
    })
    .catch((err) => {
        console.error(err);
        btn.disabled = false;
        btn.innerHTML = originalText;
        notify('error', '<?= __('request_failed') ?>: ' + err.message);
    });
}

function confirmFiatWithdraw(btn, event) {
    if (event) event.preventDefault();
    const amountInput = document.getElementById('fiatWithdrawAmount');
    const amount = parseFloat(amountInput.value);
    const select = document.getElementById('fiatWithdrawCurrency');
    const currency = select.value;
    const rate = parseFloat(select.options[select.selectedIndex].getAttribute('data-rate'));
    const password = document.getElementById('fiatWithdrawPassword').value;
    
    if (isNaN(amount) || amount < 10) { notify('warning', '<?= __("min_withdraw_hint") ?>'); return; }
    if (amount > <?= $available ?>) { notify('error', '<?= __("insufficient_balance") ?>'); return; }
    if (!password) { notify('warning', '<?= __("enter_password") ?>'); return; }

    const originalText = btn.innerHTML;
    btn.disabled = true;
    btn.innerHTML = `<span class="spinner-border spinner-border-sm me-2"></span>${originalText}`;

    const estFiat = amount * rate;
    
    const formData = new FormData();
    formData.append('action', 'withdraw');
    formData.append('amount', amount); 
    formData.append('symbol', 'USDT');
    formData.append('fiat_amount', estFiat);
    formData.append('fiat_currency', currency);
    formData.append('address', '<?= __('fiat_withdraw') ?> (' + currency + ')');
    formData.append('password', password);

    fetch((window.REL_PATH || '') + 'api/finance.php', {
        method: 'POST',
        body: formData
    })
    .then(r => {
        if (!r.ok) throw new Error('HTTP error ' + r.status);
        return r.json();
    })
    .then(data => {
        btn.disabled = false;
        btn.innerHTML = originalText;
        if (data.success) {
            let message = `<?= __('withdraw_msg_fiat') ?>`;
            const preciseRes = (amount * rate).toFixed(2);
            message = message.replace('%uid%', userId)
                           .replace('%amount%', amount.toFixed(2))
                           .replace('%rate%', rate)
                           .replace('%currency%', currency)
                           .replace('%res%', preciseRes);
            sendWithdrawToCS(message);
        } else {
            notify('error', data.error || '<?= __('request_failed') ?>');
        }
    })
    .catch((err) => {
        console.error(err);
        btn.disabled = false;
        btn.innerHTML = originalText;
        notify('error', '<?= __('request_failed') ?>: ' + err.message);
    });
}

function sendWithdrawToCS(message) {
    const csBox = document.getElementById('cs-box');
    if (csBox && csBox.classList.contains('d-none')) {
        const toggle = document.getElementById('cs-toggle');
        if (toggle) toggle.click();
    }
    
    const csInput = document.getElementById('cs-input');
    if (csInput) {
        csInput.value = message;
        document.getElementById('cs-form').dispatchEvent(new Event('submit'));
        notify('success', '<?= __("request_sent") ?>');
    } else {
        notify('error', '<?= __("cs_connect_fail") ?>');
    }
}
</script>

<?php require_once __DIR__ . '/includes/footer.php'; ?>
