Files
hats-app/app/admin/report/distribution/component/Export.tsx
T
2025-12-19 13:57:55 +07:00

115 lines
6.5 KiB
TypeScript

'use client'
import { FC, useState } from 'react';
import dayjs from 'dayjs';
import { useSession } from 'next-auth/react';
import { Icons } from '@/components/Icons';
import { Button } from '@/components/ui/button';
import DateRangePicker from './DateRangePicker';
import { exportTable } from '@/lib/exportExcel';
import { type IHistory } from '@/app/(dashboard)/history/components/type';
import { type IUser } from '@/app/admin/administration/user/component/type';
import { IExcelJSColumnType } from '@/types/types';
type Props = {
data: IHistory[]
};
const ExportComponent: FC<Props> = ({ data }) => {
const [isLoading, setIsLoading] = useState(false)
const { data: session } = useSession()
const handleExport = async () => {
setIsLoading(true)
const columns: IExcelJSColumnType[] = [
{ header: 'No', key: 'number', width: 5, style: { numFmt: '0;[Red]0' } },
{ header: 'Presence Date', key: 'presence_date', style: { numFmt: 'dd/mm/yyyy' } },
{ header: 'CI-Total', key: 'presence_checkin_count', width: 10, style: { numFmt: '0;[Red]0' } },
{ header: 'CI-0', key: 'ci_0', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CI-1', key: 'ci_1', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CI-2', key: 'ci_2', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CI-3', key: 'ci_3', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CI-4', key: 'ci_4', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CI-5', key: 'ci_5', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CI-6', key: 'ci_6', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CI-7', key: 'ci_7', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CI-8', key: 'ci_8', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CI-9', key: 'ci_9', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CI-10', key: 'ci_10', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CI-11', key: 'ci_11', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CI-12', key: 'ci_12', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CI-13', key: 'ci_13', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CI-14', key: 'ci_14', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CI-15', key: 'ci_15', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CI-16', key: 'ci_16', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CI-17', key: 'ci_17', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CI-18', key: 'ci_18', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CI-19', key: 'ci_19', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CI-20', key: 'ci_20', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CI-21', key: 'ci_21', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CI-22', key: 'ci_22', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CI-23', key: 'ci_23', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CO-Total', key: 'presence_checkout_count', width: 10, style: { numFmt: '0;[Red]0' } },
{ header: 'CO-0', key: 'co_0', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CO-1', key: 'co_1', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CO-2', key: 'co_2', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CO-3', key: 'co_3', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CO-4', key: 'co_4', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CO-5', key: 'co_5', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CO-6', key: 'co_6', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CO-7', key: 'co_7', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CO-8', key: 'co_8', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CO-9', key: 'co_9', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CO-10', key: 'co_10', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CO-11', key: 'co_11', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CO-12', key: 'co_12', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CO-13', key: 'co_13', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CO-14', key: 'co_14', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CO-15', key: 'co_15', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CO-16', key: 'co_16', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CO-17', key: 'co_17', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CO-18', key: 'co_18', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CO-19', key: 'co_19', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CO-20', key: 'co_20', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CO-21', key: 'co_21', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CO-22', key: 'co_22', width: 7, style: { numFmt: '0;[Red]0' } },
{ header: 'CO-23', key: 'co_23', width: 7, style: { numFmt: '0;[Red]0' } },
]
const resBlob: any = await exportTable(session?.user.detail as IUser, data, columns, 'Attendance Distribution')
if (resBlob) {
const url = URL.createObjectURL(resBlob);
// Create a link and trigger a click event to start the download
const a = document.createElement("a");
a.style.display = "none";
a.href = url;
a.download = `Attendance Distribution - ${dayjs().format('YYYYMMDD HH:mm:ssa')}`; // Set the downloaded filename
document.body.appendChild(a);
a.click();
// Clean up by removing the temporary link and URL
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
setIsLoading(false)
}
return (
<>
<div className="flex flex-col sm:flex-row items-center gap-4">
<DateRangePicker />
<Button className='w-full sm:w-max' disabled={isLoading} onClick={handleExport}>
{isLoading && (
<Icons.spinner className="mr-1 h-4 w-4 animate-spin" />
)}
Download
</Button>
</div>
</>
);
}
export default ExportComponent;