import { useState, useEffect, useRef } from 'react' // ── Helpers ─────────────────────────────────────────────────────────────────── const DECIMAL_UNIT_CONFIG = { kg: { step: 0.05, min: 0.05, presets: [0.10, 0.20, 0.25, 0.50, 0.70, 1.00, 1.50, 2.00, 2.50, 3.00, 4.00, 5.00], offsets: [0.1, 0.2, 0.5, 1.0], label: 'kg' }, liter: { step: 0.05, min: 0.05, presets: [0.10, 0.20, 0.25, 0.50, 0.70, 1.00, 1.50, 2.00, 2.50, 3.00, 4.00, 5.00], offsets: [0.1, 0.2, 0.5, 1.0], label: 'L' }, gram: { step: 10, min: 10, presets: [50, 100, 150, 200, 250, 300, 400, 500], offsets: [10, 50, 100, 200], label: 'g' }, ml: { step: 5, min: 5, presets: [25, 50, 100, 150, 200, 250, 300, 500], offsets: [25, 50, 100, 200], label: 'mL' }, } function fmtDecimalQty(qty, cfg) { if (!cfg) return String(qty) if (cfg.label === 'kg' || cfg.label === 'L') { const n = Number(qty) return n % 1 === 0 ? n.toFixed(1) : (Math.round(n * 100) / 100).toFixed(2).replace(/0$/, '') } return String(qty) } function buildInitialState(product) { const preferenceSets = product.preference_sets || [] const prefs = {} const subChoices = {} const sharedSubs = {} preferenceSets.forEach(ps => { const def = ps.default_choice_id != null ? ps.choices.find(c => c.id === ps.default_choice_id) ?? null : null prefs[ps.id] = def if (def) { if (def.sub_choices?.length > 0) { subChoices[def.id] = def.sub_choices.find(s => s.is_default) ?? def.sub_choices[0] } if (ps.shared_subset?.choices?.length > 0 && !def.disables_subset) { sharedSubs[ps.id] = ps.shared_subset.choices.find(s => s.is_default) ?? ps.shared_subset.choices[0] } } }) return { prefs, subChoices, sharedSubs } } const QUICK_NOTES = ['Χωρίς αλάτι', 'Βγάλτε γρήγορα', 'Αλλεργία!', 'Κόψτε σε μικρά κομμάτια', 'Έξτρα χαρτοπετσέτες'] const PRICE_DELTAS = [-5, -3, -2, -1, -0.5, -0.2, +0.2, +0.5, +1, +2, +3, +5] // ── Primitive UI ────────────────────────────────────────────────────────────── function CheckCircle({ selected }) { return (
{selected && }
) } function RadioDot({ selected }) { return (
{selected &&
}
) } function Row({ selected, onClick, children, right, left }) { return (
{left &&
{left}
}
{children}
{right &&
{right}
}
) } function Stepper({ value, onChange, min = 0, max = 99 }) { return (
e.stopPropagation()}>
{value}
) } // ── Option rows ─────────────────────────────────────────────────────────────── function QuickOptionRow({ opt, quickState, setQuickState }) { const qty = quickState[opt.id] || 0 const selected = qty > 0 return ( setQuickState(s => ({ ...s, [opt.id]: selected ? 0 : 1 }))} left={} right={opt.allow_multiple ? (
e.stopPropagation()}> {selected ? setQuickState(s => ({ ...s, [opt.id]: v }))} /> : }
) : null} >
{opt.name}
{opt.price > 0 &&
+{opt.price.toFixed(2)} €
}
) } function ExtraOptionRow({ opt, extrasState, setExtrasState, expandedExtra, setExpandedExtra }) { const sel = extrasState[opt.id] const selected = !!sel const open = expandedExtra === opt.id const hasSubs = opt.sub_choices?.length > 0 const subLabel = sel ? opt.sub_choices?.find(s => s.name === sel.subName)?.name : null const toggle = () => { if (selected) { setExtrasState(s => { const n = { ...s }; delete n[opt.id]; return n }) if (open) setExpandedExtra(null) } else { const firstSub = hasSubs ? opt.sub_choices[0] : null setExtrasState(s => ({ ...s, [opt.id]: { qty: 1, subName: firstSub?.name ?? null } })) if (hasSubs) setExpandedExtra(opt.id) } } return (
} right={
e.stopPropagation()}> {selected && opt.allow_multiple && ( { if (v === 0) { setExtrasState(s => { const n = { ...s }; delete n[opt.id]; return n }); return } setExtrasState(s => ({ ...s, [opt.id]: { ...sel, qty: v } })) }} /> )} {selected && hasSubs && ( )}
} >
{opt.name}
{(opt.extra_cost ?? 0) !== 0 ? `+${opt.extra_cost.toFixed(2)} €` : 'Included'} {subLabel && · {subLabel}}
{selected && open && hasSubs && (
Επιλογή
{opt.sub_choices.map((sub, si) => { const isSel = sel.subName === sub.name return ( setExtrasState(s => ({ ...s, [opt.id]: { ...sel, subName: sub.name } }))} left={}>
{sub.name}
{(sub.extra_cost ?? 0) !== 0 &&
+{sub.extra_cost.toFixed(2)} €
}
) })}
)}
) } function IngredientRow({ ing, removedState, setRemovedState }) { const removed = !!removedState[ing.id] return ( setRemovedState(s => ({ ...s, [ing.id]: !s[ing.id] }))} right={
{removed ? 'Αφαιρέθηκε' : 'Αφαίρεση'}
} >
{ing.name}
) } function PrefSetBlock({ ps, prefs, setPrefs, subChoices, setSubChoices, sharedSubs, setSharedSubs }) { const selChoice = prefs[ps.id] ?? null const complete = selChoice != null && !(selChoice.sub_choices?.length > 0 && subChoices[selChoice.id] == null) && !(ps.shared_subset?.choices?.length > 0 && !selChoice.disables_subset && sharedSubs[ps.id] == null) const showShared = ps.shared_subset?.choices?.length > 0 && selChoice != null && !selChoice.disables_subset function selectPref(choice) { setPrefs(p => ({ ...p, [ps.id]: choice })) if (choice?.sub_choices?.length > 0) { setSubChoices(s => ({ ...s, [choice.id]: s[choice.id] ?? (choice.sub_choices.find(x => x.is_default) ?? choice.sub_choices[0]) })) } if (ps.shared_subset?.choices?.length > 0 && !choice?.disables_subset) { setSharedSubs(s => s[ps.id] != null ? s : { ...s, [ps.id]: ps.shared_subset.choices.find(x => x.is_default) ?? ps.shared_subset.choices[0] }) } } return (
{ps.name}
{!complete &&
Απαιτείται
}
{ps.choices.map(ch => { const isSel = selChoice?.id === ch.id const hasSubs = ch.sub_choices?.length > 0 return (
selectPref(ch)} left={} right={(ch.extra_cost ?? 0) !== 0 ?
{ch.extra_cost > 0 ? '+' : ''}{ch.extra_cost.toFixed(2)} €
: null}>
{ch.name}
{isSel && hasSubs && (
{ch.sub_choices.map((sub, si) => { const subSel = subChoices[ch.id]?.name === sub.name return ( setSubChoices(s => ({ ...s, [ch.id]: sub }))} left={} right={(sub.extra_cost ?? 0) !== 0 ?
+{sub.extra_cost.toFixed(2)} €
: null}>
{sub.name}
) })}
)}
) })} {showShared && (
{ps.shared_subset.name}
{ps.shared_subset.choices.map((sub, si) => { const subSel = sharedSubs[ps.id]?.name === sub.name return ( setSharedSubs(s => ({ ...s, [ps.id]: sub }))} left={} right={(sub.extra_cost ?? 0) !== 0 ?
+{sub.extra_cost.toFixed(2)} €
: null}>
{sub.name}
) })}
)}
) } // ── Tab content ─────────────────────────────────────────────────────────────── function QuickTab({ product, quickState, setQuickState }) { const quickOptions = product.quick_options || [] if (quickOptions.length === 0) return

Δεν υπάρχουν γρήγορες επιλογές.

return (
{quickOptions.map(opt => (
))}
) } function ExtrasTab({ product, extrasState, setExtrasState, expandedExtra, setExpandedExtra }) { const options = product.options || [] if (options.length === 0) return

Δεν υπάρχουν extras.

return (
{options.map(opt => ( ))}
) } function IngredientsTab({ product, removedState, setRemovedState }) { const ingredients = product.ingredients || [] if (ingredients.length === 0) return

Δεν υπάρχουν υλικά.

return (
Κάντε κλικ για να αφαιρέσετε υλικό.
{ingredients.map(ing => ( ))}
) } function PrefsTab({ product, prefs, setPrefs, subChoices, setSubChoices, sharedSubs, setSharedSubs }) { const preferenceSets = product.preference_sets || [] if (preferenceSets.length === 0) return

Δεν υπάρχουν προτιμήσεις.

return (
{preferenceSets.map(ps => ( ))}
) } function QuantityTab({ unitCfg, qty, setQty }) { const [editing, setEditing] = useState(false) const [qtyInput, setQtyInput] = useState('') const inputRef = useRef(null) function clamp(v) { return Math.max(unitCfg.min, Math.round(v * 1000) / 1000) } return (
{editing ? ( setQtyInput(e.target.value)} onBlur={() => { const v = parseFloat(qtyInput); if (!isNaN(v)) setQty(clamp(v)); setEditing(false) }} onKeyDown={e => { if (e.key === 'Enter') e.target.blur() }} autoFocus style={{ width: 70, height: 48, border: 'none', background: 'transparent', textAlign: 'center', fontSize: 16, fontWeight: 700, color: '#1e293b', outline: 'none', fontFamily: 'inherit' }} /> ) : (
{ setQtyInput(String(qty)); setEditing(true) }} style={{ minWidth: 70, textAlign: 'center', fontSize: 16, fontWeight: 700, color: '#1e293b', cursor: 'text', display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 3 }}> {fmtDecimalQty(qty, unitCfg)} {unitCfg.label}
)}

ΓΡΗΓΟΡΗ ΕΠΙΛΟΓΗ

{unitCfg.presets.map(v => ( ))}

ΓΡΗΓΟΡΗ ΠΡΟΣΘΗΚΗ

{unitCfg.offsets.map(off => ( ))} {unitCfg.offsets.map(off => ( ))}
) } function PriceAdjustTab({ basePrice, priceAdj, setPriceAdj }) { const [editing, setEditing] = useState(false) const [rawInput, setRawInput] = useState('') const inputRef = useRef(null) const effectivePrice = basePrice + priceAdj function applyDelta(d) { setPriceAdj(a => { const next = Math.round((a + d) * 100) / 100 return Math.max(-(basePrice - 0.01), next) }) } function commitInput() { const v = parseFloat(rawInput.replace(',', '.')) if (!isNaN(v) && v >= 0.01) setPriceAdj(Math.round((v - basePrice) * 100) / 100) setEditing(false) } useEffect(() => { if (editing && inputRef.current) inputRef.current.focus() }, [editing]) return (
Αλλαγή τιμής μόνο για αυτό το αντικείμενο.
{editing ? ( setRawInput(e.target.value)} onBlur={commitInput} onKeyDown={e => { if (e.key === 'Enter') e.target.blur() }} style={{ width: 100, height: 48, border: '2px solid #f59e0b', borderRadius: 12, background: 'white', textAlign: 'center', fontSize: 20, fontWeight: 800, color: '#f59e0b', outline: 'none', fontFamily: 'inherit' }} /> ) : ( )}
{priceAdj !== 0 && (
Βάση: {basePrice.toFixed(2)} € 0 ? '#16a34a' : '#dc2626' }}>{priceAdj > 0 ? '+' : ''}{priceAdj.toFixed(2)} €
)}
{PRICE_DELTAS.map(d => ( ))}
) } function NotesTab({ note, setNote }) { return (