'use client'; import { useState, useEffect } from 'react'; import { useParams, useRouter } from 'next/navigation'; import { ArrowLeft, Bike, User, Calendar, DollarSign, Wallet, Shield, CheckCircle, XCircle, Clock, Edit, Save, Plus, Trash2, Image, Upload, Lock, Unlock, AlertTriangle, MessageSquare, MapPin, Phone, MessageCircle } from 'lucide-react'; type RentalStatus = 'active' | 'pending' | 'completed' | 'disputed' | 'cancelled' | 'locked'; type RentalType = 'single' | 'shared' | 'rent-to-own'; interface BikeImage { id: string; type: 'front' | 'back' | 'left' | 'right'; url?: string; } interface Note { id: string; text: string; createdAt: string; } interface Rental { id: string; bikeId: string; userId: string; type: RentalType; status: RentalStatus; startDate: string; endDate?: string; deposit: number; dailyRate: number; totalPaid: number; dueRental?: number; lockedAt?: string; lockedReason?: string; hubId?: string; hubName?: string; } const mockRentals: Rental[] = [ { id: 'RNT-001', bikeId: 'BIKE-001', userId: 'USR-001', type: 'single', status: 'active', startDate: '2024-01-15', deposit: 5000, dailyRate: 300, totalPaid: 81900, dueRental: 0, hubId: 'HUB-001', hubName: 'Gulshan Hub' }, { id: 'RNT-002', bikeId: 'BIKE-002', userId: 'USR-002', type: 'shared', status: 'pending', startDate: '2024-02-01', deposit: 3000, dailyRate: 200, totalPaid: 2000, dueRental: 0, hubId: 'HUB-002', hubName: 'Banani Hub' }, { id: 'RNT-003', bikeId: 'BIKE-003', userId: 'USR-003', type: 'rent-to-own', status: 'completed', startDate: '2023-06-01', endDate: '2023-12-01', deposit: 10000, dailyRate: 500, totalPaid: 150000, hubId: 'HUB-001', hubName: 'Gulshan Hub' } ]; const mockBikes: Record = { 'BIKE-001': { id: 'BIKE-001', model: 'AIMA Lightning', plate: 'Dhaka Metro Cha-9012', status: 'active', odometer: 3510, batteryHealth: 85, images: [ { id: 'img1', type: 'front', url: '' }, { id: 'img2', type: 'back', url: '' }, { id: 'img3', type: 'left', url: '' }, { id: 'img4', type: 'right', url: '' } ] }, 'BIKE-002': { id: 'BIKE-002', model: 'Yadea DT3', plate: 'Dhaka Metro Ba-5521', status: 'active', odometer: 2100, batteryHealth: 92, images: [] } }; const mockUsers: Record = { 'USR-001': { id: 'USR-001', name: 'Rahim Ahmed', phone: '+8801712345678', email: 'rahim@example.com', walletBalance: 2100, membership: 'vip', joinedFrom: 'Facebook', kycStatus: 'verified', insurance: 'active', insuranceExpiry: '2024-12-01' }, 'USR-002': { id: 'USR-002', name: 'Karim Hasan', phone: '+8801812345678', email: 'karim@example.com', walletBalance: 500, membership: 'standard', joinedFrom: 'Referral', kycStatus: 'pending', insurance: 'none' } }; const mockHubs = [ { id: 'HUB-001', name: 'Gulshan Hub', address: 'Gulshan 1, Dhaka' }, { id: 'HUB-002', name: 'Banani Hub', address: 'Banani, Dhaka' }, { id: 'HUB-003', name: 'Uttara Hub', address: 'Uttara, Dhaka' } ]; export default function RentalDetailPage() { const params = useParams(); const router = useRouter(); const id = params.id as string; const [rental, setRental] = useState(null); const [user, setUser] = useState(null); const [bike, setBike] = useState(null); const [editMode, setEditMode] = useState(false); const [notes, setNotes] = useState([ { id: 'n1', text: 'Initial rental started. Bike in good condition.', createdAt: '2024-01-15' }, { id: 'n2', text: 'Battery replaced on 2024-01-20.', createdAt: '2024-01-20' } ]); const [newNote, setNewNote] = useState(''); const [editForm, setEditForm] = useState>({}); const [showLockModal, setShowLockModal] = useState(false); const [lockReason, setLockReason] = useState(''); const [dueAmount, setDueAmount] = useState(0); const [showDueModal, setShowDueModal] = useState(false); const [showImageModal, setShowImageModal] = useState(false); const [uploadImageType, setUploadImageType] = useState(''); useEffect(() => { const found = mockRentals.find(r => r.id === id); if (found) { setRental(found); setEditForm(found); setUser(mockUsers[found.userId as keyof typeof mockUsers] || null); setBike(mockBikes[found.bikeId as keyof typeof mockBikes] || mockBikes['BIKE-001']); } }, [id]); if (!rental) { return (

Rental not found

); } const handleSaveEdit = () => { setRental(prev => prev ? { ...prev, ...editForm } : null); setEditMode(false); }; const handleLockRental = () => { if (!lockReason.trim()) return; setRental(prev => prev ? { ...prev, status: 'locked', lockedAt: new Date().toISOString(), lockedReason: lockReason } : null); setShowLockModal(false); setLockReason(''); }; const handleUnlockRental = () => { setRental(prev => prev ? { ...prev, status: 'active', lockedAt: undefined, lockedReason: undefined } : null); }; const handleCancelRental = () => { if (confirm('Are you sure you want to cancel this rental? This action cannot be undone.')) { setRental(prev => prev ? { ...prev, status: 'cancelled', endDate: new Date().toISOString() } : null); } }; const handleAddDue = () => { setRental(prev => prev ? { ...prev, dueRental: (prev.dueRental || 0) + dueAmount } : null); setShowDueModal(false); setDueAmount(0); }; const handleAddNote = () => { if (!newNote.trim()) return; setNotes(prev => [...prev, { id: `n${Date.now()}`, text: newNote, createdAt: new Date().toISOString().split('T')[0] }]); setNewNote(''); }; const handleUpdateOdometer = (value: number) => { if (bike) setBike(prev => prev ? { ...prev, odometer: value } : null); }; const handleUpdateBattery = (value: number) => { if (bike) setBike(prev => prev ? { ...prev, batteryHealth: value } : null); }; const handleUploadImage = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file || !bike) return; const url = URL.createObjectURL(file); setBike(prev => prev ? { ...prev, images: prev.images.map(img => img.type === uploadImageType ? { ...img, url } : img) } : null); setShowImageModal(false); }; const statusColors = { active: 'bg-green-100 text-green-700', pending: 'bg-amber-100 text-amber-700', completed: 'bg-blue-100 text-blue-700', disputed: 'bg-red-100 text-red-700', cancelled: 'bg-slate-100 text-slate-700', locked: 'bg-red-100 text-red-700' }; const statusLabels: Record = { active: 'Active', pending: 'Pending', completed: 'Completed', disputed: 'Disputed', cancelled: 'Cancelled', locked: 'Locked' }; return (

{rental.id}

{statusLabels[rental.status] || rental.status} {rental.status === 'locked' && ( Locked )}

Started {rental.startDate} • From {rental.hubName || 'N/A'}

{editMode ? ( <> ) : ( <> {rental.status === 'active' && ( <> {rental.status === 'active' ? ( ) : rental.status === 'locked' ? ( ) : null} )} {rental.status !== 'cancelled' && rental.status !== 'completed' && ( )} )}

Total Spent

৳{rental.totalPaid.toLocaleString()}

Wallet Balance

৳{user?.walletBalance.toLocaleString() || 0}

Deposit Paid

৳{rental.deposit.toLocaleString()}

Due Rental

৳{(rental.dueRental || 0).toLocaleString()}

Rented Bike Details

{bike && (
Model{bike.model}
Plate{bike.plate}
Type{rental.type}
Daily Rate৳{rental.dailyRate}/day
)}

Mileage Tracking

handleUpdateOdometer(Number(e.target.value))} className="w-full px-3 py-2 border border-amber-200 rounded-lg text-sm mt-1" disabled={rental.status !== 'active'} />

{(bike?.odometer || 0).toLocaleString()} km

Battery Health

handleUpdateBattery(Number(e.target.value))} className="w-full px-3 py-2 border border-green-200 rounded-lg text-sm mt-1" disabled={rental.status !== 'active'} />

70 ? 'text-green-700' : (bike?.batteryHealth || 0) > 40 ? 'text-amber-700' : 'text-red-700'}`}> {(bike?.batteryHealth || 0) > 70 ? 'Good' : (bike?.batteryHealth || 0) > 40 ? 'Fair' : 'Poor'}

Estimated Range: {Math.round((bike?.batteryHealth || 0) * 1)} km

Bike Images

{rental.status === 'active' && ( )}
{['front', 'back', 'left', 'right'].map(type => { const img = bike?.images.find(i => i.type === type); return (
{img?.url ? ( {type} ) : (

{type}

)}
); })}

Notes ({notes.length})

{notes.length > 0 ? (
{notes.map(note => (

{note.text}

{note.createdAt}

))}
) : (

No notes yet.

)} {rental.status === 'active' && (
setNewNote(e.target.value)} placeholder="Add notes about the bike condition, issues, etc..." className="flex-1 px-3 py-2 border border-slate-200 rounded-lg text-sm" />
)}

User Info

Name{user?.name}
Phone{user?.phone}
Email{user?.email || '-'}
{user && ( )}

Membership & Insurance

Membership{user?.membership}
KYC Status{user?.kycStatus}
Insurance{user?.insurance}
{user?.insuranceExpiry &&
Insurance Expiry{user.insuranceExpiry}
}

Hub Info

Hub{rental.hubName || '-'}
{editMode ? ( ) : (
Joined From{user?.joinedFrom || '-'}
)}
{rental.status === 'locked' && (

Locked Info

Locked At{rental.lockedAt?.split('T')[0]}
Reason{rental.lockedReason}
)}
{showLockModal && (

Lock Rental