import React, { useState, useEffect } from 'react'; import { Text, View, TouchableOpacity, ScrollView, Switch, Image, Alert, } from 'react-native'; import AsyncStorage from '@react-native-async-storage/async-storage'; import { useNavigation } from '@react-navigation/native'; import Icon from 'react-native-vector-icons/Ionicons'; import { getStyles } from './profile.styles'; import { useTheme } from '../../theme'; import { ProfileInfoRow } from '@components'; import { useAppDispatch, useAppSelector, logout, RootState } from '@store'; import { NotificationService } from '@services'; export const ProfileScreen = () => { const navigation = useNavigation(); const { theme: colors, isDark, toggleTheme } = useTheme(); const styles = getStyles(colors); const dispatch = useAppDispatch(); const [pushEnabled, setPushEnabled] = useState(true); const userData = useAppSelector((state: RootState) => state.auth.user_data); const handleLogout = async () => { try { const fcmToken = await NotificationService.getFCMToken(); console.log('fcmToken', fcmToken) await dispatch( logout({ id: userData?.staffid ?? '', fcm_token: fcmToken ?? '', }), ).unwrap(); navigation.reset({ index: 0, routes: [{ name: 'AuthStack' }], }); } catch (e: any) { const message = typeof e === 'string' ? e : e?.message || 'Logout failed. Please try again.'; Alert.alert('Logout Failed', message); } }; // Derive user info const fullName = userData?.full_name || `${userData?.firstname || ''} ${userData?.lastname || ''}`.trim() || 'User Profile'; const email = userData?.email || 'N/A'; const phone = userData?.phonenumber || 'N/A'; const staffId = userData?.staffid ? `#${userData.staffid}` : 'N/A'; const isAdmin = userData?.admin === '1'; const roleText = isAdmin ? 'Administrator' : 'Staff Member'; const isActive = userData?.active === '1'; const [profileImageUrl, setProfileImageUrl] = useState(''); useEffect(() => { const resolveImageUrl = async () => { if (!userData?.profile_image) { setProfileImageUrl(''); return; } const rawImage = userData.profile_image.replace(/"/g, '').trim(); if (!rawImage) { setProfileImageUrl(''); return; } if ( rawImage.startsWith('http') || rawImage.startsWith('file://') || rawImage.startsWith('content://') ) { setProfileImageUrl(rawImage); return; } const storedBaseUrl = await AsyncStorage.getItem('base_url'); const baseUrl = storedBaseUrl || 'https://demo-convexcrm.convexsol.co'; const cleanBase = baseUrl.replace(/\/+$/, ''); const cleanPath = rawImage.replace(/^\/+/, ''); setProfileImageUrl(`${cleanBase}/${cleanPath}`); }; resolveImageUrl(); }, [userData?.profile_image]); const hasProfileImage = !!profileImageUrl; // Derive initials const initials = fullName .split(' ') .filter(Boolean) .map(n => n[0]) .join('') .substring(0, 2) .toUpperCase() || 'U'; const hasSocials = Boolean(userData?.linkedin) || Boolean(userData?.facebook) || Boolean(userData?.skype); return ( {/* ── Header Card ── */} {hasProfileImage ? ( ) : ( {initials} )} {isActive && } {fullName} {email} {roleText} {/* ── Stats Summary Row ── */} {userData?.staffid || '—'} Staff ID {userData?.total_unfinished_todos ?? '0'} Pending Todos {userData?.total_unread_notifications ?? '0'} Unread Alerts {/* ── Personal Details ── */} Personal Details {/* ── System & Account ── */} System & Account {userData?.default_language ? ( ) : null} {/* ── Social Profiles ── */} {hasSocials && ( Social Profiles {userData?.linkedin ? ( ) : null} {userData?.facebook ? ( ) : null} {userData?.skype ? ( ) : null} )} {/* ── Preferences ── */} Preferences Dark Mode {/* Push Notifications */} Push Notifications {/* ── Sign Out ── */} Sign Out ); };