<?php
require_once 'config/config.php';

$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = clean_input($_POST['name']);
    $business = clean_input($_POST['business_name']);
    $phone = clean_input($_POST['phone']);
    $email = clean_input($_POST['email']);
    $pass = $_POST['password'];
    $confirm = $_POST['confirm_password'];

    // Basic Validation
    if ($pass !== $confirm) {
        $error = "Passwords do not match!";
    } elseif (strlen($pass) < 6) {
        $error = "Password must be at least 6 characters.";
    } else {
        // Check if email exists
        $stmt = $pdo->prepare("SELECT id FROM users WHERE email = ?");
        $stmt->execute([$email]);
        if ($stmt->rowCount() > 0) {
            $error = "Email already registered.";
        } else {
            // Create User
            $hash = password_hash($pass, PASSWORD_DEFAULT);
            $sql = "INSERT INTO users (name, business_name, email, phone_whatsapp, password_hash, role) VALUES (?, ?, ?, ?, ?, 'user')";
            $stmt = $pdo->prepare($sql);
            if ($stmt->execute([$name, $business, $email, $phone, $hash])) {
                // Success - Redirect to login
                header("Location: login.php?registered=1");
                exit;
            } else {
                $error = "Something went wrong. Try again.";
            }
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Register - Team Hydra Support</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light d-flex align-items-center justify-content-center" style="height: 100vh;">

<div class="card shadow p-4" style="max-width: 500px; width: 100%;">
    <h3 class="text-center mb-4">Create Partner Account</h3>
    
    <?php if($error): ?>
        <div class="alert alert-danger"><?php echo $error; ?></div>
    <?php endif; ?>

    <form method="POST">
        <div class="mb-3">
            <label>Full Name</label>
            <input type="text" name="name" class="form-control" required>
        </div>
        <div class="mb-3">
            <label>Business Name</label>
            <input type="text" name="business_name" class="form-control" required>
        </div>
        <div class="mb-3">
            <label>WhatsApp Number</label>
            <input type="text" name="phone" class="form-control" required placeholder="e.g. 917038146526">
        </div>
        <div class="mb-3">
            <label>Email Address</label>
            <input type="email" name="email" class="form-control" required>
        </div>
        <div class="row">
            <div class="col-6 mb-3">
                <label>Password</label>
                <input type="password" name="password" class="form-control" required>
            </div>
            <div class="col-6 mb-3">
                <label>Confirm</label>
                <input type="password" name="confirm_password" class="form-control" required>
            </div>
        </div>
        <button type="submit" class="btn btn-primary w-100">Register</button>
    </form>
    <div class="text-center mt-3">
        <a href="login.php">Already have an account? Login</a>
    </div>
</div>

</body>
</html>