import React, { useState, useEffect } from 'react'; import { CycleData, CyclePhase, CycleStatus } from '../types.ts'; import { useAppContext } from '../context/AppContext.tsx'; import CycleCreateModal from './cycle/CycleCreateModal.tsx'; const PhaseSegment: React.FC<{ phase: CyclePhase; totalDays: number }> = ({ phase, totalDays }) => { const widthPercentage = ((phase.endDay - phase.startDay + 1) / totalDays) * 100; return (
{phase.name}
); }; const statusConfig: Record = { Completed: { bg: 'bg-green-100', text: 'text-green-800', icon: 'fa-check-circle' }, Active: { bg: 'bg-red-100', text: 'text-red-800', icon: 'fa-running' }, Upcoming: { bg: 'bg-blue-100', text: 'text-blue-800', icon: 'fa-calendar-alt' }, }; const CycleDisplayHeader: React.FC<{ cycle: CycleData; }> = ({ cycle }) => { const getHarvestDate = () => { const harvestDate = new Date(cycle.startDate); harvestDate.setDate(harvestDate.getDate() + cycle.totalDays - 1); return harvestDate.toLocaleDateString('id-ID', { day: 'numeric', month: 'long', year: 'numeric', }); }; return (

ID Siklus: {cycle.id}

Estimasi Panen

{getHarvestDate()}

); }; const SingleCycleView: React.FC<{ cycle: CycleData }> = ({ cycle }) => { const { totalDays, startDate, phases, status } = cycle; // Hitung currentDay dinamis berdasarkan tanggal hari ini dan tanggal mulai siklus const today = new Date(); const start = new Date(startDate); start.setHours(0, 0, 0, 0); const todayMs = today.setHours(0, 0, 0, 0); const diffMsToday = todayMs - start.getTime(); let derivedCurrentDay = 0; if (diffMsToday >= 0) { derivedCurrentDay = Math.floor(diffMsToday / (1000 * 60 * 60 * 24)) + 1; if (derivedCurrentDay > totalDays) derivedCurrentDay = totalDays; } const progressPercentage = (derivedCurrentDay / totalDays) * 100; const getHarvestDate = () => { const harvestDate = new Date(startDate); harvestDate.setDate(harvestDate.getDate() + totalDays - 1); return harvestDate.toLocaleDateString('id-ID', { day: 'numeric', month: 'long', year: 'numeric', }); }; return (
{phases.map((phase) => ( ))}
{status === 'Active' && derivedCurrentDay > 0 && (
Hari ke-{derivedCurrentDay}
)}
Hari 1 {startDate.toLocaleDateString('id-ID', { day: 'numeric', month: 'short', year: 'numeric', })} {cycle.chickInWeight && ( Berat DOC: {cycle.chickInWeight} gram )} {cycle.docInCount && ( DOC In: {cycle.docInCount.toLocaleString()} ekor )}
Hari {totalDays} {getHarvestDate()}
); }; const CycleTimeline: React.FC = () => { const { cycles, onCreateCycle } = useAppContext(); const activeCycle = cycles.find((c) => c.status === 'Active'); const [selectedCycleId, setSelectedCycleId] = useState( activeCycle?.id || cycles[0]?.id || '' ); const [isCreateModalOpen, setIsCreateModalOpen] = useState(false); // Only set to active cycle on initial mount or when cycles change and current selection is empty useEffect(() => { if (!selectedCycleId) { const activeCycle = cycles.find((c) => c.status === 'Active'); if (activeCycle) setSelectedCycleId(activeCycle.id); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [cycles]); const selectedCycle = cycles.find((c) => c.id === selectedCycleId); return (

Linimasa Siklus Produksi

{selectedCycle ? :

No cycle selected.

} setIsCreateModalOpen(false)} onSave={(startDate, docInCount, chickInWeight) => { onCreateCycle(startDate, docInCount, chickInWeight); setIsCreateModalOpen(false); const year = startDate.getFullYear(); const month = String(startDate.getMonth() + 1).padStart(2, '0'); const day = String(startDate.getDate()).padStart(2, '0'); setSelectedCycleId(`CYCLE-JBW-${year}-${month}-${day}`); }} />
); }; export default CycleTimeline;