"use client"; import React, { useState, useRef } from 'react'; import { Operator } from './types'; import { Button, TextField, Typography, Paper, Box, IconButton, Stack } from '@mui/material'; import CloseIcon from '@mui/icons-material/Close'; interface OperatorScannerProps { operators: Operator[]; onOperatorsChange: (operators: Operator[]) => void; error?: string; } const operatorDatabase: Operator[] = [ { id: 1, name: 'John Smith', employeeId: 'EMP001', cardId: '12345678' }, { id: 2, name: 'Maria Garcia', employeeId: 'EMP002', cardId: '23456789' }, { id: 3, name: 'David Chen', employeeId: 'EMP003', cardId: '34567890' }, { id: 4, name: 'Sarah Johnson', employeeId: 'EMP004', cardId: '45678901' }, { id: 5, name: 'Mike Wilson', employeeId: 'EMP005', cardId: '56789012' } ]; const OperatorScanner: React.FC = ({ operators, onOperatorsChange, error }) => { const [scanningMode, setScanningMode] = useState(false); const operatorScanRef = useRef(null); const startScanning = (): void => { setScanningMode(true); setTimeout(() => { if (operatorScanRef.current) { operatorScanRef.current.focus(); } }, 100); }; const stopScanning = (): void => { setScanningMode(false); }; const handleOperatorScan = (e: React.KeyboardEvent): void => { if (e.key === 'Enter') { const target = e.target as HTMLInputElement; const scannedId = target.value.trim(); const operator = operatorDatabase.find(op => op.cardId === scannedId || op.employeeId === scannedId ); if (operator) { const isAlreadyAdded = operators.some(op => op.id === operator.id); if (!isAlreadyAdded) { onOperatorsChange([...operators, operator]); } target.value = ''; stopScanning(); } else { alert('Operator not found. Please check the ID and try again.'); target.value = ''; } } }; const removeOperator = (operatorId: number): void => { onOperatorsChange(operators.filter(op => op.id !== operatorId)); }; return ( Operators * {scanningMode && ( Position the ID card scanner and scan, or type the employee ID manually )} {error && {error}} {operators.map((operator) => ( {operator.name} {operator.employeeId} removeOperator(operator.id)} color="error"> ))} {operators.length === 0 && ( No operators added yet. Use the scan button to add operators. )} ); }; export default OperatorScanner;