"use client"; import { Paper, Select, Title, Button, Text, Grid, ScrollArea, Table, Divider, Center, Loader, Stack, Group } from "@mantine/core"; import { DateInput } from '@mantine/dates'; import { useEffect, useRef, useState } from "react"; import { useSearchParams } from "next/navigation"; import { notifications } from "@mantine/notifications"; import dayjs from 'dayjs'; import { IconFileSpreadsheet, IconFileText, IconFileTypePdf } from "@tabler/icons-react"; import { generatePDF } from "@/app/_components/statement_download/PdfGenerator"; import { generateCSV } from "@/app/_components/statement_download/CsvGenerator"; export default function AccountStatementPage() { const pdfRef = useRef(null); const [accountOptions, setAccountOptions] = useState<{ value: string; label: string }[]>([]); const [selectedAccNo, setSelectedAccNo] = useState(null); const [startDate, setStartDate] = useState(null); const [endDate, setEndDate] = useState(null); const [transactions, setTransactions] = useState([]); const searchParams = useSearchParams(); const passedAccNo = searchParams.get("accNo"); const [loading, setLoading] = useState(false); const [availableBalance, setAvailableBalance] = useState(null); useEffect(() => { const saved = sessionStorage.getItem("accountData"); if (saved) { const parsed = JSON.parse(saved); const options = parsed.map((acc: any) => ({ label: `${acc.stAccountNo} - ${acc.stAccountType}`, value: acc.stAccountNo, })); setAccountOptions(options); if (passedAccNo) { setSelectedAccNo(passedAccNo); //Automatically fetch last 5 transactions if accNo is passed const token = localStorage.getItem("access_token"); setLoading(true); fetch(`/api/transactions/account/${passedAccNo}`, { method: "GET", headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}`, }, }) .then(res => res.json()) .then(data => { if (Array.isArray(data)) { const last5 = data.slice(0, 5); setTransactions(last5); // Reuse balance logic const saved = sessionStorage.getItem("accountData"); if (saved) { const parsed = JSON.parse(saved); const acc = parsed.find((a: any) => a.stAccountNo === passedAccNo); if (acc) { setAvailableBalance(acc.stAvailableBalance); } } } }) .catch(() => { notifications.show({ withBorder: true, color: "red", title: "Fetch Failed", message: "Could not load recent transactions.", autoClose: 5000, }); }) .finally(() => setLoading(false)); } } }, [passedAccNo]); const handleAccountTransaction = async () => { if (!selectedAccNo || !startDate || !endDate) { notifications.show({ withBorder: true, color: "red", title: "Missing field", message: "Please select Account number,Start date and End date", autoClose: 5000, }); return; } const start = dayjs(startDate); const end = dayjs(endDate); const today = dayjs().startOf('day'); if (end.isAfter(today)) { notifications.show({ withBorder: true, color: "red", title: "Invalid End Date", message: "End date can not be the future date.", autoClose: 4000, }); return; } if (start.isAfter(end)) { notifications.show({ withBorder: true, color: "red", title: "Invalid Start Date", message: "Start date can not be less than end date", autoClose: 4000, }); return; } if (end.diff(start, "day") > 90) { notifications.show({ withBorder: true, color: "red", title: "Invalid Date range", message: "End date must be within 90 days from start date", autoClose: 4000, }); return; } try { const token = localStorage.getItem("access_token"); setLoading(true); const response = await fetch(`/api/transactions/account/${selectedAccNo}?fromDate=${dayjs(start).format('DDMMYYYY')}&toDate=${dayjs(end).format('DDMMYYYY')}`, { method: "GET", headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}`, }, }); const data = await response.json(); if (response.ok && Array.isArray(data)) { setTransactions(data.reverse()); // SHowing Available balance const saved = sessionStorage.getItem("accountData"); if (saved) { const parsed = JSON.parse(saved); const acc = parsed.find((a: any) => a.stAccountNo === selectedAccNo); if (acc) { setAvailableBalance(acc.stAvailableBalance); } } } } catch { notifications.show({ withBorder: true, color: "red", title: "Please try again later", message: "Unable to Fetch Account Transaction, Please try again later", autoClose: 5000, }); } finally { setLoading(false); } }; const cellStyle = { border: "1px solid #ccc", padding: "8px", }; return ( {/* Left side – form */} Account Transactions