import React, { useState } from "react"; import { View, ScrollView, TextInput, ActivityIndicator, KeyboardAvoidingView, Platform, Pressable, useColorScheme, } from "react-native"; import { useSirouRouter } from "@sirou/react-native"; import { AppRoutes } from "@/lib/routes"; import { Stack } from "expo-router"; import { Text } from "@/components/ui/text"; import { Button } from "@/components/ui/button"; import { Mail, Lock, User, Phone, ArrowRight, ShieldCheck, ChevronDown, } from "@/lib/icons"; import { ScreenWrapper } from "@/components/ScreenWrapper"; import { StandardHeader } from "@/components/StandardHeader"; import { api } from "@/lib/api"; import { toast } from "@/lib/toast-store"; import { PickerModal, SelectOption } from "@/components/PickerModal"; import { getPlaceholderColor } from "@/lib/colors"; const ROLES = ["VIEWER", "EMPLOYEE", "ACCOUNTANT", "CUSTOMER_SERVICE"]; export default function CreateUserScreen() { const nav = useSirouRouter(); const colorScheme = useColorScheme(); const isDark = colorScheme === "dark"; const [form, setForm] = useState({ firstName: "", lastName: "", email: "", phone: "", password: "", role: "VIEWER", }); const [showRolePicker, setShowRolePicker] = useState(false); const [loading, setLoading] = useState(false); const handleCreate = async () => { const { firstName, lastName, email, phone, password, role } = form; if (!firstName || !lastName || !email || !phone || !password) { toast.error("Required Fields", "Please fill in all fields"); return; } setLoading(true); try { // Prepend +251 if not present const formattedPhone = phone.startsWith("+") ? phone : `+251${phone}`; await api.users.create({ body: { ...form, phone: formattedPhone, }, }); toast.success( "User Created!", `${firstName} has been added to the system.`, ); nav.back(); } catch (err: any) { toast.error( "Creation Failed", err.message || "Failed to create user account", ); } finally { setLoading(false); } }; const updateForm = (key: keyof typeof form, val: string) => setForm((prev) => ({ ...prev, [key]: val })); return ( User Details Configure credentials and system access {/* Identity Group */} First Name updateForm("firstName", v)} /> Last Name updateForm("lastName", v)} /> {/* Email */} Email Address updateForm("email", v)} autoCapitalize="none" keyboardType="email-address" /> {/* Phone */} Phone Number updateForm("phone", v)} keyboardType="phone-pad" /> {/* Role - Dropdown */} System Role setShowRolePicker(true)} className="flex-row items-center rounded-xl px-4 border border-border h-12" > {form.role} {/* Password */} Initial Password updateForm("password", v)} secureTextEntry /> setShowRolePicker(false)} title="Select System Role" > {ROLES.map((role) => ( { updateForm("role", v); setShowRolePicker(false); }} /> ))} ); }