<?php
require_once 'includes/header.php';

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

    if (empty($username) || empty($password)) {
        $error = mt('Please fill in all fields.');
    } elseif ($password !== $confirm_password) {
        $error = mt('Passwords do not match.');
    } elseif (!$agree) {
        $error = mt('You must agree to the terms and privacy policy.');
    } else {
        try {
            $pdo = db();
            // Check if user exists
            $stmt = $pdo->prepare("SELECT id FROM users WHERE username = ?");
            $stmt->execute([$username]);
            if ($stmt->fetch()) {
                $error = mt('Username already exists.');
            } else {
                // Generate unique 6-digit UID
                do {
                    $uid = rand(100000, 999999);
                    $stmt = $pdo->prepare("SELECT id FROM users WHERE uid = ?");
                    $stmt->execute([$uid]);
                } while ($stmt->fetch());

                $hash = password_hash($password, PASSWORD_DEFAULT);
                $stmt = $pdo->prepare("INSERT INTO users (username, uid, email, password_hash, balance_usdt) VALUES (?, ?, ?, ?, ?)");
                $stmt->execute([$username, $uid, $username . '@example.com', $hash, 10000.00]); // Give $10k demo balance
                
                $userId = $pdo->lastInsertId();
                $_SESSION['user_id'] = $userId;
                header('Location: index.php');
                exit;
            }
        } catch (Exception $e) {
            $error = mt('Error') . ': ' . $e->getMessage();
        }
    }
}
?>

<div class="container my-5 py-5">
    <div class="row justify-content-center">
        <div class="col-md-5">
            <div class="card bg-dark border-secondary p-4 shadow-lg" style="border-radius: 20px;">
                <h2 class="text-center mb-4 fw-bold text-white"><?php echo mt('Sign Up'); ?></h2>
                <p class="text-center text-muted mb-4 small"><?php echo mt("Join the world's leading crypto exchange"); ?></p>
                <?php if ($error): ?>
                    <div class="alert alert-danger border-0 small"><?php echo $error; ?></div>
                <?php endif; ?>
                <form method="POST">
                    <div class="mb-3">
                        <label class="form-label text-muted small"><?php echo mt('Username / Email'); ?></label>
                        <input type="text" name="username" class="form-control bg-dark text-white border-secondary py-2" required placeholder="<?php echo mt('Enter username'); ?>">
                    </div>
                    <div class="mb-3">
                        <label class="form-label text-muted small"><?php echo mt('Password'); ?></label>
                        <input type="password" name="password" class="form-control bg-dark text-white border-secondary py-2" required placeholder="<?php echo mt('Enter password'); ?>">
                    </div>
                    <div class="mb-3">
                        <label class="form-label text-muted small"><?php echo mt('Confirm Password'); ?></label>
                        <input type="password" name="confirm_password" class="form-control bg-dark text-white border-secondary py-2" required placeholder="<?php echo mt('Repeat password'); ?>">
                    </div>
                    <div class="mb-4 form-check">
                        <input type="checkbox" name="agree" class="form-check-input" id="agree" required>
                        <label class="form-check-label small text-muted" for="agree">
                            <?php echo mt('I have read and agree to the'); ?> <a href="#" class="text-primary fw-bold text-decoration-none"><?php echo mt('Terms of Service'); ?></a> <?php echo mt('and'); ?> <a href="#" class="text-primary fw-bold text-decoration-none"><?php echo mt('Privacy Policy'); ?></a>.
                        </label>
                    </div>
                    <button type="submit" class="btn btn-primary w-100 py-3 fw-bold mb-3" style="border-radius: 12px; background-color: var(--okx-blue); border: none;"><?php echo mt('Create Account'); ?></button>
                    <div class="text-center">
                        <span class="text-muted small"><?php echo mt('Already have an account?'); ?></span> 
                        <a href="login.php" class="text-primary text-decoration-none fw-bold small ms-1"><?php echo mt('Login'); ?></a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<style>
    .form-control:focus {
        background-color: #2b2f36;
        border-color: var(--okx-blue);
        box-shadow: none;
        color: #fff;
    }
</style>

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