<?php
require_once 'db/config.php';
session_start();
$db = db();

$error = '';
$success = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $confirm = $_POST['confirm_password'];
    $email = $_POST['email'];
    
    if ($password !== $confirm) {
        $error = '两次输入的密码不一致';
    } else {
        $hashed = password_hash($password, PASSWORD_DEFAULT);
        try {
            $stmt = $db->prepare("INSERT INTO users (username, password, email, role) VALUES (?, ?, ?, 'user')");
            $stmt->execute([$username, $hashed, $email]);
            $success = '注册成功，请登录';
        } catch (PDOException $e) {
            $error = '用户名或邮箱已存在';
        }
    }
}

$page_title = '注册 - 豪软世界';
require_once 'includes/header.php';
?>

<main class="py-5">
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-5">
                <div class="bg-dark bg-opacity-50 rounded-5 border border-secondary border-opacity-25 p-5 shadow-lg">
                    <div class="text-center mb-5">
                        <h2 class="text-white fw-bold">加入我们</h2>
                        <p class="text-muted">开启您的软件选购之旅</p>
                    </div>

                    <?php if ($error): ?>
                    <div class="alert alert-danger bg-danger bg-opacity-10 border-0 text-danger rounded-4 small">
                        <?php echo $error; ?>
                    </div>
                    <?php endif; ?>

                    <?php if ($success): ?>
                    <div class="alert alert-success bg-success bg-opacity-10 border-0 text-success rounded-4 small">
                        <?php echo $success; ?>
                    </div>
                    <?php endif; ?>

                    <form method="POST">
                        <div class="mb-4">
                            <label class="form-label text-white small fw-bold">用户名</label>
                            <input type="text" name="username" class="form-control bg-black bg-opacity-50 border-secondary border-opacity-50 text-white p-3 rounded-3" required>
                        </div>
                        <div class="mb-4">
                            <label class="form-label text-white small fw-bold">电子邮箱</label>
                            <input type="email" name="email" class="form-control bg-black bg-opacity-50 border-secondary border-opacity-50 text-white p-3 rounded-3" required>
                        </div>
                        <div class="mb-4">
                            <label class="form-label text-white small fw-bold">设置密码</label>
                            <input type="password" name="password" class="form-control bg-black bg-opacity-50 border-secondary border-opacity-50 text-white p-3 rounded-3" required>
                        </div>
                        <div class="mb-5">
                            <label class="form-label text-white small fw-bold">确认密码</label>
                            <input type="password" name="confirm_password" class="form-control bg-black bg-opacity-50 border-secondary border-opacity-50 text-white p-3 rounded-3" required>
                        </div>
                        <button type="submit" class="btn btn-primary btn-lg w-100 py-3 rounded-3 shadow mb-4">立即注册</button>
                        <div class="text-center">
                            <span class="text-muted small">已有账号? <a href="login.php" class="text-primary text-decoration-none">立即登录</a></span>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</main>

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