<?php
include_once 'config.php';
check_auth();

$user_id = $_SESSION['user_id'];
$account = get_account($user_id);
$settings = get_site_settings();

$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $amount = (float)($_POST['amount'] ?? 0);
    $method = $_POST['pay_method'] ?? 'USDT';
    $tx_hash = $_POST['tx_hash'] ?? 'FIAT_DEPOSIT_' . time();
    
    if ($amount < 10) {
        $error = '最小充值金额为 10 USDT';
    } else {
        $stmt = db()->prepare("INSERT INTO transactions (account_id, transaction_type, currency, pay_method, amount, tx_hash, status) VALUES (?, 'deposit', 'USDT', ?, ?, ?, 'pending')");
        $stmt->execute([$account['id'], $method, $amount, $tx_hash]);
        $success = '充值申请已提交，请等待系统确认';
    }
}

include 'header.php';
?>
<div class="container py-5">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="glass-card p-4">
                <h3 class="text-white mb-4"><i class="bi bi-wallet2 text-warning me-2"></i> 充值中心</h3>
                
                <?php if ($success): ?>
                    <div class="alert alert-success"><?php echo $success; ?></div>
                <?php endif; ?>
                <?php if ($error): ?>
                    <div class="alert alert-danger"><?php echo $error; ?></div>
                <?php endif; ?>

                <ul class="nav nav-pills mb-4 bg-dark p-1 rounded" id="depositTab" role="tablist">
                    <li class="nav-item flex-fill" role="presentation">
                        <button class="nav-link active w-100 text-white" id="usdt-tab" data-bs-toggle="pill" data-bs-target="#usdt-pane" type="button">数字货币 (USDT)</button>
                    </li>
                    <li class="nav-item flex-fill" role="presentation">
                        <button class="nav-link w-100 text-white" id="fiat-tab" data-bs-toggle="pill" data-bs-target="#fiat-pane" type="button">法币充值 (Bank/Alipay)</button>
                    </li>
                </ul>

                <div class="tab-content" id="depositTabContent">
                    <!-- USDT Pane -->
                    <div class="tab-pane fade show active" id="usdt-pane">
                        <div class="row">
                            <div class="col-md-5 text-center mb-4">
                                <div class="bg-white p-3 d-inline-block rounded mb-3">
                                    <img src="https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=<?php echo $settings['deposit_address']; ?>" alt="QR" width="160">
                                </div>
                                <div class="text-secondary small">扫描上方二维码获取地址</div>
                            </div>
                            <div class="col-md-7">
                                <div class="mb-4">
                                    <label class="text-secondary small d-block mb-1">选择网络</label>
                                    <div class="btn-group w-100">
                                        <button class="btn btn-outline-warning active">TRC20</button>
                                        <button class="btn btn-outline-secondary disabled">ERC20</button>
                                        <button class="btn btn-outline-secondary disabled">BEP20</button>
                                    </div>
                                </div>
                                <div class="mb-4">
                                    <label class="text-secondary small d-block mb-1">充值地址</label>
                                    <div class="input-group">
                                        <input type="text" id="addr" class="form-control bg-dark border-secondary text-white" value="<?php echo $settings['deposit_address']; ?>" readonly>
                                        <button class="btn btn-outline-warning" onclick="navigator.clipboard.writeText('<?php echo $settings['deposit_address']; ?>')">复制</button>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <form method="POST" class="mt-4 border-top border-secondary pt-4">
                            <input type="hidden" name="pay_method" value="USDT">
                            <div class="mb-3">
                                <label class="form-label text-secondary small">充值金额 (USDT)</label>
                                <input type="number" name="amount" class="form-control bg-dark border-secondary text-white" placeholder="0.00" required>
                            </div>
                            <div class="mb-3">
                                <label class="form-label text-secondary small">交易哈希 / 凭证号</label>
                                <input type="text" name="tx_hash" class="form-control bg-dark border-secondary text-white" placeholder="请输入 TxID 或 交易流水号" required>
                            </div>
                            <button type="submit" class="btn btn-warning w-100 fw-bold py-3">提交充值申请</button>
                        </form>
                    </div>

                    <!-- Fiat Pane -->
                    <div class="tab-pane fade" id="fiat-pane">
                        <div class="alert alert-info bg-dark border-secondary text-secondary small mb-4">
                            <i class="bi bi-info-circle text-warning me-1"></i> 法币充值按实时汇率折算，当前汇率: 1 USDT ≈ 7.25 CNY
                        </div>
                        
                        <div class="glass-card p-3 mb-4" style="background: rgba(255,255,255,0.03);">
                            <h6 class="text-white mb-3">收款账户信息</h6>
                            <div class="d-flex justify-content-between mb-2 small">
                                <span class="text-secondary">收款银行</span>
                                <span class="text-white">工商银行 (ICBC)</span>
                            </div>
                            <div class="d-flex justify-content-between mb-2 small">
                                <span class="text-secondary">收款人</span>
                                <span class="text-white">BitCrypto Technology Co.</span>
                            </div>
                            <div class="d-flex justify-content-between mb-2 small">
                                <span class="text-secondary">银行账号</span>
                                <span class="text-white">6222 0000 0000 0000 888</span>
                            </div>
                            <div class="d-flex justify-content-between mb-0 small">
                                <span class="text-secondary">开户支行</span>
                                <span class="text-white">上海自贸区支行</span>
                            </div>
                        </div>

                        <form method="POST">
                            <input type="hidden" name="pay_method" value="FIAT">
                            <div class="mb-3">
                                <label class="form-label text-secondary small">支付方式</label>
                                <select name="sub_method" class="form-select bg-dark border-secondary text-white">
                                    <option value="BANK">网银转账</option>
                                    <option value="ALIPAY">支付宝转账</option>
                                    <option value="WECHAT">微信支付</option>
                                </select>
                            </div>
                            <div class="mb-3">
                                <label class="form-label text-secondary small">充值金额 (CNY)</label>
                                <div class="input-group">
                                    <input type="number" id="fiat_amount" name="fiat_amount" class="form-control bg-dark border-secondary text-white" placeholder="0.00" oninput="document.getElementById('usdt_equiv').value = (this.value / 7.25).toFixed(2)">
                                    <span class="input-group-text bg-dark border-secondary text-secondary">CNY</span>
                                </div>
                            </div>
                            <div class="mb-3">
                                <label class="form-label text-secondary small">折合 (USDT)</label>
                                <input type="text" id="usdt_equiv" name="amount" class="form-control bg-dark border-secondary text-white" readonly>
                            </div>
                            <div class="mb-3">
                                <label class="form-label text-secondary small">汇款人姓名</label>
                                <input type="text" name="payer_name" class="form-control bg-dark border-secondary text-white" placeholder="请填写汇款账户实名" required>
                            </div>
                            <button type="submit" class="btn btn-warning w-100 fw-bold py-3">我已完成转账</button>
                        </form>
                    </div>
                </div>

                <div class="mt-4 p-3 rounded" style="background: rgba(255,255,255,0.02); border: 1px solid rgba(255,255,255,0.05);">
                    <h6 class="text-secondary small mb-2">充值说明：</h6>
                    <ul class="text-secondary small ps-3 mb-0">
                        <li>数字货币充值通常在 10-30 分钟内到账。</li>
                        <li>法币充值需要人工审核，工作时间 (9:00-22:00) 约 30 分钟内到账。</li>
                        <li>请务必在汇款备注中填写您的 UID: <span class="text-warning fw-bold"><?php echo $account['uid']; ?></span></li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>
<?php include 'footer.php'; ?>