import { View, Text, Modal, ScrollView, TouchableOpacity, KeyboardAvoidingView, Platform } from 'react-native'; import { useState } from 'react'; import { X } from 'lucide-react-native'; import { Input } from '@/components/ui/Input'; import { Button } from '@/components/ui/Button'; import { DatePicker } from '@/components/ui/DatePicker'; import { TopicSelector } from '@/components/visits/TopicSelector'; import { useUserStore } from '@/store/useUserStore'; import { getDatabase } from '@/lib/database'; import { newVisitId } from '@/lib/visitHelpers'; interface Props { contactId: string; contactName: string; visible: boolean; onClose: () => void; onSaved: () => void; } const RESPONSE_OPTIONS = ['Interested', 'Not Interested', 'Busy', 'No Answer', 'Do Not Call']; export function LogVisitSheet({ contactId, contactName, visible, onClose, onSaved }: Props) { const user = useUserStore((s) => s.user); const [visitDate, setVisitDate] = useState(Math.floor(Date.now() / 1000)); const [topic, setTopic] = useState(''); const [response, setResponse] = useState(''); const [remarks, setRemarks] = useState(''); const [nextVisitDate, setNextVisitDate] = useState(null); const [loading, setLoading] = useState(false); function reset() { setVisitDate(Math.floor(Date.now() / 1000)); setTopic(''); setResponse(''); setRemarks(''); setNextVisitDate(null); } async function handleSave() { setLoading(true); try { const db = await getDatabase(); const now = Math.floor(Date.now() / 1000); const id = newVisitId(); await db.runAsync( `INSERT INTO visits (id, contact_id, visited_by_name, visited_by_id, visit_date, topic, response, remarks, next_visit_date, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, [id, contactId, user?.displayName ?? 'Unknown', user?.id ?? 'unknown', visitDate, topic || null, response || null, remarks.trim() || null, nextVisitDate, now, now] ); // Update contact status if response is Do Not Call if (response === 'Do Not Call') { await db.runAsync('UPDATE contacts SET status=?, updated_at=? WHERE id=?', ['Do Not Call', now, contactId]); } else if (response === 'Interested') { await db.runAsync('UPDATE contacts SET status=?, updated_at=? WHERE id=?', ['Return Visit', now, contactId]); } reset(); onSaved(); } finally { setLoading(false); } } return ( { reset(); onClose(); }}> { reset(); onClose(); }} accessibilityRole="button" accessibilityLabel="Close" style={{ minWidth: 44, minHeight: 44, alignItems: 'center', justifyContent: 'center' }} > Log Visit Contact: {contactName} Response {RESPONSE_OPTIONS.map((r) => ( setResponse(r === response ? '' : r)} className={`px-3 py-1.5 rounded-full border ${response === r ? 'bg-primary border-primary' : 'bg-white border-gray-200'}`} accessibilityRole="button" accessibilityLabel={r} accessibilityState={{ selected: response === r }} style={{ minHeight: 36 }} > {r} ))}