'use client'
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
import {
BarChart,
Bar,
XAxis,
YAxis,
Tooltip,
ResponsiveContainer,
CartesianGrid,
Cell,
} from 'recharts'
export default function ReportCharts({
type,
data,
}: {
type: 'SUBMITTOR' | 'CREATOR'
data: any[]
}) {
const formatRupiah = (value: number) => {
return new Intl.NumberFormat('id-ID', {
style: 'currency',
currency: 'IDR',
maximumFractionDigits: 0,
}).format(value)
}
// Prepare chart data based on type
let chartData: any[] = []
if (type === 'SUBMITTOR') {
chartData = [...data]
.filter((sub) => sub.order.status === 'CLOSE')
.sort((a, b) => new Date(a.order.date).getTime() - new Date(b.order.date).getTime()) // oldest → newest
.map((sub) => {
const bill = sub.bill || 0
const paid =
(sub.paid_amount ?? (sub.payment_status === 'LUNAS' ? bill : 0)) + (sub.saldo_used || 0)
const unpaid = Math.max(0, bill - paid)
return {
name:
sub.order.title.length > 15
? sub.order.title.substring(0, 15) + '...'
: sub.order.title,
fullTitle: sub.order.title,
Total: bill,
Paid: paid,
Unpaid: unpaid,
status: unpaid > 0 ? (paid > 0 ? 'SEBAGIAN' : 'BELUM BAYAR') : 'LUNAS',
}
})
} else {
chartData = [...data]
.filter((order) => order.status === 'CLOSE')
.sort((a, b) => new Date(a.date).getTime() - new Date(b.date).getTime()) // oldest → newest
.map((order) => {
const total = order.submissions.reduce((acc: number, sub: any) => acc + (sub.bill || 0), 0)
return {
name: order.title.length > 15 ? order.title.substring(0, 15) + '...' : order.title,
fullTitle: order.title,
Total: total,
status: order.status,
}
})
}
// If no data to show
if (chartData.length === 0) {
return (
Belum ada data transaksi yang cukup untuk ditampilkan di grafik.
{data.fullTitle}
Total: {formatRupiah(data.Total)}
{type === 'SUBMITTOR' && ( <>Dibayar: {formatRupiah(data.Paid)}
{data.Unpaid > 0 && (Kurang: {formatRupiah(data.Unpaid)}
)} > )}Status: {data.status}