diff --git a/components/SpotHistory.tsx b/components/SpotHistory.tsx new file mode 100644 index 0000000..982b8b6 --- /dev/null +++ b/components/SpotHistory.tsx @@ -0,0 +1,139 @@ +import React, { useState, useEffect } from 'react'; +import { HistoryIcon, Download, Filter } from 'lucide-react'; +import { supabase } from '../services/supabaseClient'; + +interface SpotHistoryItem { + id: string; + name: string; + date: string; + total_amount: number; + payment_status: string; + items_count: number; +} + +export const SpotHistory: React.FC = () => { + const [history, setHistory] = useState([]); + const [loading, setLoading] = useState(true); + const [dateFilter, setDateFilter] = useState<{ from: string; to: string }>({ from: '', to: '' }); + + useEffect(() => { + fetchSpotHistory(); + }, []); + + const fetchSpotHistory = async () => { + try { + setLoading(true); + // Fetch completed/archived spots from database + const { data, error } = await supabase + .from('spots') + .select('id, name, created_at, total_amount, payment_status, items_count') + .eq('status', 'completed') + .order('created_at', { ascending: false }); + + if (error) throw error; + setHistory(data || []); + } catch (error) { + console.error('Error fetching spot history:', error); + } finally { + setLoading(false); + } + }; + + const handleExport = () => { + // Export functionality to CSV/PDF + const csv = history + .map(item => `${item.name},${item.date},${item.total_amount},${item.payment_status}`) + .join('\n'); + + const blob = new Blob([csv], { type: 'text/csv' }); + const url = window.URL.createObjectURL(blob); + const a = document.createElement('a'); + a.href = url; + a.download = 'spot-history.csv'; + a.click(); + }; + + return ( +
+
+
+ +

Spot History

+
+ +
+ +
+
+ + setDateFilter({ ...dateFilter, from: e.target.value })} + className="w-full px-3 py-2 border rounded-lg" + /> +
+
+ + setDateFilter({ ...dateFilter, to: e.target.value })} + className="w-full px-3 py-2 border rounded-lg" + /> +
+
+ + {loading ? ( +
Loading history...
+ ) : ( +
+ + + + + + + + + + + + {history.length === 0 ? ( + + + + ) : ( + history.map((item) => ( + + + + + + + + )) + )} + +
Spot NameDateAmountStatusItems
+ No spots in history +
{item.name}{new Date(item.date).toLocaleDateString()}₹{item.total_amount.toFixed(2)} + + {item.payment_status} + + {item.items_count}
+
+ )} +
+ ); +}; + +export default SpotHistory;