diff --git a/app/staff/history/page.tsx b/app/staff/history/page.tsx index 99bbee6f..fe70e076 100644 --- a/app/staff/history/page.tsx +++ b/app/staff/history/page.tsx @@ -24,11 +24,24 @@ type AttendanceRecord = { total_hours: number | null status?: string // 'Complete' | 'Pending' created_at: string + type: 'attendance' +} + +type LeaveRecord = { + id: string + user_id: string + leave_type: string + start_date: string + end_date: string + reason: string + status: 'pending' | 'approved' | 'rejected' + created_at: string + type: 'leave' } export default function StaffHistoryPage() { const { user, loading: authLoading } = useAuth() - const [history, setHistory] = useState([]) + const [history, setHistory] = useState<(AttendanceRecord | LeaveRecord)[]>([]) const [loading, setLoading] = useState(true) const [stats, setStats] = useState({ totalHours: 0, @@ -43,20 +56,38 @@ export default function StaffHistoryPage() { const fetchData = async () => { try { - const { data, error } = await supabase + // Fetch Attendance + const { data: attendanceData, error: attendanceError } = await supabase .from("attendance_logs") .select("*") .eq("user_id", user.id) - .order("check_in", { ascending: false }) - if (error) throw error + if (attendanceError) throw attendanceError + + // Fetch Leaves + const { data: leaveData, error: leaveError } = await supabase + .from("leave_requests") + .select("*") + .eq("user_id", user.id) + .neq("status", "rejected") // Only show pending/approved + + if (leaveError) throw leaveError - const records = data || [] - setHistory(records) + const attendanceRecords: AttendanceRecord[] = (attendanceData || []).map(r => ({ ...r, type: 'attendance' })) + const leaveRecords: LeaveRecord[] = (leaveData || []).map(r => ({ ...r, type: 'leave' })) + + // Merge and Sort + const mergedRecords = [...attendanceRecords, ...leaveRecords].sort((a, b) => { + const dateA = a.type === 'attendance' ? new Date(a.check_in) : new Date(a.start_date) + const dateB = b.type === 'attendance' ? new Date(b.check_in) : new Date(b.start_date) + return dateB.getTime() - dateA.getTime() // Descending + }) - // Calculate stats - const totalHours = records.reduce((acc, curr) => acc + (curr.total_hours || 0), 0) - const daysPresent = records.length + setHistory(mergedRecords) + + // Calculate stats (only from attendance) + const totalHours = attendanceRecords.reduce((acc, curr) => acc + (curr.total_hours || 0), 0) + const daysPresent = attendanceRecords.length const averageHours = daysPresent > 0 ? totalHours / daysPresent : 0 setStats({ @@ -92,17 +123,9 @@ export default function StaffHistoryPage() { Attendance History

- View and track your past attendance records. + View and track your past attendance and leave records.

- {/*
- - -
*/} {/* Stats Overview */} @@ -167,55 +190,86 @@ export default function StaffHistoryPage() { {history.length === 0 ? ( - No attendance records found. + No records found. ) : ( history.map((record) => { - const date = new Date(record.check_in).toLocaleDateString(undefined, { - weekday: 'short', - year: 'numeric', - month: 'short', - day: 'numeric' - }) - const checkInTime = new Date(record.check_in).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }) - const checkOutTime = record.check_out - ? new Date(record.check_out).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }) - : '-' - - const isComplete = !!record.check_out - - return ( - - {date} - {checkInTime} - {checkOutTime} - - = 8 + if (record.type === 'leave') { + // Render Leave Row + const r = record as LeaveRecord + const date = new Date(r.start_date).toLocaleDateString(undefined, { + weekday: 'short', + year: 'numeric', + month: 'short', + day: 'numeric' + }) + return ( + + {date} + - + - + + + {r.leave_type} Leave + + + +
+ + On Leave +
+ + + ) + } else { + // Render Attendance Row + const r = record as AttendanceRecord + const date = new Date(r.check_in).toLocaleDateString(undefined, { + weekday: 'short', + year: 'numeric', + month: 'short', + day: 'numeric' + }) + const checkInTime = new Date(r.check_in).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }) + const checkOutTime = r.check_out + ? new Date(r.check_out).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }) + : '-' + + const isComplete = !!r.check_out + + return ( + + {date} + {checkInTime} + {checkOutTime} + + = 8 ? "bg-green-500/10 text-green-400 border border-green-500/20" : "bg-blue-500/10 text-blue-400 border border-blue-500/20" - }`}> - {record.total_hours ? `${record.total_hours.toFixed(2)}h` : '-'} - - - -
- {isComplete ? ( - <> - - Completed - - ) : ( - <> - - Active - - )} -
- - - ) + }`}> + {r.total_hours ? `${r.total_hours.toFixed(2)}h` : '-'} +
+ + +
+ {isComplete ? ( + <> + + Completed + + ) : ( + <> + + Active + + )} +
+ + + ) + } }) )}