import { useRef } from "react"; import Box from "@mui/material/Box"; import Typography from "@mui/material/Typography"; import IconButton from "@mui/material/IconButton"; import { Modal, Button, Select, TextField, DateField, TimeField, Field, Alert, } from "../ui"; import { calcFormWorkMinutes, calcProjectMinutesTotal, formatDate, } from "../utils/attendanceHelpers"; // ---------- Shared types ---------- // Definitions live in the dependency-free ./shiftFormTypes module; re-exported // here so existing importers of ShiftFormModal keep working. export type { ShiftFormData, ProjectLog, Project, User, EditingRecord, } from "./shiftFormTypes"; import type { ShiftFormData, ProjectLog, Project, User, EditingRecord, } from "./shiftFormTypes"; // ---------- Sub-component props ---------- interface ProjectTimeStatusProps { form: ShiftFormData; projectLogs: ProjectLog[]; } interface ProjectLogRowProps { log: ProjectLog; index: number; projectList: Project[]; onUpdate: (index: number, field: string, value: string) => void; onRemove: (index: number) => void; } export interface ShiftFormModalProps { mode: "create" | "edit"; isOpen: boolean; onClose: () => void; onSubmit: () => void; form: ShiftFormData; setForm: (form: ShiftFormData) => void; projectLogs: ProjectLog[]; setProjectLogs: (logs: ProjectLog[]) => void; projectList: Project[]; users: User[]; onShiftDateChange: (value: string) => void; editingRecord: EditingRecord | null; } const RemoveIcon = ( ); // ---------- ProjectTimeStatus ---------- function ProjectTimeStatus({ form, projectLogs }: ProjectTimeStatusProps) { const totalWork = calcFormWorkMinutes(form); const totalProject = calcProjectMinutesTotal( projectLogs.map((l) => ({ ...l, project_id: l.project_id !== "" && l.project_id != null ? Number(l.project_id) : undefined, })), ); const remaining = totalWork - totalProject; const hasLogs = projectLogs.some((l) => l.project_id); if (!hasLogs || totalWork <= 0) return null; const isMatch = remaining === 0; return ( Odpracováno: {Math.floor(totalWork / 60)}h {totalWork % 60}m | Přiřazeno: {Math.floor(totalProject / 60)}h {totalProject % 60}m | Zbývá: {Math.floor(Math.abs(remaining) / 60)}h{" "} {Math.abs(remaining) % 60}m {remaining < 0 ? "(překročeno)" : ""} ); } // ---------- ProjectLogRow ---------- function ProjectLogRow({ log, index, projectList, onUpdate, onRemove, }: ProjectLogRowProps) { return ( updateField("user_id", val)} options={[ { value: "", label: "Vyberte zaměstnance" }, ...users.map((user) => ({ value: String(user.id), label: user.name, })), ]} /> onShiftDateChange(val)} /> ) : ( updateField("shift_date", val)} /> )}