import { useNavigate, useParams } from 'react-router-dom'; import { Modal } from '../components/reusable'; import { CallsView } from '../components/rv'; import { CallDetail } from '../components/dv'; import { useState } from 'react'; import { Button } from '../components/buttons/Button'; import { Plus } from 'lucide-react'; import { DynamicForm } from '../components/forms/DynamicForm'; import { ORDER_BOOKING } from '../api/config'; import { orderBookingClient } from '../api/clients'; export function CallsPage() { const params = useParams(); const instanceId = params.instanceId ? Number(params.instanceId) : undefined; const navigate = useNavigate(); const [isCreating, setIsCreating] = useState(false); const [refreshKey, setRefreshKey] = useState(0); const [activeActivity, setActiveActivity] = useState<{ id: string; name: string } | null>(null); const [isFabOpen, setIsFabOpen] = useState(false); const [miningLoading, setMiningLoading] = useState(false); const [miningPrefill, setMiningPrefill] = useState | undefined>(); const [selectedRow, setSelectedRow] = useState | null>(null); const handlePotentialMiningClick = async () => { setMiningLoading(true); setMiningPrefill(undefined); try { const storeCode = selectedRow?.store_code || selectedRow?.code || selectedRow?.store?.store_code; if (!storeCode) { console.warn("Store code not found on selected row, proceeding anyway."); } const payload = { store_code: storeCode, instance_id: String(instanceId) }; const response = await orderBookingClient.request<{ potential: { potential: any[] } }>( 'POST', '/api/papi2/potential-mining', payload, { 'TemplateID': '146' } ); const rawPotential = response.potential?.potential || []; const mappedPotential = rawPotential.map((row: any) => { const cat = row.product_category || row.product_category_ || row.category; return { ...row, product_category_: cat, product_category: cat, productcategory: cat, category: cat, product_category_1: cat }; }); setMiningPrefill({ potential: mappedPotential }); setActiveActivity({ id: ORDER_BOOKING.activities.POTENTIAL_MINING.uid, name: 'Potential Mining' }); } catch (e: any) { alert("Failed to fetch potential mining data: " + (e.message || "Unknown error")); } finally { setMiningLoading(false); setIsFabOpen(false); } }; return ( <> { setSelectedRow(row); const id = row.instance_id as number | string | undefined; if (id != null) navigate(`/calls/${id}`); }} headerActions={ } /> setIsCreating(false)} title="Log Visit" width="md" > { setIsCreating(false); setRefreshKey(k => k + 1); }} onCancel={() => setIsCreating(false)} /> { navigate(`/calls`); setIsFabOpen(false); }} title={instanceId != null ? `Call #${instanceId}` : undefined} width="lg" > {instanceId != null && ( <>
{isFabOpen && (
)}
)}
setActiveActivity(null)} title={activeActivity?.name} width="md" > {activeActivity && instanceId != null && ( { setActiveActivity(null); setRefreshKey(k => k + 1); }} onCancel={() => setActiveActivity(null)} /> )} ); }