Files

143 lines
5.2 KiB
TypeScript

import React, { useState } from 'react';
interface CycleCreateModalProps {
isOpen: boolean;
onClose: () => void;
onSave: (startDate: Date, docInCount?: number, chickInWeight?: number) => void;
}
const CycleCreateModal: React.FC<CycleCreateModalProps> = ({ isOpen, onClose, onSave }) => {
const [startDate, setStartDate] = useState(new Date().toISOString().split('T')[0] ?? '');
const [docInCount, setDocInCount] = useState('20000');
const [chickInWeight, setChickInWeight] = useState('');
const [error, setError] = useState('');
const generatedId = `CYCLE-JBW-${startDate}`;
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
const start = new Date(startDate);
const docCount = docInCount ? parseInt(docInCount, 10) : undefined;
const weight = chickInWeight ? parseInt(chickInWeight, 10) : undefined;
if (isNaN(start.getTime())) {
setError('Tanggal mulai tidak valid');
return;
}
if (docInCount && (isNaN(docCount!) || docCount! <= 0)) {
setError('Jumlah DOC harus berupa angka positif');
return;
}
if (chickInWeight && (isNaN(weight!) || weight! <= 0)) {
setError('Berat DOC harus berupa angka positif');
return;
}
onSave(start, docCount, weight);
onClose();
};
if (!isOpen) return null;
return (
<div
className="fixed inset-0 bg-black bg-opacity-60 flex justify-center items-center z-50 p-4"
onClick={onClose}
>
<div
className="bg-white rounded-xl shadow-2xl w-full max-w-lg"
onClick={(e) => e.stopPropagation()}
>
<form onSubmit={handleSubmit}>
<div className="p-6">
<div className="flex justify-between items-center mb-6">
<h2 className="text-2xl font-bold text-gray-800">Siklus Produksi Baru</h2>
<button
type="button"
onClick={onClose}
className="text-gray-400 hover:text-gray-600 text-2xl"
>
&times;
</button>
</div>
{error && <p className="text-sm text-red-600 mb-4">{error}</p>}
<div className="space-y-4">
<div className="bg-gray-50 p-3 rounded-lg">
<p className="text-xs text-gray-500">ID Siklus (otomatis)</p>
<p className="font-mono font-semibold text-gray-800">{generatedId}</p>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Tanggal Mulai (Chick-in)
</label>
<input
type="date"
value={startDate}
onChange={(e) => {
setStartDate(e.target.value);
setError('');
}}
className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:ring-red-500 focus:border-red-500"
required
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Jumlah DOC In (ekor)
</label>
<input
type="number"
value={docInCount}
onChange={(e) => {
setDocInCount(e.target.value);
setError('');
}}
placeholder="Contoh: 20000"
className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:ring-red-500 focus:border-red-500"
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Berat DOC (gram, opsional)
</label>
<input
type="number"
value={chickInWeight}
onChange={(e) => {
setChickInWeight(e.target.value);
setError('');
}}
placeholder="Contoh: 40"
className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:ring-red-500 focus:border-red-500"
/>
</div>
<div className="bg-blue-50 p-3 rounded-lg text-sm text-blue-700">
<i className="fa-solid fa-info-circle mr-1"></i>
Durasi siklus: 42 hari (standar broiler). Siklus aktif sebelumnya akan otomatis
ditandai selesai.
</div>
</div>
</div>
<div className="bg-gray-50 px-6 py-4 rounded-b-xl flex justify-end space-x-3">
<button
type="button"
onClick={onClose}
className="bg-gray-200 text-gray-700 px-4 py-2 rounded-lg hover:bg-gray-300 font-medium"
>
Batal
</button>
<button
type="submit"
className="bg-red-600 text-white px-4 py-2 rounded-lg hover:bg-red-700 font-medium shadow-sm"
>
<i className="fa-solid fa-plus mr-1"></i>Buat Siklus
</button>
</div>
</form>
</div>
</div>
);
};
export default CycleCreateModal;