"use client"; import { useEffect, useState } from "react"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { priorityLabels, statusLabels } from "@/components/repairs/RepairStatusBadge"; import { api } from "@/lib/api"; import type { Customer } from "@/types/customer"; import type { Repair, RepairPayload, RepairPriority, RepairSource, RepairStatus } from "@/types/repair"; type Props = { open: boolean; repair?: Repair | null; pending: boolean; serverError: string; onOpenChange: (open: boolean) => void; onSubmit: (payload: RepairPayload) => void; }; const initialPayload: RepairPayload = { customer_id: null, contact_id: null, status: "new", priority: "normal", source: "manual", source_reference: null, customer_name: "", customer_email: null, customer_phone: "", device_manufacturer: "", device_model: "", device_serial_number: null, device_type: "", accessories: "", fault_description: "", previous_work: "", device_opened: false, intake_notes: "", diagnosis_notes: "", repair_notes: "", estimate_notes: "", estimated_cost_cents: null, }; function payloadFromRepair(repair?: Repair | null): RepairPayload { if (!repair) { return initialPayload; } return { customer_id: repair.customer_id, contact_id: repair.contact_id, status: repair.status, priority: repair.priority, source: repair.source, source_reference: repair.source_reference, customer_name: repair.customer_name, customer_email: repair.customer_email || null, customer_phone: repair.customer_phone, device_manufacturer: repair.device_manufacturer, device_model: repair.device_model, device_serial_number: repair.device_serial_number, device_type: repair.device_type, accessories: repair.accessories, fault_description: repair.fault_description, previous_work: repair.previous_work, device_opened: repair.device_opened, intake_notes: repair.intake_notes, diagnosis_notes: repair.diagnosis_notes, repair_notes: repair.repair_notes, estimate_notes: repair.estimate_notes, estimated_cost_cents: repair.estimated_cost_cents, }; } export default function RepairFormDialog({ open, repair, pending, serverError, onOpenChange, onSubmit }: Props) { const [payload, setPayload] = useState(payloadFromRepair(repair)); const [clientError, setClientError] = useState(""); const [customers, setCustomers] = useState([]); useEffect(() => { if (!open) { return; } let active = true; async function loadCustomers() { try { const response = await api.get("/customers"); if (active) { setCustomers(response.data); } } catch { if (active) { setCustomers([]); } } } void loadCustomers(); return () => { active = false; }; }, [open]); function update(key: K, value: RepairPayload[K]) { setPayload((current) => ({ ...current, [key]: value })); } function submit() { if (!payload.customer_name.trim() || !payload.device_manufacturer.trim() || !payload.device_model.trim() || !payload.fault_description.trim()) { setClientError("Bitte Kundennamen, Hersteller, Modell und Fehlerbeschreibung ausfüllen."); return; } setClientError(""); onSubmit(payload); } function selectCustomer(customerId: string) { if (!customerId) { update("customer_id", null); return; } const customer = customers.find((item) => String(item.id) === customerId); if (!customer) { return; } setPayload((current) => ({ ...current, customer_id: customer.id, customer_name: customer.company_name, customer_email: customer.email || current.customer_email, customer_phone: customer.phone || current.customer_phone, })); } return ( {repair ? "Reparatur bearbeiten" : "Reparatur anlegen"} Erfasse Kundendaten, Gerätedaten und Werkstattnotizen.

Kunde

Gerät

Vorgang

Beschreibung