<?php
include_once 'config.php';
check_auth();
$account = get_account($_SESSION['user_id']);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $amount = (float)($_POST['amount'] ?? 0);
    $address = $_POST['address'] ?? '';
    
    if ($amount >= 10 && $address) {
        if ($account['balance'] >= $amount) {
            try {
                $db = db();
                $db->beginTransaction();
                
                // Deduct balance and freeze it
                $stmt = $db->prepare("UPDATE accounts SET balance = balance - ?, frozen_balance = frozen_balance + ? WHERE id = ?");
                $stmt->execute([$amount, $amount, $account['id']]);
                
                // Record transaction
                $stmt = $db->prepare("INSERT INTO transactions (account_id, transaction_type, amount, tx_hash, status) VALUES (?, 'withdraw', ?, ?, 'pending')");
                $stmt->execute([$account['id'], $amount, $address]);
                
                $db->commit();
                $success = "提现申请已提交，资金已冻结，请等待审核。";
                $account = get_account($_SESSION['user_id']); // refresh
            } catch (Exception $e) {
                $db->rollBack();
                $error = "系统错误: " . $e->getMessage();
            }
        } else {
            $error = "余额不足。";
        }
    } else {
        $error = "请输入有效金额(最小10)和地址。";
    }
}

include 'header.php';
?>
<div class="container py-5">
    <div class="row justify-content-center">
        <div class="col-md-6">
            <div class="glass-card p-4 bg-dark">
                <h4 class="text-white mb-4"><i class="bi bi-box-arrow-up text-warning me-2"></i> 提现 USDT</h4>
                
                <?php if(isset($success)): ?><div class="alert alert-success"><?php echo $success; ?></div><?php endif; ?>
                <?php if(isset($error)): ?><div class="alert alert-danger"><?php echo $error; ?></div><?php endif; ?>

                <div class="mb-4 d-flex justify-content-between">
                    <span class="text-secondary">可用余额:</span>
                    <span class="text-white fw-bold"><?php echo number_format($account['balance'], 2); ?> USDT</span>
                </div>

                <form method="POST">
                    <div class="mb-3">
                        <label class="form-label text-secondary">提现金额</label>
                        <input type="number" name="amount" step="0.01" class="form-control bg-dark text-white border-secondary" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label text-secondary">收币地址 (TRC20)</label>
                        <input type="text" name="address" class="form-control bg-dark text-white border-secondary" placeholder="T..." required>
                    </div>
                    <button type="submit" class="btn btn-warning w-100 fw-bold py-2 mt-3">申请提现</button>
                </form>
            </div>
        </div>
    </div>
</div>
<?php include 'footer.php'; ?>
