<?php
require_once '../includes/functions.php';
requireLogin();
requireAdmin();

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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $password = $_POST['password'];
    $balance = $_POST['balance'] ?? 0;

    // Basic validation
    if (empty($name) || empty($email) || empty($phone) || empty($password)) {
        $error = "All fields are required.";
    } else {
        // Check if email or phone already exists
        $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', ?, 'active')");
            if ($stmt->execute([$name, $email, $phone, $hashed_password, $balance])) {
                $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 = "User created successfully! User ID: <b>$generated_username</b>";
            } else {
                $error = "Failed to create user.";
            }
        }
    }
}

include '../includes/header.php';
?>

<div class="row justify-content-center animate-fade-in">
    <div class="col-md-8 col-lg-6">
        <div class="mb-4">
            <h2 class="fw-bold"><i class="fas fa-user-plus text-primary me-2"></i> Add New <span class="text-primary">User</span></h2>
            <p class="text-muted">Create a new user account with custom details.</p>
        </div>

        <div class="glass-card p-5">
            <?php if($error): ?>
                <div class="alert alert-danger border-0 rounded-3 mb-4"><i class="fas fa-exclamation-circle me-2"></i> <?php echo $error; ?></div>
            <?php endif; ?>
            <?php if($success): ?>
                <div class="alert alert-success border-0 rounded-3 mb-4"><i class="fas fa-check-circle me-2"></i> <?php echo $success; ?></div>
            <?php endif; ?>

            <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 form-control-lg bg-light" placeholder="User's Full Name" 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 form-control-lg bg-light" placeholder="email@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 form-control-lg bg-light" placeholder="Phone Number" 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 form-control-lg bg-light" placeholder="Initial Password" required>
                    </div>
                    <div class="col-md-6 mb-4">
                        <label class="form-label fw-semibold small">Initial Balance (₹)</label>
                        <input type="number" name="balance" step="0.01" class="form-control form-control-lg bg-light" value="0.00">
                    </div>
                </div>
                
                <div class="d-flex gap-2">
                    <button type="submit" class="btn btn-premium btn-lg w-100 py-3 shadow">
                        <i class="fas fa-plus-circle me-2"></i> Create User
                    </button>
                    <a href="manage_users.php" class="btn btn-light btn-lg px-4 py-3 border">Cancel</a>
                </div>
            </form>
        </div>
    </div>
</div>

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