import { useState, useEffect } from 'react'; import { mockApplications, locations, ApplicationStatus, Application } from '../../lib/mock-data'; import { onboardingService } from '../../services/onboarding.service'; import { Button } from '../ui/button'; import { Input } from '../ui/input'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '../ui/select'; import { Search, Filter, Download, Mail, Plus } from 'lucide-react'; import { Badge } from '../ui/badge'; import { Checkbox } from '../ui/checkbox'; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from '../ui/table'; import { Progress } from '../ui/progress'; import { Dialog, DialogContent, DialogHeader, DialogTitle } from '../ui/dialog'; import { Label } from '../ui/label'; interface ApplicationsPageProps { onViewDetails: (id: string) => void; initialFilter?: string; } export function ApplicationsPage({ onViewDetails, initialFilter }: ApplicationsPageProps) { const [searchQuery, setSearchQuery] = useState(''); const [locationFilter, setLocationFilter] = useState('all'); const [statusFilter, setStatusFilter] = useState(initialFilter || 'all'); const [selectedIds, setSelectedIds] = useState([]); const [sortBy, setSortBy] = useState<'date'>('date'); const [showNewApplicationModal, setShowNewApplicationModal] = useState(false); // Real Data Integration const [applications, setApplications] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { const fetchApplications = async () => { try { setLoading(true); const response = await onboardingService.getApplications(); // Check if response is array or wrapped in data property const applicationsData = response.data || (Array.isArray(response) ? response : []); // Map backend data to frontend Application interface const mappedApps = applicationsData.map((app: any) => ({ id: app.id, registrationNumber: app.applicationId || 'N/A', name: app.applicantName, email: app.email, phone: app.phone, age: app.age, education: app.education, residentialAddress: app.address || app.city || '', businessAddress: app.address || '', preferredLocation: app.preferredLocation, state: app.state, ownsBike: app.ownRoyalEnfield === 'yes', pastExperience: app.experienceYears ? `${app.experienceYears} years` : (app.description || ''), status: app.overallStatus as ApplicationStatus, questionnaireMarks: 0, rank: 0, totalApplicantsAtLocation: 0, submissionDate: app.createdAt, assignedUsers: [], progress: app.progressPercentage || 0, isShortlisted: true, // Show all for admin view // Add other fields to match interface companyName: app.companyName, source: app.source, existingDealer: app.existingDealer, royalEnfieldModel: app.royalEnfieldModel, description: app.description, pincode: app.pincode, locationType: app.locationType, ownRoyalEnfield: app.ownRoyalEnfield, address: app.address })); setApplications(mappedApps); } catch (error) { console.error('Failed to fetch applications', error); } finally { setLoading(false); } }; fetchApplications(); }, []); // Filter and sort applications - ONLY show shortlisted applications // Exclude specific applications (APP-005, APP-006, APP-007, APP-008) from Dealership Requests page const excludedApplicationIds = ['5', '6', '7', '8']; const filteredApplications = applications .filter((app) => { const matchesSearch = app.name.toLowerCase().includes(searchQuery.toLowerCase()) || app.registrationNumber.toLowerCase().includes(searchQuery.toLowerCase()) || app.email.toLowerCase().includes(searchQuery.toLowerCase()); const matchesLocation = locationFilter === 'all' || app.preferredLocation === locationFilter; const matchesStatus = statusFilter === 'all' || app.status === statusFilter; const isShortlisted = app.isShortlisted === true; // Only show shortlisted applications const notExcluded = !excludedApplicationIds.includes(app.id); // Exclude APP-005, 006, 007, 008 return matchesSearch && matchesLocation && matchesStatus && isShortlisted && notExcluded; }) .sort((a, b) => { if (sortBy === 'date') { return new Date(b.submissionDate).getTime() - new Date(a.submissionDate).getTime(); } return 0; }); const toggleSelection = (id: string) => { setSelectedIds(prev => prev.includes(id) ? prev.filter(i => i !== id) : [...prev, id] ); }; const toggleSelectAll = () => { if (selectedIds.length === filteredApplications.length) { setSelectedIds([]); } else { setSelectedIds(filteredApplications.map(app => app.id)); } }; const handleBulkReminders = () => { alert(`Sending reminders to ${selectedIds.length} applicants`); setSelectedIds([]); }; const handleExport = () => { alert('Exporting applications to CSV...'); }; const getStatusColor = (status: string) => { const statusColors: Record = { 'Submitted': 'bg-slate-500', 'Questionnaire Pending': 'bg-orange-500', 'Questionnaire Completed': 'bg-blue-500', 'Shortlisted': 'bg-cyan-500', 'Level 1 Pending': 'bg-amber-500', 'Level 1 Approved': 'bg-green-500', 'Level 2 Pending': 'bg-purple-500', 'Level 2 Approved': 'bg-green-600', 'Level 2 Recommended': 'bg-teal-500', 'Level 3 Pending': 'bg-indigo-500', 'FDD Verification': 'bg-violet-500', 'Payment Pending': 'bg-yellow-500', 'LOI Issued': 'bg-lime-500', 'Dealer Code Generation': 'bg-fuchsia-500', 'Architecture Team Assigned': 'bg-blue-500', 'Architecture Document Upload': 'bg-blue-500', 'Architecture Team Completion': 'bg-blue-500', 'Statutory GST': 'bg-emerald-500', 'Statutory PAN': 'bg-emerald-500', 'Statutory Nodal': 'bg-emerald-500', 'Statutory Check': 'bg-emerald-500', 'Statutory Partnership': 'bg-emerald-500', 'Statutory Firm Reg': 'bg-emerald-500', 'Statutory Virtual Code': 'bg-emerald-500', 'Statutory Domain': 'bg-emerald-500', 'Statutory MSD': 'bg-emerald-500', 'Statutory LOI Ack': 'bg-emerald-500', 'EOR In Progress': 'bg-sky-500', 'LOA Pending': 'bg-emerald-500', 'Approved': 'bg-green-700', 'Rejected': 'bg-red-500', 'Disqualified': 'bg-red-700' }; return statusColors[status] || 'bg-slate-500'; }; return (
{/* Info Banner - Only visible for DD users */} {/* Note: This page shows only applications that have been shortlisted */} {/* Filters and Actions Bar */}
{/* Search */}
setSearchQuery(e.target.value)} className="pl-10" />
{/* Location Filter */} {/* Status Filter */} {/* Sort By */}
{/* Action Buttons */}
{selectedIds.length > 0 && ( )}
{filteredApplications.length} application{filteredApplications.length !== 1 ? 's' : ''}
{/* Applications Table */}
ID Name Preferred Location Status Applicant Location Progress Applied On Actions {filteredApplications.map((app) => ( toggleSelection(app.id)} /> {app.registrationNumber} {app.name} {app.preferredLocation} {app.status} {app.residentialAddress}
{app.progress}%
{new Date(app.submissionDate).toLocaleDateString()}
))}
{/* New Application Modal */} Add New Application (Admin)

This form allows administrators to manually add applications to the system.

); }