316 lines
17 KiB
TypeScript
316 lines
17 KiB
TypeScript
'use client'
|
|
|
|
import React, { useState, useEffect } from 'react'
|
|
import { Card } from '@/components/ui/card'
|
|
import { ChevronDown, ChevronUp, Users, CheckCircle2, Package, InboxIcon, ChevronLeft, ChevronRight, Search } from 'lucide-react'
|
|
import { cn } from '@/lib/utils'
|
|
import { Button } from '@/components/ui/button'
|
|
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, DialogDescription } from '@/components/ui/dialog'
|
|
import { updateBulkSubmissionPayment } from '@/app/actions'
|
|
import { format } from 'date-fns'
|
|
|
|
const ITEMS_PER_PAGE = 5
|
|
|
|
const formatRupiah = (value: number) =>
|
|
new Intl.NumberFormat('id-ID', { style: 'currency', currency: 'IDR', maximumFractionDigits: 0 }).format(value)
|
|
|
|
export default function ReportByPersonGrid({ data, onUpdate }: { data: any[]; onUpdate: () => void }) {
|
|
const [expandedRows, setExpandedRows] = useState<Record<string, boolean>>({})
|
|
const [currentPage, setCurrentPage] = useState(1)
|
|
const [searchQuery, setSearchQuery] = useState('')
|
|
const [modalOpen, setModalOpen] = useState(false)
|
|
const [saving, setSaving] = useState(false)
|
|
const [target, setTarget] = useState<{ ids: string[]; label: string; amount: number }>({ ids: [], label: '', amount: 0 })
|
|
|
|
// Reset page + search when data changes (e.g. filter changed)
|
|
useEffect(() => {
|
|
setCurrentPage(1)
|
|
setExpandedRows({})
|
|
setSearchQuery('')
|
|
}, [data])
|
|
|
|
// Reset page when search changes
|
|
useEffect(() => {
|
|
setCurrentPage(1)
|
|
}, [searchQuery])
|
|
|
|
const toggleRow = (id: string) =>
|
|
setExpandedRows(prev => ({ ...prev, [id]: !prev[id] }))
|
|
|
|
// Group by user
|
|
const usersMap = new Map<string, any>()
|
|
data.forEach(order => {
|
|
order.submissions.forEach((sub: any) => {
|
|
if (!usersMap.has(sub.user.id)) {
|
|
usersMap.set(sub.user.id, {
|
|
user: sub.user,
|
|
totalPiutang: 0,
|
|
totalPaid: 0,
|
|
submissions: []
|
|
})
|
|
}
|
|
const obj = usersMap.get(sub.user.id)
|
|
const amount = Number(sub.bill) || 0
|
|
if (sub.payment_status !== 'LUNAS') obj.totalPiutang += amount
|
|
else obj.totalPaid += amount
|
|
obj.submissions.push({ ...sub, orderTitle: order.title, orderDate: order.date })
|
|
})
|
|
})
|
|
|
|
const userList = Array.from(usersMap.values())
|
|
.map(obj => ({
|
|
...obj,
|
|
// Sort each person's PO list by order date, newest first
|
|
submissions: [...obj.submissions].sort(
|
|
(a: any, b: any) => new Date(b.orderDate).getTime() - new Date(a.orderDate).getTime()
|
|
)
|
|
}))
|
|
.sort((a, b) => b.totalPiutang - a.totalPiutang)
|
|
|
|
const q = searchQuery.toLowerCase().trim()
|
|
const filteredUserList = q
|
|
? userList.filter(u => u.user.name?.toLowerCase().includes(q))
|
|
: userList
|
|
|
|
const totalPages = Math.ceil(filteredUserList.length / ITEMS_PER_PAGE)
|
|
const paginatedList = filteredUserList.slice((currentPage - 1) * ITEMS_PER_PAGE, currentPage * ITEMS_PER_PAGE)
|
|
|
|
const openModal = (e: React.MouseEvent, ids: string[], label: string, amount: number) => {
|
|
e.stopPropagation()
|
|
setTarget({ ids, label, amount })
|
|
setModalOpen(true)
|
|
}
|
|
|
|
const handleConfirm = async () => {
|
|
if (!target.ids.length) return
|
|
setSaving(true)
|
|
const res = await updateBulkSubmissionPayment(target.ids, 'LUNAS')
|
|
setSaving(false)
|
|
if (res.success) {
|
|
setModalOpen(false)
|
|
onUpdate()
|
|
}
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<Card className="rounded-2xl border border-slate-200/90 dark:border-slate-800 bg-white dark:bg-slate-900 shadow-sm overflow-hidden">
|
|
<div className="bg-slate-50/50 dark:bg-slate-800/40 px-4 py-3 border-b border-slate-100 dark:border-slate-800 flex flex-col sm:flex-row sm:items-center gap-3">
|
|
<div className="flex items-center gap-2 flex-1">
|
|
<Users className="w-4 h-4 text-[#1B2CC1] shrink-0" />
|
|
<h3 className="text-sm font-black text-slate-800 dark:text-slate-200">Detail Piutang Berdasarkan Orang</h3>
|
|
</div>
|
|
<div className="flex items-center gap-2 bg-white dark:bg-slate-900 border border-slate-200 dark:border-slate-700 rounded-lg px-3 py-1.5 w-full sm:w-48">
|
|
<Search className="w-3.5 h-3.5 text-slate-400 shrink-0" />
|
|
<input
|
|
type="text"
|
|
placeholder="Cari nama pemesan..."
|
|
value={searchQuery}
|
|
onChange={e => setSearchQuery(e.target.value)}
|
|
className="text-xs font-medium bg-transparent border-none outline-none w-full text-slate-700 dark:text-slate-200 placeholder:text-slate-400"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
{userList.length === 0 ? (
|
|
<div className="flex flex-col items-center justify-center py-16 gap-3 text-slate-400">
|
|
<InboxIcon className="w-10 h-10 opacity-40" />
|
|
<p className="text-sm font-semibold">Tidak ada data pemesan di periode ini.</p>
|
|
</div>
|
|
) : filteredUserList.length === 0 ? (
|
|
<div className="flex flex-col items-center justify-center py-16 gap-3 text-slate-400">
|
|
<Search className="w-10 h-10 opacity-40" />
|
|
<p className="text-sm font-semibold">Tidak ada hasil untuk "{searchQuery}".</p>
|
|
</div>
|
|
) : (
|
|
<>
|
|
<div className="overflow-x-auto">
|
|
<table className="w-full text-sm text-left">
|
|
<thead className="text-xs text-slate-500 uppercase bg-slate-50/30 dark:bg-slate-900/50">
|
|
<tr>
|
|
<th className="px-6 py-4 font-bold w-8"></th>
|
|
<th className="px-6 py-4 font-bold">Nama Pemesan</th>
|
|
<th className="px-6 py-4 font-bold text-center">Total PO</th>
|
|
<th className="px-6 py-4 font-bold text-right">Total Piutang</th>
|
|
<th className="px-6 py-4 font-bold text-center">Aksi</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody className="divide-y divide-slate-100 dark:divide-slate-800">
|
|
{paginatedList.map((userObj) => {
|
|
const isExpanded = !!expandedRows[userObj.user.id]
|
|
const unpaid = userObj.submissions.filter((s: any) => s.payment_status !== 'LUNAS')
|
|
|
|
return (
|
|
<React.Fragment key={userObj.user.id}>
|
|
<tr
|
|
onClick={() => toggleRow(userObj.user.id)}
|
|
className={cn(
|
|
'hover:bg-slate-50/50 dark:hover:bg-slate-800/40 cursor-pointer transition-colors group',
|
|
isExpanded && 'bg-slate-50/30 dark:bg-slate-800/20'
|
|
)}
|
|
>
|
|
<td className="px-6 py-4">
|
|
<button className="text-slate-400 group-hover:text-[#1B2CC1] transition-colors p-1 rounded-full hover:bg-blue-50 dark:hover:bg-blue-900/30">
|
|
{isExpanded ? <ChevronUp className="w-4 h-4" /> : <ChevronDown className="w-4 h-4" />}
|
|
</button>
|
|
</td>
|
|
<td className="px-6 py-4">
|
|
<div className="flex items-center gap-3">
|
|
{userObj.user.photo ? (
|
|
<img src={userObj.user.photo} alt={userObj.user.name} className="w-8 h-8 rounded-full object-cover ring-2 ring-slate-100 dark:ring-slate-800" />
|
|
) : (
|
|
<div className="w-8 h-8 rounded-full bg-[#1B2CC1]/10 text-[#1B2CC1] flex items-center justify-center font-bold text-xs ring-2 ring-slate-100 dark:ring-slate-800">
|
|
{userObj.user.name.charAt(0).toUpperCase()}
|
|
</div>
|
|
)}
|
|
<p className="font-bold text-slate-800 dark:text-slate-200">{userObj.user.name}</p>
|
|
</div>
|
|
</td>
|
|
<td className="px-6 py-4 text-center font-semibold text-slate-600 dark:text-slate-400">
|
|
{userObj.submissions.length} PO
|
|
</td>
|
|
<td className="px-6 py-4 text-right">
|
|
{userObj.totalPiutang > 0 ? (
|
|
<p className="font-black text-rose-600 dark:text-rose-400">{formatRupiah(userObj.totalPiutang)}</p>
|
|
) : (
|
|
<p className="font-bold text-emerald-600 dark:text-emerald-400">Lunas Semua</p>
|
|
)}
|
|
{userObj.totalPaid > 0 && (
|
|
<p className="text-[10px] text-slate-500 font-semibold mt-0.5">Lunas: {formatRupiah(userObj.totalPaid)}</p>
|
|
)}
|
|
</td>
|
|
<td className="px-6 py-4 text-center">
|
|
{unpaid.length > 0 ? (
|
|
<Button
|
|
onClick={(e) => openModal(e, unpaid.map((s: any) => s.id), `Semua tagihan ${userObj.user.name}`, userObj.totalPiutang)}
|
|
size="sm"
|
|
className="h-8 text-[11px] font-bold bg-[#1B2CC1] text-white hover:bg-[#121E85] shadow-sm shadow-[#1B2CC1]/30 gap-1 px-3"
|
|
>
|
|
<CheckCircle2 className="w-3.5 h-3.5" /> Lunasi Semua
|
|
</Button>
|
|
):( <span className="text-[10px] font-bold text-slate-400">Selesai</span>)}
|
|
</td>
|
|
</tr>
|
|
|
|
{isExpanded && (
|
|
<tr className="bg-slate-50 dark:bg-slate-900/60">
|
|
<td colSpan={5} className="px-6 py-5 border-l-4 border-l-[#1B2CC1]">
|
|
<div className="pl-8 space-y-3">
|
|
<h4 className="text-[11px] font-bold uppercase tracking-wider text-slate-500 flex items-center gap-1.5">
|
|
<Package className="w-3.5 h-3.5" /> Rincian Hutang per PO
|
|
</h4>
|
|
<div className="overflow-hidden border border-slate-200/80 dark:border-slate-700 rounded-xl bg-white dark:bg-slate-800">
|
|
<table className="w-full text-xs">
|
|
<thead className="bg-slate-100/50 dark:bg-slate-700/30 text-slate-500 uppercase font-semibold">
|
|
<tr>
|
|
<th className="px-4 py-2.5 text-left">Judul PO</th>
|
|
<th className="px-4 py-2.5 text-left">Tgl Order</th>
|
|
<th className="px-4 py-2.5 text-left">Status</th>
|
|
<th className="px-4 py-2.5 text-right">Tagihan</th>
|
|
<th className="px-4 py-2.5 text-center">Aksi</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody className="divide-y divide-slate-100 dark:divide-slate-700/50">
|
|
{userObj.submissions.map((sub: any) => {
|
|
const isUnpaid = sub.payment_status !== 'LUNAS'
|
|
return (
|
|
<tr key={sub.id}>
|
|
<td className="px-4 py-2.5 font-bold text-slate-800 dark:text-slate-200">{sub.orderTitle}</td>
|
|
<td className="px-4 py-2.5 text-slate-500 font-medium">
|
|
{format(new Date(sub.orderDate), 'dd MMM yyyy')}
|
|
</td>
|
|
<td className="px-4 py-2.5">
|
|
<span className={cn(
|
|
'text-[9px] font-bold px-1.5 py-0.5 rounded uppercase',
|
|
!isUnpaid
|
|
? 'text-emerald-700 bg-emerald-100 dark:text-emerald-400 dark:bg-emerald-950/40'
|
|
: 'text-rose-700 bg-rose-100 dark:text-rose-400 dark:bg-rose-950/40'
|
|
)}>
|
|
{isUnpaid ? 'Belum' : 'Lunas'}
|
|
</span>
|
|
</td>
|
|
<td className={cn('px-4 py-2.5 text-right font-black', isUnpaid ? 'text-rose-600 dark:text-rose-400' : 'text-emerald-600 dark:text-emerald-400')}>
|
|
{sub.bill ? formatRupiah(sub.bill) : 'Rp0'}
|
|
</td>
|
|
<td className="px-4 py-2.5 text-center">
|
|
{isUnpaid ? (
|
|
<Button
|
|
onClick={(e) => openModal(e, [sub.id], `PO: ${sub.orderTitle}`, Number(sub.bill) || 0)}
|
|
variant="outline"
|
|
size="sm"
|
|
className="h-6 text-[10px] px-2 font-bold border-[#1B2CC1]/40 text-[#1B2CC1] hover:bg-[#1B2CC1] hover:text-white transition-colors"
|
|
>
|
|
Lunasi
|
|
</Button>
|
|
) : (
|
|
<span className="text-[10px] font-bold text-slate-400">Selesai</span>
|
|
)}
|
|
</td>
|
|
</tr>
|
|
)
|
|
})}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
)}
|
|
</React.Fragment>
|
|
)
|
|
})}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
{/* Pagination */}
|
|
{totalPages > 1 && (
|
|
<div className="flex items-center justify-between px-6 py-4 bg-slate-50/50 dark:bg-slate-900/50 border-t border-slate-100 dark:border-slate-800">
|
|
<p className="text-xs font-medium text-slate-500">
|
|
Halaman <span className="font-bold text-slate-700 dark:text-slate-300">{currentPage}</span> dari{' '}
|
|
<span className="font-bold text-slate-700 dark:text-slate-300">{totalPages}</span>
|
|
</p>
|
|
<div className="flex items-center gap-1">
|
|
<button onClick={() => setCurrentPage(p => Math.max(1, p - 1))} disabled={currentPage === 1} className="p-1.5 rounded-lg border border-slate-200 dark:border-slate-700 disabled:opacity-40 hover:bg-slate-100 dark:hover:bg-slate-800 transition-colors">
|
|
<ChevronLeft className="w-3.5 h-3.5" />
|
|
</button>
|
|
<button onClick={() => setCurrentPage(p => Math.min(totalPages, p + 1))} disabled={currentPage === totalPages} className="p-1.5 rounded-lg border border-slate-200 dark:border-slate-700 disabled:opacity-40 hover:bg-slate-100 dark:hover:bg-slate-800 transition-colors">
|
|
<ChevronRight className="w-3.5 h-3.5" />
|
|
</button>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</>
|
|
)}
|
|
</Card>
|
|
|
|
{/* Confirmation Modal */}
|
|
<Dialog open={modalOpen} onOpenChange={setModalOpen}>
|
|
<DialogContent className="sm:max-w-md rounded-2xl">
|
|
<DialogHeader>
|
|
<DialogTitle className="text-xl font-black text-slate-900 dark:text-white flex items-center gap-2">
|
|
<CheckCircle2 className="w-6 h-6 text-emerald-500" />
|
|
Konfirmasi Pelunasan
|
|
</DialogTitle>
|
|
<DialogDescription className="text-slate-500 pt-2">
|
|
Tandai <strong className="text-slate-800 dark:text-slate-200">{target.label}</strong> sebagai <strong>LUNAS</strong>?
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
<div className="bg-slate-50 dark:bg-slate-900/50 p-4 rounded-xl border border-slate-100 dark:border-slate-800 my-2">
|
|
<p className="text-xs font-bold text-slate-500 uppercase tracking-wider mb-1">Total Nominal</p>
|
|
<p className="text-2xl font-black text-emerald-600 dark:text-emerald-400">{formatRupiah(target.amount)}</p>
|
|
</div>
|
|
<DialogFooter className="gap-2 sm:gap-0">
|
|
<Button variant="outline" onClick={() => setModalOpen(false)} disabled={saving} className="rounded-xl font-bold">
|
|
Batal
|
|
</Button>
|
|
<Button onClick={handleConfirm} disabled={saving} className="rounded-xl font-bold bg-emerald-600 hover:bg-emerald-700 text-white shadow-md shadow-emerald-600/20">
|
|
{saving ? 'Menyimpan...' : 'Ya, Simpan Pelunasan'}
|
|
</Button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
</>
|
|
)
|
|
}
|