import api from './api'; import type { PaginatedResponse } from '../types'; export interface Attendance { id: number; account_id: number; user_id: number; section_id: number | null; time_log: string; checkout_time: string | null; coordinates: Record | null; note: string | null; action?: string | null; user_name?: string; user_lastname?: string; role?: string | null; card_id?: string | null; section_name?: string; is_late?: boolean; late_reason?: string | null; late_authorized_by?: string | null; } export interface TodayCheckIn { id: number; user_id: number; card_id?: string; user_name: string; user_lastname: string; role?: string | null; section_name: string | null; time_log: string; checkout_time: string | null; note?: string | null; is_late?: boolean; late_reason?: string | null; late_authorized_by?: string | null; } export interface HeatmapEntry { date: string; count: number; } export interface AttendanceFilters { page?: number; per_page?: number; user_id?: number; section_id?: number; role?: string; date_from?: string; date_to?: string; type?: string; } export async function downloadReportCSV(filters: { role?: string; date_from?: string; date_to?: string; user_id?: number } = {}): Promise { const params = new URLSearchParams(); Object.entries(filters).forEach(([k, v]) => { if (v !== undefined && v !== '') params.set(k, String(v)); }); const { data } = await api.get(`/attendances/report-export?${params}`, { responseType: 'blob' }); const url = URL.createObjectURL(data); const a = document.createElement('a'); a.href = url; a.download = 'reporte_asistencia.csv'; a.click(); URL.revokeObjectURL(url); } export async function listAttendances(filters: AttendanceFilters = {}): Promise> { const params = new URLSearchParams(); Object.entries(filters).forEach(([k, v]) => { if (v !== undefined && v !== '') params.set(k, String(v)); }); const { data } = await api.get(`/attendances?${params}`); return data; } export async function checkin(payload: { card_id: string; section_id?: number; note?: string; late_note?: string }): Promise { const { data } = await api.post('/attendances/checkin', payload); return data; } export async function checkout(card_id: string, late_note?: string): Promise { const { data } = await api.post('/attendances/checkout', { card_id, late_note }); return data; } export async function getTodayCheckins(): Promise { const { data } = await api.get('/attendances/today'); return data; } export async function getHeatmap(student_id: number, year?: number): Promise { const params = year ? `?year=${year}` : ''; const { data } = await api.get(`/attendances/heatmap/${student_id}${params}`); return data; } export interface CurrentMode { mode: string; schedule: { entry: string; exit: string } | null; today: string; locked: boolean; message: string; tolerance?: number; active?: boolean; checkin_mode?: string; } export async function getCurrentMode(): Promise { const { data } = await api.get('/attendances/current-mode'); return data; } export async function adminEarlyCheckout(userId: number, reason: string): Promise { const { data } = await api.post('/attendances/admin-early-checkout', { user_id: userId, reason }); return data; }