import { useState } from 'react'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { HoverCard, HoverCardContent, HoverCardTrigger } from '@/components/ui/hover-card'; import { Calendar, Factory, TrendingUp, TrendingDown, Clock, Package, Wrench, Check } from 'lucide-react'; import { formatISK } from '@/utils/priceUtils'; import { IndJob } from '@/lib/types'; import { Input } from '@/components/ui/input'; import { useToast } from '@/components/ui/use-toast'; interface JobCardProps { job: IndJob; onEdit: (job: any) => void; onDelete: (jobId: string) => void; onUpdateProduced?: (jobId: string, produced: number) => void; isTracked?: boolean; } const JobCard: React.FC = ({ job, onEdit, onDelete, onUpdateProduced, isTracked = false }) => { const [isEditingProduced, setIsEditingProduced] = useState(false); const [producedValue, setProducedValue] = useState(job.produced?.toString() || '0'); const [copyingBom, setCopyingBom] = useState(false); const [copyingConsumed, setCopyingConsumed] = useState(false); const { toast } = useToast(); // Sort transactions by date descending const sortedExpenditures = [...job.expenditures].sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime() ); const sortedIncome = [...job.income].sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime() ); // Calculate totals for this job (including tracked jobs) const totalExpenditure = sortedExpenditures.reduce((sum, tx) => sum + tx.totalPrice, 0); const totalIncome = sortedIncome.reduce((sum, tx) => sum + tx.totalPrice, 0); const profit = totalIncome - totalExpenditure; const margin = totalIncome > 0 ? ((profit / totalIncome) * 100) : 0; const itemsSold = sortedIncome.reduce((sum, tx) => sum + tx.quantity, 0); const saleStartTime = job.saleStart ? new Date(job.saleStart).getTime() : null; const daysSinceStart = saleStartTime ? Math.max(1, Math.ceil((Date.now() - saleStartTime) / (1000 * 60 * 60 * 24))) : 0; const itemsPerDay = daysSinceStart > 0 ? itemsSold / daysSinceStart : 0; const getStatusColor = (status: string) => { switch (status) { case 'Planned': return 'bg-gray-600'; case 'Acquisition': return 'bg-yellow-600'; case 'Running': return 'bg-blue-600'; case 'Done': return 'bg-purple-600'; case 'Selling': return 'bg-orange-600'; case 'Closed': return 'bg-green-600'; case 'Tracked': return 'bg-cyan-600'; default: return 'bg-gray-600'; } }; const formatDateTime = (dateString: string | null | undefined) => { if (!dateString) return 'Not set'; return new Date(dateString).toLocaleString('en-CA', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit' }).replace(',', ''); }; const handleProducedUpdate = () => { const newValue = parseInt(producedValue); if (!isNaN(newValue) && onUpdateProduced) { onUpdateProduced(job.id, newValue); setIsEditingProduced(false); } else { setProducedValue(job.produced?.toString() || '0'); setIsEditingProduced(false); } }; const handleProducedKeyPress = (e: React.KeyboardEvent) => { if (e.key === 'Enter') { handleProducedUpdate(); } else if (e.key === 'Escape') { setIsEditingProduced(false); setProducedValue(job.produced?.toString() || '0'); } }; const copyBillOfMaterials = async () => { if (!job.billOfMaterials?.length) return; const text = job.billOfMaterials .map(item => `${item.name}\t${item.quantity.toLocaleString()}`) .join('\n'); try { await navigator.clipboard.writeText(text); setCopyingBom(true); toast({ title: "Copied!", description: "Bill of materials copied to clipboard", duration: 2000, }); setTimeout(() => setCopyingBom(false), 1000); } catch (err) { toast({ title: "Error", description: "Failed to copy to clipboard", variant: "destructive", duration: 2000, }); } }; const copyConsumedMaterials = async () => { if (!job.consumedMaterials?.length) return; const text = job.consumedMaterials .map(item => `${item.name}\t${item.quantity.toLocaleString()}`) .join('\n'); try { await navigator.clipboard.writeText(text); setCopyingConsumed(true); toast({ title: "Copied!", description: "Consumed materials copied to clipboard", duration: 2000, }); setTimeout(() => setCopyingConsumed(false), 1000); } catch (err) { toast({ title: "Error", description: "Failed to copy to clipboard", variant: "destructive", duration: 2000, }); } }; return (
{job.outputItem} {job.status} {job.billOfMaterials && job.billOfMaterials.length > 0 && (

Bill of Materials (click to copy)

{job.billOfMaterials.map((item, index) => (
{item.name} {item.quantity.toLocaleString()}
))}
)} {job.consumedMaterials && job.consumedMaterials.length > 0 && (

Consumed Materials (click to copy)

{job.consumedMaterials.map((item, index) => (
{item.name} {item.quantity.toLocaleString()}
))}
)}

Quantity: {job.outputQuantity.toLocaleString()} Produced: { isEditingProduced && job.status !== 'Closed' ? ( setProducedValue(e.target.value)} onBlur={handleProducedUpdate} onKeyDown={handleProducedKeyPress} className="w-24 h-6 px-2 py-1 inline-block bg-gray-800 border-gray-600 text-white" min="0" autoFocus /> ) : ( job.status !== 'Closed' && setIsEditingProduced(true)} className={job.status !== 'Closed' ? "cursor-pointer hover:text-blue-400" : undefined} title={job.status !== 'Closed' ? "Click to edit" : undefined} > {(job.produced || 0).toLocaleString()} ) } Sold: {itemsSold.toLocaleString()}

Created: {formatDateTime(job.created)}
Start: {formatDateTime(job.jobStart)}
Job ID: {job.id}
{job.saleStart && (
Sale Period: {formatDateTime(job.saleStart)} - {formatDateTime(job.saleEnd)}
{itemsPerDay > 0 && (
Items/Day: {itemsPerDay.toFixed(2)}
)}
)}
Expenditure
{formatISK(totalExpenditure)}
{job.projectedCost > 0 && (
vs Projected: {formatISK(job.projectedCost)} {((totalExpenditure / job.projectedCost) * 100).toFixed(1)}%
)}
Income
{formatISK(totalIncome)}
{job.projectedRevenue > 0 && (
vs Projected: {formatISK(job.projectedRevenue)} = job.projectedRevenue ? 'default' : 'destructive'} className="ml-1 text-xs" > {((totalIncome / job.projectedRevenue) * 100).toFixed(1)}%
)}
Profit
= 0 ? 'text-green-400' : 'text-red-400'}`}> {formatISK(profit)} = 0 ? 'default' : 'destructive'} className="ml-1 text-xs"> {margin.toFixed(1)}%
{job.projectedRevenue > 0 && job.projectedCost > 0 && (
vs Projected: {formatISK(job.projectedRevenue - job.projectedCost)} = (job.projectedRevenue - job.projectedCost) ? 'default' : 'destructive'} className="ml-1 text-xs" > {((profit / (job.projectedRevenue - job.projectedCost)) * 100).toFixed(1)}%
)}
); }; export default JobCard;