'use client' import React, { useState, useEffect } from 'react' import Link from 'next/link' interface ProductionLineItem { id: string name: string description: string station_count: number created_at: string updated_at: string } interface ProjectItem { id: string machine_name: string machine_type: string status: string } const STATION_TYPES = [ { value: 'press', label: 'Presse' }, { value: 'cobot', label: 'Cobot/Roboter' }, { value: 'motor', label: 'Motor/Antrieb' }, { value: 'rotary_transfer', label: 'Rundtaktanlage' }, { value: 'conveyor', label: 'Foerderer' }, { value: 'assembly', label: 'Montage' }, { value: 'milling', label: 'Fraese' }, { value: 'turning', label: 'Drehmaschine' }, { value: 'welding', label: 'Schweissen' }, { value: 'inspection', label: 'Pruefung' }, { value: 'packaging', label: 'Verpackung' }, ] export default function ProductionLinesListPage() { const [lines, setLines] = useState([]) const [projects, setProjects] = useState([]) const [loading, setLoading] = useState(true) const [showCreate, setShowCreate] = useState(false) const [creating, setCreating] = useState(false) const [lineName, setLineName] = useState('') const [lineDesc, setLineDesc] = useState('') const [selectedStations, setSelectedStations] = useState>([]) useEffect(() => { fetchLines(); fetchProjects() }, []) async function fetchLines() { try { const res = await fetch('/api/sdk/v1/iace/production-lines') if (res.ok) { const json = await res.json() setLines(json.lines || []) } } catch { /* ignore */ } finally { setLoading(false) } } async function fetchProjects() { try { const res = await fetch('/api/sdk/v1/iace/projects') if (res.ok) { const json = await res.json() setProjects((json.projects || []).map((p: Record) => ({ id: p.id, machine_name: p.machine_name, machine_type: p.machine_type, status: p.status, }))) } } catch { /* ignore */ } } function addStation() { setSelectedStations((prev) => [...prev, { projectId: '', stationType: 'assembly' }]) } function removeStation(idx: number) { setSelectedStations((prev) => prev.filter((_, i) => i !== idx)) } function updateStation(idx: number, field: 'projectId' | 'stationType', value: string) { setSelectedStations((prev) => prev.map((s, i) => i === idx ? { ...s, [field]: value } : s)) } async function handleCreate() { if (!lineName.trim()) return setCreating(true) try { // 1. Create the line const lineRes = await fetch('/api/sdk/v1/iace/production-lines', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: lineName.trim(), description: lineDesc.trim() }), }) if (!lineRes.ok) return const lineJson = await lineRes.json() const lineId = lineJson.line?.id || lineJson.id // 2. Add stations for (let i = 0; i < selectedStations.length; i++) { const s = selectedStations[i] if (!s.projectId) continue const proj = projects.find((p) => p.id === s.projectId) await fetch(`/api/sdk/v1/iace/production-lines/${lineId}/stations`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ project_id: s.projectId, station_type: s.stationType, station_label: proj?.machine_name || '', sort_order: i + 1, }), }) } setShowCreate(false) setLineName('') setLineDesc('') setSelectedStations([]) await fetchLines() } catch (err) { console.error('Failed to create line:', err) } finally { setCreating(false) } } if (loading) { return (
) } return (
IACE

Produktionslinien

Verkettete Fertigungsstrassen mit aggregierter Risikoansicht

{/* Create form */} {showCreate && (

Neue Produktionslinie erstellen

setLineName(e.target.value)} placeholder="z.B. Fertigungsstrasse Halle 3" className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white" />
setLineDesc(e.target.value)} placeholder="Optionale Beschreibung" className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white" />
{/* Stations */}
{selectedStations.length === 0 && (

Noch keine Stationen. Klicken Sie "+ Station hinzufuegen" um Projekte zuzuordnen.

)}
{selectedStations.map((s, i) => (
{i + 1}.
))}
)} {/* Lines list */} {lines.length > 0 && (
{lines.map((line) => (

{line.name}

{line.description &&

{line.description}

}
{line.station_count || 0} Stationen Aktualisiert: {new Date(line.updated_at || line.created_at).toLocaleDateString('de-DE')}
))}
)} {lines.length === 0 && !showCreate && (

Noch keine Produktionslinien

Produktionslinien verketten mehrere CE-Projekte zu einer Fertigungsstrasse.

)}
) }