"use client" import { useState, useEffect } from "react" import { useRouter } from "next/navigation" export default function CreateShiftPage() { const router = useRouter() const [loading, setLoading] = useState(false) const [managerTeam, setManagerTeam] = useState(null) const [workers, setWorkers] = useState([]) const [machines, setMachines] = useState([]) const [formData, setFormData] = useState({ name: "AM", shiftDate: new Date().toISOString().split('T')[0], teamId: "", operators: Array(7).fill(""), level2Id: "", engineerId: "" }) useEffect(() => { // Fetch manager's team fetch(`/api/shift-manager/my-team`) .then(r => r.json()) .then(team => { if (team && !team.error) { setManagerTeam(team) setFormData(prev => ({ ...prev, teamId: team.id })) // Fetch workers for this team only fetch(`/api/workers?teamId=${team.id}`) .then(r => r.json()) .then(setWorkers) } }) .catch(err => console.error("Error fetching team:", err)) fetch('/api/machines').then(r => r.json()).then(data => { // Sort machines by name (T1, T2, T3, etc.) const sortedMachines = data.sort((a: any, b: any) => { const numA = parseInt(a.name.replace('T', '')) const numB = parseInt(b.name.replace('T', '')) return numA - numB }) setMachines(sortedMachines) }) }, []) // Get available operators for a specific machine index const getAvailableOperators = (currentIndex: number) => { const selectedOperatorIds = formData.operators.filter((id, idx) => id && idx !== currentIndex) return workers.filter((w: any) => w.jobPosition === "Blow Moulder Level 1" && !selectedOperatorIds.includes(w.id) ) } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setLoading(true) const response = await fetch('/api/shifts', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(formData) }) if (response.ok) { router.push('/shift-manager/shifts') } setLoading(false) } return (

Create New Shift

setFormData({...formData, shiftDate: e.target.value})} className="w-full px-4 py-2 border rounded-lg" required />

You can only create shifts for your assigned team

Assign Operators to Machines

Assign one operator to each machine. Once selected, an operator won't appear in other dropdowns.

{machines.slice(0, 7).map((machine: any, index: number) => { const availableOps = getAvailableOperators(index) const currentOperator = formData.operators[index] return (
{machine.name} {currentOperator && ( )}
) })}
{formData.operators.filter(op => op).length > 0 && (

{formData.operators.filter(op => op).length} of 7 operators assigned

)}
) }