<?php
$pageTitle = "Register - ShadiHall";
require_once 'includes/header.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = clean($conn, $_POST['name']);
    $email = clean($conn, $_POST['email']);
    $phone = clean($conn, $_POST['phone']);
    $password = $_POST['password'];
    $role = clean($conn, $_POST['role']);
    
    if (!in_array($role, ['user', 'vendor'])) {
        $error = "Invalid role.";
    } elseif (strlen($password) < 6) {
        $error = "Password must be at least 6 characters.";
    } else {
        $check = mysqli_query($conn, "SELECT id FROM users WHERE email='$email'");
        if (mysqli_num_rows($check) > 0) {
            $error = "This email is already registered.";
        } else {
            $hashed = password_hash($password, PASSWORD_DEFAULT);
            $status = ($role === 'vendor') ? 'pending' : 'active';
            $q = "INSERT INTO users (name, email, password, phone, role, status) 
                  VALUES ('$name','$email','$hashed','$phone','$role','$status')";
            if (mysqli_query($conn, $q)) {
                if ($role === 'vendor') {
                    $success = "Registration successful! Admin will approve your vendor account soon.";
                } else {
                    $success = "Registration successful! Please login now.";
                }
            } else {
                $error = "Registration failed. Try again.";
            }
        }
    }
}
?>

<div class="form-page">
    <div class="form-container" style="max-width:540px;">
        <h2>Join ShadiHall</h2>
        <p class="form-ornament">Create your account</p>
        
        <?php if (!empty($error)): ?>
            <div class="alert alert-danger"><?php echo $error; ?></div>
        <?php endif; ?>
        <?php if (!empty($success)): ?>
            <div class="alert alert-success"><?php echo $success; ?></div>
        <?php endif; ?>
        
        <form method="POST">
            <div class="form-group">
                <label>Full Name</label>
                <input type="text" name="name" required placeholder="Your name">
            </div>
            <div class="form-group">
                <label>Email Address</label>
                <input type="email" name="email" required placeholder="you@example.com">
            </div>
            <div class="form-group">
                <label>Phone Number</label>
                <input type="text" name="phone" required placeholder="03XXXXXXXXX">
            </div>
            <div class="form-group">
                <label>I am registering as</label>
                <select name="role" required>
                    <option value="user">User — I want to book a hall</option>
                    <option value="vendor">Vendor — I own a shadi hall</option>
                </select>
            </div>
            <div class="form-group">
                <label>Password</label>
                <input type="password" name="password" required minlength="6" placeholder="At least 6 characters">
            </div>
            <button type="submit" class="btn" style="width:100%; margin-top:10px;">Create Account</button>
        </form>
        
        <p style="text-align:center; margin-top:25px; color:#666; font-size:14px;">
            Already have an account? 
            <a href="<?php echo BASE_URL; ?>/login.php" style="color:var(--maroon); font-weight:600; text-decoration:none;">Sign In</a>
        </p>
    </div>
</div>

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