import React, { useState } from "react";
import { View, TextInput, StyleSheet, Pressable } from "react-native";
import { useSirouRouter } from "@sirou/react-native";
import { useColorScheme } from "nativewind";
import { api } from "@/lib/api";
import { AppRoutes } from "@/lib/routes";
import { toast } from "@/lib/toast-store";
import { ScreenWrapper } from "@/components/ScreenWrapper";
import { FormFlow } from "@/components/FormFlow";
import { Text } from "@/components/ui/text";
import { PickerModal, SelectOption } from "@/components/PickerModal";
import {
Mail,
Phone,
User,
Lock,
ShieldCheck,
ChevronDown,
} from "@/lib/icons";
const S = StyleSheet.create({
input: {
paddingHorizontal: 12,
paddingVertical: 12,
fontSize: 12,
fontWeight: "500",
borderRadius: 6,
borderWidth: 1,
textAlignVertical: "center",
},
});
function useInputColors() {
const { colorScheme } = useColorScheme();
const dark = colorScheme === "dark";
return {
bg: dark ? "rgba(30,30,30,0.8)" : "rgba(241,245,249,0.2)",
border: dark ? "rgba(255,255,255,0.08)" : "rgba(203,213,225,0.6)",
text: dark ? "#f1f5f9" : "#0f172a",
placeholder: "rgba(100,116,139,0.45)",
};
}
function Field({
label,
value,
onChangeText,
placeholder,
icon,
flex,
numeric = false,
secureTextEntry = false,
}: {
label: string;
value: string;
onChangeText: (v: string) => void;
placeholder: string;
icon?: React.ReactNode;
flex?: number;
numeric?: boolean;
secureTextEntry?: boolean;
}) {
const c = useInputColors();
return (
{label}
{icon}
);
}
const ROLES = ["VIEWER", "EMPLOYEE", "ACCOUNTANT", "CUSTOMER_SERVICE"];
const STEPS = [
{ key: "name", label: "Name" },
{ key: "contact", label: "Contact" },
{ key: "access", label: "Access" },
];
export default function CreateTeamMemberScreen() {
const nav = useSirouRouter();
const { colorScheme } = useColorScheme();
const isDark = colorScheme === "dark";
const iconColor = isDark ? "#94a3b8" : "#64748b";
const [step, setStep] = useState(0);
const [submitting, setSubmitting] = useState(false);
const [showRolePicker, setShowRolePicker] = useState(false);
const [firstName, setFirstName] = useState("");
const [lastName, setLastName] = useState("");
const [email, setEmail] = useState("");
const [phone, setPhone] = useState("");
const [role, setRole] = useState("VIEWER");
const [password, setPassword] = useState("");
const handleNext = () => {
if (step === 0 && (!firstName.trim() || !lastName.trim())) {
toast.error("Validation", "First and last name are required");
return;
}
if (step === 1 && (!email.trim() || !phone.trim())) {
toast.error("Validation", "Email and phone are required");
return;
}
if (step < STEPS.length - 1) setStep(step + 1);
};
const handleBack = () => {
if (step > 0) setStep(step - 1);
};
const handleSubmit = async () => {
if (!password.trim()) {
toast.error("Validation", "Password is required");
return;
}
setSubmitting(true);
try {
const formattedPhone = `+251${phone.trim()}`;
await api.team.create({
body: {
firstName: firstName.trim(),
lastName: lastName.trim(),
email: email.trim(),
phone: formattedPhone,
role,
password: password.trim(),
},
});
toast.success("Team Member Added", `${firstName} has been added to the team.`);
nav.back();
} catch (err: any) {
toast.error("Creation Failed", err.message || "Failed to add team member");
} finally {
setSubmitting(false);
}
};
return (
{step === 0 && (
Personal Info
Enter the team member's full name
}
/>
}
/>
)}
{step === 1 && (
Contact Details
How to reach this team member
}
/>
Phone Number
+251
)}
{step === 2 && (
System Access
Set role and initial password
Role
setShowRolePicker(true)}
style={[S.input, { backgroundColor: useInputColors().bg, borderColor: useInputColors().border, flexDirection: "row", alignItems: "center" }]}
>
{role.replace("_", " ")}
}
secureTextEntry
/>
)}
setShowRolePicker(false)}
title="Select Role"
>
{ROLES.map((r) => (
{
setRole(v);
setShowRolePicker(false);
}}
/>
))}
);
}