import { RefreshCcw, Plus, Eye, Calendar, FileText, Loader2 } from 'lucide-react'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '../ui/card'; import { Badge } from '../ui/badge'; import { Button } from '../ui/button'; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '../ui/table'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from '../ui/dialog'; import { Input } from '../ui/input'; import { Label } from '../ui/label'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '../ui/select'; import { Textarea } from '../ui/textarea'; import { useState, useEffect } from 'react'; import { User as UserType } from '../../lib/mock-data'; import { toast } from 'sonner'; import { dealerService } from '../../services/dealer.service'; interface DealerConstitutionalChangePageProps { currentUser: UserType | null; onViewDetails?: (id: string) => void; } const getStatusColor = (status: string) => { if (status === 'Completed') return 'bg-green-100 text-green-700 border-green-300'; if (status.includes('Review') || status.includes('Pending')) return 'bg-yellow-100 text-yellow-700 border-yellow-300'; if (status.includes('Rejected')) return 'bg-red-100 text-red-700 border-red-300'; return 'bg-slate-100 text-slate-700 border-slate-300'; }; const constitutionTypes = ['Proprietorship', 'Partnership', 'LLP', 'Pvt Ltd']; export function DealerConstitutionalChangePage({ currentUser, onViewDetails }: DealerConstitutionalChangePageProps) { const [isDialogOpen, setIsDialogOpen] = useState(false); const [currentConstitution, setCurrentConstitution] = useState(''); const [proposedConstitution, setProposedConstitution] = useState(''); const [reason, setReason] = useState(''); const [newPartners, setNewPartners] = useState(''); const [shareholdingPattern, setShareholdingPattern] = useState(''); const [requests, setRequests] = useState([]); const [loading, setLoading] = useState(true); const [submitting, setSubmitting] = useState(false); const [profile, setProfile] = useState(null); useEffect(() => { fetchData(); }, []); const fetchData = async () => { try { setLoading(true); const dashboard = await dealerService.getDashboardData(); const constitutionalRes = await dealerService.getConstitutionalChanges(); setProfile(dashboard.profile); setCurrentConstitution(dashboard.profile?.constitutionType || 'Proprietorship'); setRequests(constitutionalRes.requests || []); } catch (error) { console.error('Fetch constitutional data error:', error); toast.error('Failed to load requests'); } finally { setLoading(false); } }; const handleSubmitRequest = async (e: React.FormEvent) => { e.preventDefault(); if (!proposedConstitution) { toast.error('Please select proposed constitution type'); return; } if (currentConstitution === proposedConstitution) { toast.error('Proposed constitution must be different from current'); return; } if (!reason.trim()) { toast.error('Please provide a reason for constitutional change'); return; } try { setSubmitting(true); const payload = { currentConstitution, changeType: proposedConstitution, reason, newPartnersDetails: newPartners, shareholdingPattern }; await dealerService.submitConstitutionalChange(payload); toast.success('Constitutional change request submitted successfully'); setIsDialogOpen(false); fetchData(); // Refresh list // Reset form setProposedConstitution(''); setReason(''); setNewPartners(''); setShareholdingPattern(''); } catch (error) { console.error('Submit constitutional change error:', error); toast.error('Failed to submit constitutional change request'); } finally { setSubmitting(false); } }; const stats = [ { title: 'Total Requests', value: requests.length, icon: RefreshCcw, color: 'bg-blue-500', }, { title: 'Pending', value: requests.filter(r => r.status !== 'Completed' && r.status !== 'Rejected').length, icon: Calendar, color: 'bg-yellow-500', }, { title: 'Completed', value: requests.filter(r => r.status === 'Completed').length, icon: FileText, color: 'bg-green-500', }, ]; return (
{/* Loading Overlay */} {loading && (
)} {!loading && ( <> {/* Header */}

My Constitutional Change Requests

Submit and track requests for changing your business constitution

Submit Constitutional Change Request Request to change your dealership's business constitution structure
{/* Dealer Info */}

Current Dealership Information

Dealer Code:

{profile?.dealerCode || 'N/A'}

Dealer Name:

{profile?.businessName || 'N/A'}

Current Constitution:

{currentConstitution}

{/* Constitution Change */}
{/* Reason */}