'use client' import React, { useState, useEffect } from 'react' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { Badge } from '@/components/ui/badge' import { Button } from '@/components/ui/button' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select' import { Skeleton } from '@/components/ui/skeleton' import { useToast } from '@/hooks/use-toast' import { formatCurrency } from '@/lib/balance-service' interface BillingRecord { billing_id: string service: string service_type: string amount: number currency: string user_email: string silicon_id: string status: string payment_status: string cycle: string service_name?: string total_amount: number remarks?: string createdAt: string updatedAt: string } interface BillingStats { totalSpent: number activeServices: number totalServices: number serviceBreakdown: Record } interface BillingHistoryProps { className?: string } const statusColors = { pending: 'bg-yellow-100 text-yellow-800', active: 'bg-green-100 text-green-800', completed: 'bg-blue-100 text-blue-800', cancelled: 'bg-gray-100 text-gray-800', failed: 'bg-red-100 text-red-800', refunded: 'bg-purple-100 text-purple-800', } const paymentStatusColors = { pending: 'bg-yellow-100 text-yellow-800', paid: 'bg-green-100 text-green-800', failed: 'bg-red-100 text-red-800', refunded: 'bg-purple-100 text-purple-800', } const serviceTypeLabels = { vps: 'VPS Server', kubernetes: 'Kubernetes', developer_hire: 'Developer Hire', vpn: 'VPN Service', hosting: 'Web Hosting', storage: 'Cloud Storage', database: 'Database', ai_service: 'AI Service', custom: 'Custom Service', } export default function BillingHistory({ className }: BillingHistoryProps) { const [billings, setBillings] = useState([]) const [stats, setStats] = useState(null) const [loading, setLoading] = useState(true) const [filter, setFilter] = useState<{ serviceType?: string status?: string }>({}) const [pagination, setPagination] = useState({ limit: 20, offset: 0, total: 0, }) const { toast } = useToast() const fetchBillingData = async () => { try { setLoading(true) const params = new URLSearchParams({ limit: pagination.limit.toString(), offset: pagination.offset.toString(), }) if (filter.serviceType) params.append('serviceType', filter.serviceType) if (filter.status) params.append('status', filter.status) const response = await fetch(`/api/billing?${params}`) const data = await response.json() if (data.success) { setBillings(data.data.billings) setStats(data.data.stats) setPagination((prev) => ({ ...prev, total: data.data.pagination.total, })) } else { throw new Error(data.error?.message || 'Failed to fetch billing data') } } catch (error) { console.error('Error fetching billing data:', error) toast({ title: 'Error', description: 'Failed to load billing history', variant: 'destructive', }) } finally { setLoading(false) } } useEffect(() => { fetchBillingData() }, [filter, pagination.limit, pagination.offset]) const handleFilterChange = (key: string, value: string) => { setFilter((prev) => ({ ...prev, [key]: value === 'all' ? undefined : value, })) setPagination((prev) => ({ ...prev, offset: 0 })) } const loadMore = () => { setPagination((prev) => ({ ...prev, offset: prev.offset + prev.limit, })) } if (loading && billings.length === 0) { return (
) } return (
{/* Statistics Cards */} {stats && (
Total Spent
{formatCurrency(stats.totalSpent)}
Active Services
{stats.activeServices}
Total Services
{stats.totalServices}
Service Types
{Object.keys(stats.serviceBreakdown).length}
)} {/* Filters */} Billing History View and manage your service billing records
{/* Billing Records */}
{billings.length === 0 ? (
No billing records found
) : ( billings.map((billing) => (

{billing.service}

{serviceTypeLabels[ billing.service_type as keyof typeof serviceTypeLabels ] || billing.service_type}
Billing ID: {billing.billing_id}
Cycle: {billing.cycle}
{billing.remarks &&
Remarks: {billing.remarks}
}
Created: {new Date(billing.createdAt).toLocaleDateString()}
{formatCurrency( billing.total_amount || billing.amount, billing.currency as 'INR' | 'USD' )}
{billing.status} {billing.payment_status}
)) )}
{/* Load More Button */} {billings.length > 0 && billings.length < pagination.total && (
)}
) }