<?php
session_start();
include '../config.php';
include 'auth_check.php';

// Auth check handled by auth_check.php

$error = null;
$success = null;

// Handle Form Submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $org_name = htmlspecialchars($_POST['org_name']);
    $email = htmlspecialchars($_POST['email']);
    $password = $_POST['password'];

    // Basic Validation
    if (empty($org_name) || empty($email) || empty($password)) {
        $error = "All fields are required.";
    } else {
        // Check if email already exists
        $stmt = $conn->prepare("SELECT id FROM users WHERE email = ?");
        $stmt->bind_param("s", $email);
        $stmt->execute();
        $stmt->store_result();

        if ($stmt->num_rows > 0) {
            $error = "Email already exists.";
        } else {
            // Hash Password
            $password_hash = password_hash($password, PASSWORD_DEFAULT);
            $stmt->close();

            // Insert User
            $stmt = $conn->prepare("INSERT INTO users (org_name, email, password) VALUES (?, ?, ?)");
            $stmt->bind_param("sss", $org_name, $email, $password_hash);

            if ($stmt->execute()) {
                header("Location: users.php?msg=created");
                exit;
            } else {
                $error = "Error creating user: " . $conn->error;
            }
        }
        $stmt->close();
    }
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Add User - SaniBook Admin</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" />
    <style>
        .form-input {
            width: 100%;
            border: 1px solid #e2e8f0;
            border-radius: 0.5rem;
            padding: 0.5rem 1rem;
            outline: none;
            transition: border-color 0.2s;
        }

        .form-input:focus {
            border-color: #3b82f6;
            ring: 2px solid #93c5fd;
        }
    </style>
</head>

<body class="bg-slate-100 font-sans text-slate-900">

    <?php include 'navbar.php'; ?>

    <div class="max-w-xl mx-auto p-6">
        <div class="flex items-center gap-4 mb-6">
            <a href="users.php" class="text-slate-500 hover:text-blue-600"><i class="fa-solid fa-arrow-left mr-1"></i>
                Back to Users</a>
            <h2 class="text-2xl font-bold text-slate-800">Add New User</h2>
        </div>

        <?php if ($error): ?>
            <div class="bg-red-100 text-red-700 p-4 rounded-lg mb-6 border border-red-200">
                <?php echo $error; ?>
            </div>
        <?php endif; ?>

        <div class="bg-white rounded-xl shadow-sm border border-slate-200 p-8">
            <form method="POST" class="space-y-6">

                <div>
                    <label class="block text-sm font-bold text-slate-700 mb-2">Organization Name / Name</label>
                    <input type="text" name="org_name" required
                        value="<?php echo htmlspecialchars($_POST['org_name'] ?? ''); ?>" class="form-input"
                        placeholder="e.g. WaSH Voice Admin">
                </div>

                <div>
                    <label class="block text-sm font-bold text-slate-700 mb-2">Email</label>
                    <input type="email" name="email" required
                        value="<?php echo htmlspecialchars($_POST['email'] ?? ''); ?>" class="form-input"
                        placeholder="admin@example.com">
                </div>

                <div>
                    <label class="block text-sm font-bold text-slate-700 mb-2">Password</label>
                    <input type="password" name="password" required class="form-input" placeholder="••••••••">
                </div>

                <div class="pt-4">
                    <button type="submit"
                        class="w-full bg-blue-600 hover:bg-blue-700 text-white font-bold py-3 px-8 rounded-lg shadow-lg transition-all transform hover:scale-105">
                        <i class="fa-solid fa-user-plus mr-2"></i> Create User
                    </button>
                </div>

            </form>
        </div>
    </div>
</body>

</html>
