Files
hats-app/app/admin/administration/user/component/EmployeePerformance/Table.tsx
T
2025-12-19 13:57:55 +07:00

293 lines
11 KiB
TypeScript

'use client'
import { ChangeEvent, FC, useEffect, useState } from 'react';
import { Table } from 'antd';
import dayjs from 'dayjs';
import { TableProps, type ColumnsType } from 'antd/es/table';
import { Input } from '@/components/ui/input';
import { IHistory } from '@/app/(dashboard)/history/components/type';
import { Badge } from '@/components/ui/badge';
import { getVariantApproved, getVariantWorkingStatus } from '@/lib/helper';
import useMobileView from '@/hooks/useMobileView';
type Props = {
data: IHistory[]
};
const TableAttendanceHistory: FC<Props> = ({ data }) => {
const [filterData, setFilterData] = useState<IHistory[]>([])
const { isMobile, windowWidth } = useMobileView()
useEffect(() => {
if (data) {
setFilterData(data)
}
}, [data])
const handleSearch = (e: ChangeEvent<HTMLInputElement>) => {
const text = e.target.value
if (text) {
const newData = [...data!].filter(item => (
item.user.fullname?.toLowerCase().includes(text.toLowerCase()) ||
item.working_status?.toLowerCase().includes(text.toLowerCase()) ||
item.device.name?.toLowerCase().includes(text.toLowerCase()) ||
item.location_name?.toLowerCase().includes(text.toLowerCase()) ||
item.note?.toLowerCase().includes(text.toLowerCase()) ||
item.presence_checkout?.location_name?.toLowerCase().includes(text.toLowerCase()) ||
item.presence_checkout?.note?.toLowerCase().includes(text.toLowerCase())
))
setFilterData(newData)
} else {
setFilterData(data)
}
}
const Columns: ColumnsType<IHistory> = [
{
title: 'Date',
dataIndex: 'presence_date',
sorter: ((a, b) => (dayjs(a.presence_date).isAfter(dayjs(b.presence_date)) ? 1 : -1)),
render: (val) => dayjs(val).format('MMM DD, YYYY')
},
{
title: 'Name',
dataIndex: 'fullname',
sorter: (a, b) => a.user.fullname.localeCompare(b.user.fullname),
render: (val, record) => record.user.fullname
},
{
title: 'Working Status',
dataIndex: 'working_status',
filters: [
{
text: 'WFO',
value: 'WFO',
},
{
text: 'WFS',
value: 'WFS',
},
{
text: 'WFH',
value: 'WFH',
},
{
text: 'Sakit',
value: 'SKT',
},
{
text: 'Izin',
value: 'IZ01',
}
],
onFilter: (value, record) => record.working_status!.indexOf(value as string) === 0,
sorter: (a, b) => a.working_status?.localeCompare(b.working_status),
render: (val, record) => <Badge variant={getVariantWorkingStatus(val)}>{val}</Badge>
},
{
title: 'Approval',
dataIndex: 'status',
filters: [
{
text: 'Approved',
value: 'AP',
},
{
text: 'Rejected',
value: 'R',
},
{
text: 'Need to be Appove',
value: 'NA',
}
],
onFilter: (value, record) => record.status.code!.indexOf(value as string) === 0,
render: (val, record) => record.working_status === 'WFH' || record.working_status === 'WFS' ? <Badge variant={getVariantApproved(record.status.code as any)}>{record.status.name}</Badge> : ''
},
{
title: 'Check-in',
dataIndex: 'working_hours_start',
filters: [
{
text: 'Less than 10:00 AM',
value: 'lt_10', // You can use any value you prefer
},
{
text: 'More than or equal to 10:00 AM',
value: 'gte_10', // You can use any value you prefer
},
],
onFilter: (value, record) => {
const time = new Date(record.working_hours_start).getHours();
if (value === 'lt_10') {
return time < 10;
} else if (value === 'gte_10') {
return time >= 10;
}
return false; // If the value doesn't match any filter
},
render: (val) => <span className='text-green-500'>{dayjs(val).format('HH:mm A')}</span>
},
{
title: 'Check-in Device',
dataIndex: 'device',
filters: [
{
text: 'FR Device',
value: '11001',
},
{
text: 'Web App',
value: '40001',
},
{
text: 'Android App',
value: '20001',
},
{
text: 'IOS App',
value: '30001',
}
],
onFilter: (value, record) => record.device.code!.indexOf(value as string) === 0,
render: (val, record) => record.device.name,
},
{
title: 'Check-in Location',
dataIndex: 'location_name',
},
{
title: 'Check-in Note',
dataIndex: 'note',
},
{
title: 'Check-in Photo',
dataIndex: 'face_image',
render: (val, record) => val ? <a onClick={(e) => e.stopPropagation()} className='text-his hover:opacity-70 duration-200' href={process.env.BASE_URL_IMG + val} target="_blank">View Photo</a> : ''
},
{
title: 'Check-out',
dataIndex: 'working_hours_end',
filters: [
{
text: 'Less than 17:00 PM',
value: 'lt_17', // You can use any value you prefer
},
{
text: 'More than or equal to 17:00 PM',
value: 'gte_17', // You can use any value you prefer
},
],
onFilter: (value, record) => {
const time = new Date(record.presence_checkout?.working_hours_end).getHours();
if (value === 'lt_17') {
return time < 17;
} else if (value === 'gte_17') {
return time >= 17;
}
return false; // If the value doesn't match any filter
},
render: (val, record) => record.presence_checkout?.working_hours_end ? <span className='text-rose-500'>{dayjs(record.presence_checkout?.working_hours_end).format('HH:mm A')}</span> : undefined
},
{
title: 'Check-out Device',
dataIndex: 'device',
filters: [
{
text: 'FR Device',
value: '11001',
},
{
text: 'Web App',
value: '40001',
},
{
text: 'Android App',
value: '20001',
},
{
text: 'IOS App',
value: '30001',
}
],
onFilter: (value, record) => record.presence_checkout?.device?.code!.indexOf(value as string) === 0,
render: (val, record) => record.presence_checkout?.device?.name,
},
{
title: 'Check-out Location',
dataIndex: 'location_name',
render: (val, record) => record.presence_checkout?.location_name
},
{
title: 'Check-out Note',
dataIndex: 'note',
render: (val, record) => record.presence_checkout?.note
},
{
title: 'Check-out Photo',
dataIndex: 'face_image',
render: (val, record) => record.presence_checkout?.face_image ? <a onClick={(e) => e.stopPropagation()} className='text-his hover:opacity-70 duration-200' href={process.env.BASE_URL_IMG + record.presence_checkout?.face_image} target="_blank">View Photo</a> : ''
},
{
title: 'Auto Checkout',
dataIndex: 'autocheckout',
filters: [
{
text: 'Auto Checkout',
value: true,
}
],
onFilter: (value, record) => record.presence_checkout?.auto_checkout?.valueOf() === true,
render: (val, record) => record.presence_checkout?.auto_checkout ? <i className="ti ti-check text-xl text-green-500"></i> : '-'
},
{
title: 'Clear AutoChekout',
dataIndex: 'status',
render: (val, record) => record?.is_clear_auto_checkout === false ? <Badge>need to be approve</Badge> : record.is_clear_auto_checkout === true ? <Badge variant='secondary'>approved</Badge> : undefined
},
];
const tableProps: TableProps<IHistory> = {
size: 'small',
bordered: false,
showHeader: true,
pagination: {
pageSizeOptions: [5, 10, 20, 50],
defaultPageSize: 15,
showSizeChanger: true,
showQuickJumper: true,
size: 'default'
},
scroll: { x: 'max-content' },
tableLayout: 'auto',
};
return (
<>
{/* <div className='flex flex-col space-y-4 max-w-[300px] sm:max-w-[500px] md:max-w-[600px] lg:max-w-[800px] xl:max-w-[1200px] 2xl:max-w-[1400px]'> */}
<div style={{ width: windowWidth > 1900 ? 1400 : windowWidth > 1280 ? 1200 : windowWidth > 1000 ? 800 : windowWidth > 760 ? 600 : windowWidth * 0.72 }} className='flex flex-col space-y-4'>
<div className="flex items-center justify-between">
<div className="flex flex-1 items-center space-x-2">
<Input placeholder="Search attendance..." className="h-8 w-full sm:w-[250px]" onChange={handleSearch} />
</div>
</div>
<Table
{...tableProps}
onRow={(record, rowIndex) => {
return {
onClick: event => { console.log(record.face_image); }, // click row
};
}}
rowKey='presence_checkin_code'
columns={Columns}
dataSource={filterData || []}
style={{ cursor: "pointer" }}
/>
</div>
</>
);
}
export default TableAttendanceHistory;