import React, { useEffect, useMemo, useState } from 'react'; import { View, Text, TouchableOpacity, ScrollView, KeyboardAvoidingView, Platform, Alert, Switch, ActivityIndicator, } from 'react-native'; import Icon from 'react-native-vector-icons/Ionicons'; import { getStyles } from './addCustomer.styles'; import { useTheme } from '@theme'; import { FormInput, FormPicker } from '@components'; import { RootState, useAppDispatch, useAppSelector } from '@store'; import { addCustomer, resetAddCustomerState } from './thunk'; import { getCustomers } from '../customers/thunk'; import { useNavigation } from '@react-navigation/native'; import { customerGroups } from '@mock-data'; export const AddCustomerScreen = () => { const { theme: colors } = useTheme(); const styles = getStyles(colors); const dispatch = useAppDispatch(); const navigation = useNavigation(); const userData = useAppSelector((state: RootState) => state.auth.user_data); const { loading, successMessage, error } = useAppSelector( (state: RootState) => state.addCustomer, ); const { items: countryItems } = useAppSelector( (state: RootState) => state.countryList, ); const { items: languageItems } = useAppSelector( (state: RootState) => state.languageList, ); const { currencies: currencyItems } = useAppSelector( (state: RootState) => state.currencyList, ); // Form inputs const [company, setCompany] = useState(''); const [vat, setVat] = useState(''); const [phonenumber, setPhonenumber] = useState(''); const [website, setWebsite] = useState(''); const [selectedGroups, setSelectedGroups] = useState([1]); // default group 1 selected const [defaultLanguage, setDefaultLanguage] = useState('system_default'); const [defaultCurrency, setDefaultCurrency] = useState('1'); // default to USD // Address const [address, setAddress] = useState(''); const [city, setCity] = useState(''); const [state, setState] = useState(''); const [zip, setZip] = useState(''); const [country, setCountry] = useState(''); // Billing Address const [billingStreet, setBillingStreet] = useState(''); const [billingCity, setBillingCity] = useState(''); const [billingState, setBillingState] = useState(''); const [billingZip, setBillingZip] = useState(''); const [billingCountry, setBillingCountry] = useState(''); // Shipping Address const [shippingStreet, setShippingStreet] = useState(''); const [shippingCity, setShippingCity] = useState(''); const [shippingState, setShippingState] = useState(''); const [shippingZip, setShippingZip] = useState(''); const [shippingCountry, setShippingCountry] = useState(''); // Auto-fill switches const [billingSameAsGeneral, setBillingSameAsGeneral] = useState(false); const [shippingSameAsBilling, setShippingSameAsBilling] = useState(false); const countryOptions = useMemo( () => countryItems.map(c => ({ label: c.short_name, value: c.country_id })), [countryItems], ); const languageOptions = useMemo( () => languageItems.map(lang => ({ label: lang.value, value: lang.id })), [languageItems], ); const currencyOptions = useMemo( () => currencyItems.map(c => ({ label: `${c.name} (${c.symbol})`, value: c.id })), [currencyItems], ); // Handle auto-fill logic for Billing useEffect(() => { if (billingSameAsGeneral) { setBillingStreet(address); setBillingCity(city); setBillingState(state); setBillingZip(zip); setBillingCountry(country); } }, [billingSameAsGeneral, address, city, state, zip, country]); // Handle auto-fill logic for Shipping useEffect(() => { if (shippingSameAsBilling) { setShippingStreet(billingStreet); setShippingCity(billingCity); setShippingState(billingState); setShippingZip(billingZip); setShippingCountry(billingCountry); } }, [shippingSameAsBilling, billingStreet, billingCity, billingState, billingZip, billingCountry]); // Handle Success/Error from Redux State useEffect(() => { if (successMessage) { Alert.alert('Success', successMessage, [ { text: 'OK', onPress: () => { dispatch(resetAddCustomerState()); dispatch(getCustomers()); // Refresh customers list view navigation.goBack(); }, }, ]); } else if (error) { Alert.alert('Error', error); dispatch(resetAddCustomerState()); } }, [successMessage, error, dispatch, navigation]); const toggleGroup = (groupId: number) => { if (selectedGroups.includes(groupId)) { setSelectedGroups(selectedGroups.filter(id => id !== groupId)); } else { setSelectedGroups([...selectedGroups, groupId]); } }; const handleSubmit = () => { if (!company.trim()) { Alert.alert('Error', 'Company Name is required.'); return; } const staffId = userData?.staffid || ''; // Convert selectedGroups array to JSON format matching requirement: {"0":1,"1":2} const groupsInObj: { [key: string]: number } = {}; selectedGroups.forEach((groupId, index) => { groupsInObj[index.toString()] = groupId; }); const payload = { company: company.trim(), vat: vat.trim(), phonenumber: phonenumber.trim(), website: website.trim(), groups_in: JSON.stringify(groupsInObj), default_language: defaultLanguage, default_currency: defaultCurrency, address: address.trim(), city: city.trim(), state: state.trim(), zip: zip.trim(), country: country, billing_street: billingStreet.trim(), billing_city: billingCity.trim(), billing_state: billingState.trim(), billing_zip: billingZip.trim(), billing_country: billingCountry, shipping_street: shippingStreet.trim(), shipping_city: shippingCity.trim(), shipping_state: shippingState.trim(), shipping_zip: shippingZip.trim(), shipping_country: shippingCountry, addedfrom: staffId, }; dispatch(addCustomer(payload)); }; return ( Customer Info {/* Groups Selection */} Groups {customerGroups.map(group => { const isSelected = selectedGroups.includes(group.id); return ( toggleGroup(group.id)} style={{ paddingHorizontal: 12, paddingVertical: 8, borderRadius: 20, borderWidth: 1, borderColor: isSelected ? colors.icon : colors.border, backgroundColor: isSelected ? colors.icon : colors.surface, }} activeOpacity={0.7} > {group.name} ); })} {/* Primary Address */} Primary Address {/* Billing Address */} Billing Address Same as primary {!billingSameAsGeneral && ( <> )} {/* Shipping Address */} Shipping Address Same as billing {!shippingSameAsBilling && ( <> )} {/* Submit Button */} {loading ? ( ) : ( <> Create Customer )} ); };