muller-reporting-sys/app/admin/machines/[id]/page.tsx
2025-11-12 22:21:35 +03:00

135 lines
4.1 KiB
TypeScript

"use client"
import { useState, useEffect, use } from "react"
import { useRouter } from "next/navigation"
export default function EditMachinePage({ params }: { params: Promise<{ id: string }> }) {
const router = useRouter()
const { id } = use(params)
const [loading, setLoading] = useState(false)
const [formData, setFormData] = useState({
name: "",
machineType: "",
bottlesPerMin: 0,
status: ""
})
useEffect(() => {
fetch(`/api/admin/machines/${id}`)
.then(r => r.json())
.then(data => setFormData(data))
}, [id])
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault()
setLoading(true)
const response = await fetch(`/api/admin/machines/${id}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(formData)
})
if (response.ok) {
router.push("/admin/machines")
} else {
alert("Error updating machine")
setLoading(false)
}
}
const handleDelete = async () => {
if (!confirm("Are you sure you want to delete this machine?")) return
const response = await fetch(`/api/admin/machines/${id}`, {
method: "DELETE"
})
if (response.ok) {
router.push("/admin/machines")
} else {
alert("Error deleting machine")
}
}
return (
<div className="min-h-screen bg-gray-50 p-8">
<div className="max-w-2xl mx-auto">
<h1 className="text-3xl font-bold text-gray-800 mb-6">Edit Machine</h1>
<form onSubmit={handleSubmit} className="bg-white p-6 rounded-xl shadow-sm border border-gray-200 space-y-4">
<div>
<label className="block text-sm font-medium mb-2">Machine Name *</label>
<input
type="text"
value={formData.name}
onChange={(e) => setFormData({...formData, name: e.target.value})}
className="w-full px-4 py-2 border rounded-lg"
required
/>
</div>
<div>
<label className="block text-sm font-medium mb-2">Machine Type *</label>
<input
type="text"
value={formData.machineType}
onChange={(e) => setFormData({...formData, machineType: e.target.value})}
className="w-full px-4 py-2 border rounded-lg"
required
/>
</div>
<div>
<label className="block text-sm font-medium mb-2">Bottles Per Minute *</label>
<input
type="number"
value={formData.bottlesPerMin || ""}
onChange={(e) => setFormData({...formData, bottlesPerMin: parseInt(e.target.value) || 0})}
className="w-full px-4 py-2 border rounded-lg"
required
/>
</div>
<div>
<label className="block text-sm font-medium mb-2">Status *</label>
<select
value={formData.status}
onChange={(e) => setFormData({...formData, status: e.target.value})}
className="w-full px-4 py-2 border rounded-lg"
required
>
<option value="active">Active</option>
<option value="inactive">Inactive</option>
</select>
</div>
<div className="flex gap-4 pt-4">
<button
type="submit"
disabled={loading}
className="flex-1 bg-blue-600 text-white py-3 rounded-lg font-medium hover:bg-blue-700 transition-colors disabled:opacity-50"
>
{loading ? "Updating..." : "Update Machine"}
</button>
<button
type="button"
onClick={() => router.back()}
className="px-6 py-3 border border-gray-300 rounded-lg hover:bg-gray-50"
>
Cancel
</button>
<button
type="button"
onClick={handleDelete}
className="px-6 py-3 bg-red-600 text-white rounded-lg hover:bg-red-700"
>
Delete
</button>
</div>
</form>
</div>
</div>
)
}