import { useState } from 'react'; import { View, Text, TouchableOpacity, ScrollView, Alert, ActivityIndicator, Switch } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; import { useLocalSearchParams, router } from 'expo-router'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { api } from '../../../services/api'; const ROLE_COLOR: Record = { ADMIN: '#7C3AED', STAFF: '#0891B2', TECHNICIAN: '#059669', COLLECTOR: '#D97706' }; const ROLE_BG: Record = { ADMIN: '#F5F3FF', STAFF: '#ECFEFF', TECHNICIAN: '#F0FDF4', COLLECTOR: '#FFFBEB' }; const ROLES = [ { value: 'TECHNICIAN', label: 'Technician', desc: 'Field work & installations' }, { value: 'COLLECTOR', label: 'Collector', desc: 'Payments & remittances' }, { value: 'STAFF', label: 'Staff', desc: 'General access' }, { value: 'ADMIN', label: 'Admin', desc: 'Full access + user mgmt' }, ]; function InfoRow({ label, value }: { label: string; value?: string | null }) { return ( {label} {value ?? '—'} ); } export default function UserDetailScreen() { const { id } = useLocalSearchParams<{ id: string }>(); const qc = useQueryClient(); const [editingRole, setEditingRole] = useState(false); const [newRole, setNewRole] = useState(''); const { data: user, isLoading } = useQuery({ queryKey: ['user', id], queryFn: () => api.get(`/api/v1/users/${id}`).then(r => r.data), }); const toggleActive = useMutation({ mutationFn: (isActive: boolean) => api.patch(`/api/v1/users/${id}`, { isActive }), onSuccess: () => qc.invalidateQueries({ queryKey: ['user', id] }), onError: () => Alert.alert('Error', 'Could not update user status.'), }); const changeRole = useMutation({ mutationFn: (role: string) => api.patch(`/api/v1/users/${id}`, { role }), onSuccess: () => { setEditingRole(false); qc.invalidateQueries({ queryKey: ['user', id] }); qc.invalidateQueries({ queryKey: ['users'] }); }, onError: () => Alert.alert('Error', 'Could not update role.'), }); if (isLoading) { return ( ); } const role = user?.roleAssignments?.[0]?.role ?? 'STAFF'; const roleColor = ROLE_COLOR[role] ?? '#6B7280'; const roleBg = ROLE_BG[role] ?? '#F1F5F9'; const initials = `${user?.firstName?.[0] ?? ''}${user?.lastName?.[0] ?? ''}`.toUpperCase(); const isActive = user?.isActive ?? true; const lastLogin = user?.lastLoginAt ? new Date(user.lastLoginAt).toLocaleDateString('en-PH', { month: 'long', day: 'numeric', year: 'numeric', hour: '2-digit', minute: '2-digit' }) : 'Never'; return ( {/* Header */} router.back()} style={{ alignSelf: 'flex-start', marginBottom: 16 }} activeOpacity={0.7}> ← Back {initials} {user?.firstName} {user?.lastName} {role} {!isActive && ( Inactive Account )} {/* Info Card */} Account Status {isActive ? 'Active' : 'Inactive'} {isActive ? 'User can log in' : 'Login is blocked'} Alert.alert( val ? 'Activate User' : 'Deactivate User', val ? `Allow ${user?.firstName} to log in?` : `Block ${user?.firstName} from logging in?`, [ { text: 'Cancel', style: 'cancel' }, { text: val ? 'Activate' : 'Deactivate', onPress: () => toggleActive.mutate(val), style: val ? 'default' : 'destructive' }, ] )} trackColor={{ false: '#E2E8F0', true: '#0891B2' }} thumbColor="#FFF" /> {/* Role Change */} Role { setEditingRole(!editingRole); setNewRole(role); }} hitSlop={{ top: 10, bottom: 10, left: 10, right: 10 }}> {editingRole ? 'Cancel' : 'Change'} {editingRole && ( {ROLES.map(r => { const isSelected = (newRole || role) === r.value; const rc = ROLE_COLOR[r.value] ?? '#6B7280'; const rb = ROLE_BG[r.value] ?? '#F1F5F9'; return ( setNewRole(r.value)} style={{ flexDirection: 'row', alignItems: 'center', borderRadius: 14, padding: 14, marginBottom: 8, borderWidth: 2, borderColor: isSelected ? rc : '#E2E8F0', backgroundColor: isSelected ? rb : '#FFF' }} activeOpacity={0.7} > {r.label} {r.desc} {isSelected && } ); })} newRole && newRole !== role && Alert.alert( 'Change Role', `Change ${user?.firstName}'s role to ${newRole}?`, [ { text: 'Cancel', style: 'cancel' }, { text: 'Change', onPress: () => changeRole.mutate(newRole) }, ] )} disabled={changeRole.isPending || !newRole || newRole === role} activeOpacity={0.8} > {changeRole.isPending ? : Apply Role Change } )} ); }