import { useEffect, useState } from 'react'; import { storeClient } from '../../api/clients'; import { STORE, APP_ID } from '../../api/config'; import { Card } from '../reusable/Card'; import { Spinner } from '../reusable/Spinner'; import { EmptyState } from '../reusable/EmptyState'; import { ArrowLeft, Store, User, Truck, MapPin, TrendingUp, Phone, Navigation2, Edit3, Navigation, Mail, FileText, Hash } from 'lucide-react'; import { Button } from '../buttons/Button'; export interface StoreDetailProps { instanceId: string | number; onBack?: () => void; onEdit?: () => void; } export function StoreDetail({ instanceId, onBack, onEdit }: StoreDetailProps) { const [data, setData] = useState | null>(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { let live = true; async function run() { setLoading(true); setError(null); try { const r = await storeClient.detailView(STORE.detailViews.STORE, instanceId); if (live) setData(r.data || {}); } catch (e) { if (live) setError((e as { message?: string })?.message ?? 'Failed to load'); } finally { if (live) setLoading(false); } } run(); return () => { live = false; }; }, [instanceId]); if (error) { return ( ); } if (loading) { return (
); } if (!data || Object.keys(data).length === 0) { return ( ); } const remainingData = { ...data }; const extract = (key: string, obj: any = data) => { if (obj && key in obj) { const val = obj[key]; delete obj[key]; return val; } return null; }; // State const stateName = extract('current_state_name', remainingData) || '-'; delete remainingData.current_state_id; const isSuccess = ['created', 'active', 'approve'].some(s => String(stateName).toLowerCase().includes(s)); const badgeClass = isSuccess ? 'bg-emerald-100 text-emerald-700' : 'bg-blue-100 text-blue-700'; // Store Overview const storeCode = extract('store_code', remainingData) || '-'; const businessName = extract('business_name', remainingData) || '-'; const area = extract('area', remainingData); const completeAddress = extract('complete_address', remainingData); const pinCode = extract('pin_code', remainingData); // Owner Info const ownerName = extract('owner_name', remainingData) || '-'; const email = extract('email', remainingData); let phoneNumber = extract('phone_number', remainingData); let dialPhone = ''; if (typeof phoneNumber === 'object' && phoneNumber !== null) { dialPhone = (phoneNumber as any).phone_with_dial_code || (phoneNumber as any).phone || ''; phoneNumber = (phoneNumber as any).phone_with_dial_code || (phoneNumber as any).phone || '-'; } else { phoneNumber = phoneNumber || '-'; dialPhone = String(phoneNumber); } // Distributor Info const distributorName = extract('distributor_name', remainingData); const distributorOwnerName = extract('distributor_owner_name', remainingData); const distributorEmail = extract('distributor_email', remainingData); let distributorPhone = extract('distributor_phone_number', remainingData); if (typeof distributorPhone === 'object' && distributorPhone !== null) { distributorPhone = (distributorPhone as any).phone_with_dial_code || (distributorPhone as any).phone || '-'; } else { distributorPhone = distributorPhone || '-'; } // Route Info const routeCode = extract('route_code', remainingData); const routeName = extract('route_name', remainingData); const subRoute = extract('sub_route', remainingData); // Location const storeLocation = extract('store_location', remainingData); let lat = null; let lng = null; if (storeLocation) { let locObj = storeLocation; if (typeof locObj === 'string') { try { locObj = JSON.parse(locObj); } catch (e) { } } if (locObj && typeof locObj === 'object') { lat = (locObj as any).latitude; lng = (locObj as any).longitude; } } // Potential const potential = extract('potential', remainingData); let totalPotential = 0; if (Array.isArray(potential)) { potential.forEach((p: any) => { totalPotential += (Number(p.quantity) || 0); }); } // Image const storeImage = extract('store_image', remainingData); let imageUrl = ''; if (Array.isArray(storeImage) && storeImage.length > 0) { imageUrl = `${storeClient.baseUrl}/app/${APP_ID}/view/files/${storeImage[0].uuid}/preview`; } // Meta const createdAtKey = Object.keys(remainingData).find(k => k.endsWith('__created_at')); const createdAt = createdAtKey ? extract(createdAtKey, remainingData) : '-'; const userIdKey = Object.keys(remainingData).find(k => k.endsWith('__user_id')); const userObj = userIdKey ? extract(userIdKey, remainingData) : null; const userName = userObj && typeof userObj === 'object' ? (userObj as any).name || (userObj as any).email : '-'; return (
{/* Top Bar */}
{onBack && ( )}
{String(storeCode)}
{dialPhone && dialPhone !== '-' && ( )} {lat && lng && ( )} {onEdit && ( )}
{/* Hero Card */}
{/* Image Section */} {imageUrl ? (
{String(businessName)}
) : (
)} {/* Content Section */}
{String(stateName)}
{String(storeCode)}

{String(businessName)}

{[completeAddress, area].filter(Boolean).join(', ')} {pinCode ? ` — ${pinCode}` : ''}
{/* Bottom Stats Row */}
Owner
{String(ownerName)}
Route
{String(routeName || '-')}{subRoute ? ` · ${subRoute}` : ''}
Potential
{totalPotential} units
{/* Grid Layout */}
{/* Left Column (Spans 2) */}
{/* Contact Card */}

Contact

Owner
{String(ownerName)}
Phone
{String(phoneNumber)}
Email
{email ? String(email) : '-'}
Address
{[completeAddress, area, pinCode].filter(Boolean).join(', ')}
{/* Route Assignment Card */}

Route Assignment

Route
{routeName ? String(routeName) : '-'}
Code {routeCode ? String(routeCode) : '-'}
Sub Route
{subRoute ? String(subRoute) : '-'}
Area
{area ? String(area) : '-'}
PIN {pinCode ? String(pinCode) : '-'}
{/* Order Potential Card */} {Array.isArray(potential) && potential.length > 0 && (

Order Potential

{potential.map((p: any, idx: number) => { if (!p.product_category && !p.quantity) return null; return ( ); })}
Product Category Quantity
{p.product_category ? String(p.product_category) : '-'} {p.quantity ? String(p.quantity) : '0'}
Total {totalPotential}
)} {/* Distributor Card */}

Distributor

{distributorName ? String(distributorName) : '-'}
Owner · {distributorOwnerName ? String(distributorOwnerName) : '-'}
Phone
{String(distributorPhone)}
Email
{distributorEmail ? String(distributorEmail) : '-'}
{/* Right Column (Spans 1) */}
{/* Location Map */}

Location

{lat && lng ? ( <>
window.open(`https://maps.google.com/maps?q=${lat},${lng}`, '_blank')} >
) : (
No location data
)}
{lat && lng && (
Tap map icon to open in Google Maps.
)}
{/* Meta */}

Meta

Store Code {String(storeCode)}
Instance #{instanceId}
Created by {String(userName)}
Created at {createdAt !== '-' ? new Date(createdAt as string).toLocaleDateString('en-US', { year: 'numeric', month: 'short', day: 'numeric' }) : '-'}
); }