<?php
include_once 'config.php';

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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';
    $confirm_password = $_POST['confirm_password'] ?? '';

    if ($username && $password && $confirm_password) {
        if ($password !== $confirm_password) {
            $error = '两次输入的密码不一致';
        } else {
            $db = db();
            $stmt = $db->prepare("SELECT id FROM users WHERE username = ?");
            $stmt->execute([$username]);
            if ($stmt->fetch()) {
                $error = '用户名已存在';
            } else {
                try {
                    $db->beginTransaction();
                    
                    $hashed_password = password_hash($password, PASSWORD_DEFAULT);
                    $stmt = $db->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
                    $stmt->execute([$username, $hashed_password]);
                    $user_id = $db->lastInsertId();
                    
                    $uid = str_pad(mt_rand(0, 999999), 6, '0', STR_PAD_LEFT);
                    $stmt = $db->prepare("INSERT INTO accounts (user_id, uid, balance) VALUES (?, ?, ?)");
                    $stmt->execute([$user_id, $uid, 10000.00]); // Default 10000 USDT for simulated
                    
                    $db->commit();
                    $success = '注册成功！正在跳转到登录页...';
                    header("Refresh: 2; URL=login.php");
                } catch (Exception $e) {
                    $db->rollBack();
                    $error = '注册失败: ' . $e->getMessage();
                }
            }
        }
    } else {
        $error = '请填写所有字段';
    }
}

include 'header.php';
?>
<div class="auth-container d-flex align-items-center justify-content-center" style="min-height: 90vh; background: radial-gradient(circle at top right, #1e2329 0%, #0b0e11 100%);">
    <div class="auth-card glass-card p-5" style="width: 100%; max-width: 450px; border-radius: 16px;">
        <div class="text-center mb-5">
            <h2 class="fw-bold text-white">创建账户</h2>
            <p class="text-secondary">加入全球领先的交易社区</p>
        </div>
        
        <?php if ($error): ?>
            <div class="alert alert-danger"><?php echo $error; ?></div>
        <?php endif; ?>
        <?php if ($success): ?>
            <div class="alert alert-success"><?php echo $success; ?></div>
        <?php endif; ?>

        <form method="post">
            <div class="mb-4">
                <label class="form-label text-secondary small fw-bold">用户名</label>
                <input type="text" name="username" class="form-control bg-transparent text-white border-secondary" placeholder="请输入用户名" required>
            </div>
            
            <div class="mb-4">
                <label class="form-label text-secondary small fw-bold">密码</label>
                <input type="password" name="password" class="form-control bg-transparent text-white border-secondary" placeholder="请输入密码" required>
            </div>

            <div class="mb-4">
                <label class="form-label text-secondary small fw-bold">确认密码</label>
                <input type="password" name="confirm_password" class="form-control bg-transparent text-white border-secondary" placeholder="请再次输入密码" required>
            </div>
            
            <button type="submit" class="btn btn-warning w-100 py-3 fw-bold shadow-lg mb-4">立即注册</button>
        </form>

        <div class="text-center">
            <p class="text-secondary small">已有账户? <a href="/login.php" class="text-warning text-decoration-none fw-bold">立即登录</a></p>
        </div>
    </div>
</div>
<?php include 'footer.php'; ?>
