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

$settings_stmt = $pdo->query("SELECT registration_enabled FROM settings WHERE id = 1");
$settings_res = $settings_stmt->fetch();
if (isset($settings_res['registration_enabled']) && !$settings_res['registration_enabled']) {
    die("Registration is currently disabled.");
}

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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = trim($_POST['name']);
    $email = trim($_POST['email']);
    $phone = trim($_POST['phone']);
    $password = $_POST['password'];
    $confirm_password = $_POST['confirm_password'];

    if ($password !== $confirm_password) {
        $error = "Passwords do not match";
    } else {
        $stmt = $pdo->prepare("SELECT id FROM users WHERE email = ? OR phone = ?");
        $stmt->execute([$email, $phone]);
        if ($stmt->fetch()) {
            $error = "Email or Phone already exists";
        } else {
            $hashed_password = password_hash($password, PASSWORD_DEFAULT);
            $stmt = $pdo->prepare("INSERT INTO users (name, email, phone, password, role, balance, status) VALUES (?, ?, ?, ?, 'user', 0.00, 'active')");
            if ($stmt->execute([$name, $email, $phone, $hashed_password])) {
                $last_id = $pdo->lastInsertId();
                $generated_username = 'USER' . str_pad($last_id, 5, '0', STR_PAD_LEFT);
                $pdo->prepare("UPDATE users SET username = ? WHERE id = ?")->execute([$generated_username, $last_id]);
                $success = "Registration successful! Your User ID is: <b>$generated_username</b>. You can now login.";
            } else {
                $error = "Registration failed. Try again.";
            }
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Register - <?php echo $site_name; ?></title>
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="assets/css/style.css">
    <style>
        body { 
            background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 2rem 0;
        }
        .register-card { 
            width: 100%;
            max-width: 550px;
            padding: 3rem;
            background: rgba(255, 255, 255, 0.9);
            backdrop-filter: blur(10px);
            border-radius: 24px;
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.1);
        }
        .form-control {
            border-radius: 12px;
            padding: 0.75rem 1rem;
            border: 1px solid #e2e8f0;
        }
    </style>
</head>
<body>

<div class="register-card animate-fade-in">
    <div class="text-center mb-5">
        <div class="bg-primary bg-opacity-10 d-inline-block p-3 rounded-circle mb-3">
            <i class="fas fa-user-plus text-primary fa-2x"></i>
        </div>
        <h2 class="fw-bold m-0">Create Account</h2>
        <p class="text-muted small">Join our platform today</p>
    </div>

    <?php if($error): ?>
        <div class="alert alert-danger border-0 rounded-3 small animate-fade-in"><?php echo $error; ?></div>
    <?php endif; ?>

    <?php if($success): ?>
        <div class="alert alert-success border-0 rounded-4 p-4 text-center animate-fade-in">
            <i class="fas fa-check-circle fa-3x mb-3 text-success"></i>
            <h4 class="fw-bold">Success!</h4>
            <p class="mb-3"><?php echo $success; ?></p>
            <a href="login.php" class="btn btn-premium w-100">Login Now</a>
        </div>
    <?php else: ?>
    <form method="POST">
        <div class="row">
            <div class="col-md-12 mb-4">
                <label class="form-label fw-semibold small">Full Name</label>
                <input type="text" name="name" class="form-control" placeholder="John Doe" required>
            </div>
            <div class="col-md-6 mb-4">
                <label class="form-label fw-semibold small">Email Address</label>
                <input type="email" name="email" class="form-control" placeholder="john@example.com" required>
            </div>
            <div class="col-md-6 mb-4">
                <label class="form-label fw-semibold small">Phone Number</label>
                <input type="text" name="phone" class="form-control" placeholder="+91 ..." required>
            </div>
            <div class="col-md-6 mb-4">
                <label class="form-label fw-semibold small">Password</label>
                <input type="password" name="password" class="form-control" placeholder="••••••••" required>
            </div>
            <div class="col-md-6 mb-4">
                <label class="form-label fw-semibold small">Confirm Password</label>
                <input type="password" name="confirm_password" class="form-control" placeholder="••••••••" required>
            </div>
        </div>
        
        <button type="submit" class="btn btn-premium w-100 py-3 mb-4 shadow">
            Create My Account <i class="fas fa-arrow-right ms-2"></i>
        </button>
    </form>
    <?php endif; ?>

    <div class="text-center">
        <p class="text-muted small mb-0">Already have an account?</p>
        <a href="login.php" class="text-primary fw-bold text-decoration-none">Login to Account</a>
    </div>
</div>

</body>
</html>
