import { useState, useEffect, useRef } from 'react' import { useQuery } from '@tanstack/react-query' import client from '../../api/client' import { useTranslation } from '@xenia/i18n' import { SALES_POINTS } from '@xenia/access' import MediaField from '../media/MediaField' // ── Small helpers ───────────────────────────────────────────────────────────── function moveItem(arr, i, dir) { const j = i + dir if (j < 0 || j >= arr.length) return arr const next = [...arr] ;[next[i], next[j]] = [next[j], next[i]] return next } function HeartIcon({ filled, className = '' }) { return ( ) } function ReorderBtns({ onUp, onDown, disableUp, disableDown }) { return (
) } function DefaultBtn({ isDefault, onClick }) { return ( ) } function FavoriteBtn({ isFavorite, onClick }) { return ( ) } function PriceInput({ value, onChange, placeholder, className = '', allowNegative = false }) { const step = 0.10 const num = parseFloat(value) || 0 function inc() { onChange(Math.round((num + step) * 100) / 100) } function dec() { const next = Math.round((num - step) * 100) / 100 onChange(allowNegative ? next : Math.max(0, next)) } return (
onChange(e.target.value)} placeholder={placeholder ?? '0.00'} className="flex-1 min-w-0 text-center text-sm outline-none bg-transparent px-1 [appearance:textfield] [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none" />
) } function SubChoiceRows({ subChoices, onMove, onToggleDefault, onChange, onRemove, onAdd, parentLabel, showMultiple = false }) { if (!subChoices || subChoices.length === 0) return null return (

Υπο-επιλογές του «{parentLabel || '…'}»

{subChoices.map((sc, sci) => (
onMove(sci, -1)} onDown={() => onMove(sci, 1)} disableUp={sci === 0} disableDown={sci === subChoices.length - 1} /> onToggleDefault(sci)} /> onChange(sci, 'name', e.target.value)} /> onChange(sci, 'extra_cost', v)} allowNegative className="w-28 text-sm" /> {showMultiple && ( )}
))}
) } // ── Group helpers ───────────────────────────────────────────────────────────── function GroupHeader({ group, onRename, onDelete, collapsed, onToggle }) { const [editing, setEditing] = useState(false) const [val, setVal] = useState(group.name) return (
) } // ── Form builder ────────────────────────────────────────────────────────────── function buildFormFromProduct(product) { return { name: product.name || '', description: product.description || '', category_id: product.category_id ?? '', base_price: product.base_price ?? '', unit_type: product.unit_type ?? 'piece', is_available: product.is_available ?? true, lifecycle_status: product.lifecycle_status ?? 'active', prep_zone_ids: product.prep_zone_ids ?? [], quick_options: product.quick_options?.map(q => ({ name: q.name, price: q.price ?? 0, allow_multiple: q.allow_multiple ?? false, sort_order: q.sort_order ?? 0, is_favorite: q.is_favorite ?? false, favorite_sort_order: q.favorite_sort_order ?? 0, is_compact: q.is_compact ?? false, })) ?? [], options: product.options?.map(o => ({ name: o.name, extra_cost: o.extra_cost ?? 0, allow_multiple: o.allow_multiple ?? false, multi_select: o.multi_select ?? false, is_compact: o.is_compact ?? false, sub_choices: o.sub_choices?.map(s => ({ name: s.name, extra_cost: s.extra_cost ?? 0, is_default: s.is_default ?? false, allow_multiple: s.allow_multiple ?? false, is_compact: s.is_compact ?? false })) ?? [], is_favorite: o.is_favorite ?? false, favorite_sort_order: o.favorite_sort_order ?? 0, group_id: o.group_id ?? null, })) ?? [], ingredients: product.ingredients?.map(i => ({ name: i.name, extra_cost: i.extra_cost ?? 0, is_favorite: i.is_favorite ?? false, favorite_sort_order: i.favorite_sort_order ?? 0, is_compact: i.is_compact ?? false, group_id: i.group_id ?? null, })) ?? [], preference_sets: product.preference_sets?.map(ps => ({ name: ps.name, default_choice_index: ps.choices ? ps.choices.findIndex(c => c.id === ps.default_choice_id) : -1, choices: ps.choices?.map(c => ({ name: c.name, extra_cost: c.extra_cost ?? 0, disables_subset: c.disables_subset ?? false, is_compact: c.is_compact ?? false, sub_choices: c.sub_choices?.map(s => ({ name: s.name, extra_cost: s.extra_cost ?? 0, is_default: s.is_default ?? false, is_compact: s.is_compact ?? false })) ?? [], })) ?? [], group_id: ps.group_id ?? null, shared_subset: ps.shared_subset ? { name: ps.shared_subset.name, choices: ps.shared_subset.choices?.map(s => ({ name: s.name, extra_cost: s.extra_cost ?? 0, is_default: s.is_default ?? false })) ?? [], } : null, is_favorite: ps.is_favorite ?? false, favorite_sort_order: ps.favorite_sort_order ?? 0, allow_multi_select: ps.allow_multi_select ?? false, allow_choice_quantity: ps.allow_choice_quantity ?? false, })) ?? [], sold_at: product.sold_at ?? [...SALES_POINTS], digital_available: product.digital_available ?? true, digital_name: product.digital_name ?? '', digital_description: product.digital_description ?? '', digital_image_url: product.digital_image_url ?? '', // Its photo in the media library (plan step 5.2): { id, name, thumb, card, full } or null image: product.image ?? null, // Phase 2A — cost tracking cost_mode: product.cost_breakdown?.length ? 'detailed' : 'simple', cost_simple: product.cost_simple ?? '', cost_breakdown: product.cost_breakdown?.length ? product.cost_breakdown.map(e => ({ label: e.label, amount: e.amount })) : [], // Quick Add quick_add_enabled: product.quick_add_enabled ?? true, // Tags tags: product.tags ?? [], tagsInput: '', // Service items is_service_item: product.is_service_item ?? false, // Fiscal printer (ΦΗΜ) fiscal_name: product.fiscal_name ?? '', fiscal_vat_group_id: product.fiscal_vat_group_id ?? null, // Modifier groups (folders) — keyed by _tempId in form, id from server modifier_groups: (product.modifier_groups || []).map(g => ({ _tempId: g.id, id: g.id, modifier_type: g.modifier_type, name: g.name, sort_order: g.sort_order })), } } function buildFavoritesList(form) { const items = [] form.quick_options.forEach((q, i) => { if (q.is_favorite) items.push({ type: 'quick', idx: i, favorite_sort_order: q.favorite_sort_order ?? 0 }) }) form.ingredients.forEach((ing, i) => { if (ing.is_favorite) items.push({ type: 'ingredient', idx: i, favorite_sort_order: ing.favorite_sort_order ?? 0 }) }) form.options.forEach((o, i) => { if (o.is_favorite) items.push({ type: 'option', idx: i, favorite_sort_order: o.favorite_sort_order ?? 0 }) }) form.preference_sets.forEach((ps, i) => { if (ps.is_favorite) items.push({ type: 'pref', idx: i, favorite_sort_order: ps.favorite_sort_order ?? 0 }) }) return items.sort((a, b) => a.favorite_sort_order - b.favorite_sort_order) } function setFavSortField(form, type, idx, value) { if (type === 'quick') return { ...form, quick_options: form.quick_options.map((q, i) => i === idx ? { ...q, favorite_sort_order: value } : q) } if (type === 'ingredient') return { ...form, ingredients: form.ingredients.map((ing, i) => i === idx ? { ...ing, favorite_sort_order: value } : ing) } if (type === 'option') return { ...form, options: form.options.map((o, i) => i === idx ? { ...o, favorite_sort_order: value } : o) } if (type === 'pref') return { ...form, preference_sets: form.preference_sets.map((ps, i) => i === idx ? { ...ps, favorite_sort_order: value } : ps) } return form } function getItemLabel(form, type, idx) { if (type === 'quick') return form.quick_options[idx]?.name || '(χωρίς όνομα)' if (type === 'ingredient') return form.ingredients[idx]?.name || '(χωρίς όνομα)' if (type === 'option') return form.options[idx]?.name || '(χωρίς όνομα)' if (type === 'pref') return form.preference_sets[idx]?.name || '(χωρίς όνομα)' return '' } function getItemTypeLabel(type) { if (type === 'quick') return 'Γρήγορη' if (type === 'ingredient') return 'Υλικό' if (type === 'option') return 'Έξτρα' if (type === 'pref') return 'Προτίμηση' return '' } // ── Main modal ──────────────────────────────────────────────────────────────── export default function ProductFormModal({ product, categories, printers, prepZones = [], onSave, onCopy, onClose }) { const [form, setForm] = useState(() => buildFormFromProduct(product)) const [activeTab, setActiveTab] = useState('favorites') const [leftTab, setLeftTab] = useState('info') const { t: tm } = useTranslation('manager') // Fetch fiscal VAT groups from settings (needed for the Ταμειακή tab dropdown) const { data: settingsData } = useQuery({ queryKey: ['pos-settings'], queryFn: () => client.get('/api/settings/').then(r => r.data), staleTime: 60_000, }) const fiscalEnabled = settingsData?.['fiscal.enabled']?.value === 'true' const vatGroups = (() => { try { return JSON.parse(settingsData?.['fiscal.vat_groups']?.value || '[]') } catch { return [] } })() useEffect(() => { // Escape inside a dialog on top (the media picker) closes only that dialog function onKey(e) { if (e.key === 'Escape' && !document.querySelector('.x-dialog')) onClose() } window.addEventListener('keydown', onKey) return () => window.removeEventListener('keydown', onKey) }, [onClose]) useEffect(() => { setForm(buildFormFromProduct(product)) setActiveTab('favorites') setLeftTab('info') }, [product.id, product.name]) function setField(k, v) { setForm(f => ({ ...f, [k]: v })) } // Favorites reorder function moveFavorite(favList, favIdx, dir) { const newList = [...favList] const swapIdx = favIdx + dir if (swapIdx < 0 || swapIdx >= newList.length) return const aOrder = newList[favIdx].favorite_sort_order const bOrder = newList[swapIdx].favorite_sort_order setForm(f => { let next = setFavSortField(f, newList[favIdx].type, newList[favIdx].idx, bOrder) next = setFavSortField(next, newList[swapIdx].type, newList[swapIdx].idx, aOrder) return next }) } function toggleFavorite(type, idx) { setForm(f => { const currentFavs = buildFavoritesList(f) const isFav = (() => { if (type === 'quick') return f.quick_options[idx]?.is_favorite if (type === 'ingredient') return f.ingredients[idx]?.is_favorite if (type === 'option') return f.options[idx]?.is_favorite if (type === 'pref') return f.preference_sets[idx]?.is_favorite })() const newSortOrder = isFav ? 0 : (currentFavs.length > 0 ? Math.max(...currentFavs.map(x => x.favorite_sort_order)) + 1 : 0) if (type === 'quick') return { ...f, quick_options: f.quick_options.map((q, i) => i === idx ? { ...q, is_favorite: !isFav, favorite_sort_order: isFav ? 0 : newSortOrder } : q) } if (type === 'ingredient') return { ...f, ingredients: f.ingredients.map((ing, i) => i === idx ? { ...ing, is_favorite: !isFav, favorite_sort_order: isFav ? 0 : newSortOrder } : ing) } if (type === 'option') return { ...f, options: f.options.map((o, i) => i === idx ? { ...o, is_favorite: !isFav, favorite_sort_order: isFav ? 0 : newSortOrder } : o) } if (type === 'pref') return { ...f, preference_sets: f.preference_sets.map((ps, i) => i === idx ? { ...ps, is_favorite: !isFav, favorite_sort_order: isFav ? 0 : newSortOrder } : ps) } return f }) } // Quick Options function addQuickOption() { setForm(f => ({ ...f, quick_options: [...f.quick_options, { name: '', price: 0, allow_multiple: false, sort_order: f.quick_options.length, is_favorite: false, favorite_sort_order: 0, is_compact: false }] })) } function removeQuickOption(i) { setForm(f => ({ ...f, quick_options: f.quick_options.filter((_, idx) => idx !== i) })) } function setQuickOption(i, k, v) { setForm(f => ({ ...f, quick_options: f.quick_options.map((q, idx) => idx === i ? { ...q, [k]: v } : q) })) } function moveQuickOption(i, dir) { setForm(f => ({ ...f, quick_options: moveItem(f.quick_options, i, dir) })) } // Options function addOption() { setForm(f => ({ ...f, options: [...f.options, { name: '', extra_cost: 0, allow_multiple: false, multi_select: false, is_compact: false, sub_choices: [], is_favorite: false, favorite_sort_order: 0 }] })) } function removeOption(i) { setForm(f => ({ ...f, options: f.options.filter((_, idx) => idx !== i) })) } function setOption(i, k, v) { setForm(f => ({ ...f, options: f.options.map((o, idx) => idx === i ? { ...o, [k]: v } : o) })) } function moveOption(i, dir) { setForm(f => ({ ...f, options: moveItem(f.options, i, dir) })) } function addOptionSubChoice(oi) { setForm(f => ({ ...f, options: f.options.map((o, idx) => idx !== oi ? o : { ...o, sub_choices: [...(o.sub_choices || []), { name: '', extra_cost: 0, is_default: false, allow_multiple: false, is_compact: false }] } )})) } function removeOptionSubChoice(oi, sci) { setForm(f => ({ ...f, options: f.options.map((o, idx) => idx !== oi ? o : { ...o, sub_choices: o.sub_choices.filter((_, i) => i !== sci) } )})) } function setOptionSubChoice(oi, sci, k, v) { setForm(f => ({ ...f, options: f.options.map((o, idx) => idx !== oi ? o : { ...o, sub_choices: o.sub_choices.map((sc, scidx) => { if (scidx !== sci) return k === 'is_default' && v === true ? { ...sc, is_default: false } : sc return { ...sc, [k]: v } }) } )})) } function moveOptionSubChoice(oi, sci, dir) { setForm(f => ({ ...f, options: f.options.map((o, idx) => idx !== oi ? o : { ...o, sub_choices: moveItem(o.sub_choices, sci, dir) } )})) } function toggleOptionSubDefault(oi, sci) { const sc = form.options[oi]?.sub_choices?.[sci] setOptionSubChoice(oi, sci, 'is_default', !sc?.is_default) } // Ingredients function addIngredient() { setForm(f => ({ ...f, ingredients: [...f.ingredients, { name: '', extra_cost: 0, is_favorite: false, favorite_sort_order: 0, is_compact: false }] })) } function removeIngredient(i) { setForm(f => ({ ...f, ingredients: f.ingredients.filter((_, idx) => idx !== i) })) } function setIngredient(i, k, v) { setForm(f => ({ ...f, ingredients: f.ingredients.map((ing, idx) => idx === i ? { ...ing, [k]: v } : ing) })) } function moveIngredient(i, dir) { setForm(f => ({ ...f, ingredients: moveItem(f.ingredients, i, dir) })) } // Preference sets function addPrefSet() { setForm(f => ({ ...f, preference_sets: [...f.preference_sets, { name: '', default_choice_index: -1, choices: [], shared_subset: null, is_favorite: false, favorite_sort_order: 0, allow_multi_select: false, allow_choice_quantity: false }] })) setActiveTab(form.preference_sets.length) } function removePrefSet(si) { setForm(f => ({ ...f, preference_sets: f.preference_sets.filter((_, idx) => idx !== si) })) setActiveTab('favorites') } function setPrefSetField(si, k, v) { setForm(f => ({ ...f, preference_sets: f.preference_sets.map((ps, idx) => idx === si ? { ...ps, [k]: v } : ps) })) } function addChoice(si) { setForm(f => ({ ...f, preference_sets: f.preference_sets.map((ps, idx) => idx === si ? { ...ps, choices: [...ps.choices, { name: '', extra_cost: 0, disables_subset: false, is_compact: false, sub_choices: [] }] } : ps )})) } function removeChoice(si, ci) { setForm(f => ({ ...f, preference_sets: f.preference_sets.map((ps, idx) => { if (idx !== si) return ps const newChoices = ps.choices.filter((_, cidx) => cidx !== ci) const d = ps.default_choice_index const newDefault = d === ci ? -1 : d > ci ? d - 1 : d return { ...ps, choices: newChoices, default_choice_index: newDefault } })})) } function setChoice(si, ci, k, v) { setForm(f => ({ ...f, preference_sets: f.preference_sets.map((ps, idx) => idx === si ? { ...ps, choices: ps.choices.map((ch, cidx) => cidx === ci ? { ...ch, [k]: v } : ch) } : ps )})) } function moveChoice(si, ci, dir) { setForm(f => ({ ...f, preference_sets: f.preference_sets.map((ps, idx) => { if (idx !== si) return ps const newChoices = moveItem(ps.choices, ci, dir) const j = ci + dir let nd = ps.default_choice_index if (nd === ci) nd = j; else if (nd === j) nd = ci return { ...ps, choices: newChoices, default_choice_index: nd } })})) } function toggleDefaultChoice(si, ci) { setForm(f => ({ ...f, preference_sets: f.preference_sets.map((ps, idx) => idx !== si ? ps : { ...ps, default_choice_index: ps.default_choice_index === ci ? -1 : ci } )})) } function addSubChoice(si, ci) { setForm(f => ({ ...f, preference_sets: f.preference_sets.map((ps, pidx) => pidx !== si ? ps : { ...ps, choices: ps.choices.map((ch, cidx) => cidx !== ci ? ch : { ...ch, sub_choices: [...(ch.sub_choices || []), { name: '', extra_cost: 0, is_default: false, is_compact: false }] } )} )})) } function removeSubChoice(si, ci, sci) { setForm(f => ({ ...f, preference_sets: f.preference_sets.map((ps, pidx) => pidx !== si ? ps : { ...ps, choices: ps.choices.map((ch, cidx) => cidx !== ci ? ch : { ...ch, sub_choices: ch.sub_choices.filter((_, i) => i !== sci) } )} )})) } function setSubChoice(si, ci, sci, k, v) { setForm(f => ({ ...f, preference_sets: f.preference_sets.map((ps, pidx) => pidx !== si ? ps : { ...ps, choices: ps.choices.map((ch, cidx) => cidx !== ci ? ch : { ...ch, sub_choices: ch.sub_choices.map((sc, scidx) => { if (scidx !== sci) return k === 'is_default' && v === true ? { ...sc, is_default: false } : sc return { ...sc, [k]: v } })} )} )})) } function moveSubChoice(si, ci, sci, dir) { setForm(f => ({ ...f, preference_sets: f.preference_sets.map((ps, pidx) => pidx !== si ? ps : { ...ps, choices: ps.choices.map((ch, cidx) => cidx !== ci ? ch : { ...ch, sub_choices: moveItem(ch.sub_choices, sci, dir) } )} )})) } function setSharedSubsetName(si, name) { setForm(f => ({ ...f, preference_sets: f.preference_sets.map((ps, idx) => { if (idx !== si) return ps return { ...ps, shared_subset: ps.shared_subset ? { ...ps.shared_subset, name } : { name, choices: [] } } })})) } function addSharedSubsetChoice(si) { setForm(f => ({ ...f, preference_sets: f.preference_sets.map((ps, idx) => { if (idx !== si) return ps const ss = ps.shared_subset || { name: '', choices: [] } return { ...ps, shared_subset: { ...ss, choices: [...ss.choices, { name: '', extra_cost: 0, is_default: false }] } } })})) } function removeSharedSubsetChoice(si, sci) { setForm(f => ({ ...f, preference_sets: f.preference_sets.map((ps, idx) => { if (idx !== si) return ps const newChoices = ps.shared_subset.choices.filter((_, i) => i !== sci) return { ...ps, shared_subset: newChoices.length ? { ...ps.shared_subset, choices: newChoices } : null } })})) } function setSharedSubsetChoice(si, sci, k, v) { setForm(f => ({ ...f, preference_sets: f.preference_sets.map((ps, idx) => { if (idx !== si) return ps const newChoices = ps.shared_subset.choices.map((sc, scidx) => { if (scidx !== sci) return k === 'is_default' && v === true ? { ...sc, is_default: false } : sc return { ...sc, [k]: v } }) return { ...ps, shared_subset: { ...ps.shared_subset, choices: newChoices } } })})) } function moveSharedSubsetChoice(si, sci, dir) { setForm(f => ({ ...f, preference_sets: f.preference_sets.map((ps, idx) => idx !== si ? ps : { ...ps, shared_subset: { ...ps.shared_subset, choices: moveItem(ps.shared_subset.choices, sci, dir) } } )})) } // Modifier groups const _nextTempId = useRef(-1) function addModifierGroup(modifier_type) { const _tempId = _nextTempId.current-- setForm(f => ({ ...f, modifier_groups: [...f.modifier_groups, { _tempId, id: null, modifier_type, name: 'Νέα ομάδα', sort_order: f.modifier_groups.length }] })) } function renameModifierGroup(_tempId, name) { setForm(f => ({ ...f, modifier_groups: f.modifier_groups.map(g => g._tempId === _tempId ? { ...g, name } : g) })) } function deleteModifierGroup(_tempId) { setForm(f => { const next = { ...f, modifier_groups: f.modifier_groups.filter(g => g._tempId !== _tempId) } // Un-assign all items that belonged to this group next.options = f.options.map(o => o.group_id === _tempId ? { ...o, group_id: null } : o) next.ingredients = f.ingredients.map(i => i.group_id === _tempId ? { ...i, group_id: null } : i) next.preference_sets = f.preference_sets.map(ps => ps.group_id === _tempId ? { ...ps, group_id: null } : ps) return next }) } function setItemGroup(listKey, idx, _tempId) { setForm(f => ({ ...f, [listKey]: f[listKey].map((item, i) => i === idx ? { ...item, group_id: _tempId } : item) })) } const [collapsedGroups, setCollapsedGroups] = useState({}) function toggleGroupCollapsed(_tempId) { setCollapsedGroups(s => ({ ...s, [_tempId]: !s[_tempId] })) } function buildBody() { return { name: form.name, description: form.description || null, category_id: form.category_id ? Number(form.category_id) : null, base_price: form.base_price !== '' && form.base_price !== null && form.base_price !== undefined ? parseFloat(form.base_price) : 0, unit_type: form.unit_type || 'piece', is_available: form.is_available, lifecycle_status: form.lifecycle_status, prep_zone_ids: form.prep_zone_ids, quick_options: form.quick_options.map((q, i) => ({ name: q.name, price: parseFloat(q.price) || 0, allow_multiple: q.allow_multiple ?? false, sort_order: i, is_favorite: q.is_favorite ?? false, favorite_sort_order: q.favorite_sort_order ?? 0, is_compact: q.is_compact ?? false, })), modifier_groups: form.modifier_groups.map((g, gi) => ({ modifier_type: g.modifier_type, name: g.name, sort_order: gi })), options: form.options.map(o => ({ name: o.name, extra_cost: parseFloat(o.extra_cost) || 0, allow_multiple: o.allow_multiple ?? false, multi_select: o.multi_select ?? false, is_compact: o.is_compact ?? false, sub_choices: (o.sub_choices || []).map(s => ({ name: s.name, extra_cost: parseFloat(s.extra_cost) || 0, is_default: s.is_default ?? false, allow_multiple: s.allow_multiple ?? false, is_compact: s.is_compact ?? false })), is_favorite: o.is_favorite ?? false, favorite_sort_order: o.favorite_sort_order ?? 0, group_id: o.group_id != null ? form.modifier_groups.findIndex(g => g._tempId === o.group_id) : null, })), ingredients: form.ingredients.map(i => ({ name: i.name, extra_cost: parseFloat(i.extra_cost) || 0, is_favorite: i.is_favorite ?? false, favorite_sort_order: i.favorite_sort_order ?? 0, is_compact: i.is_compact ?? false, group_id: i.group_id != null ? form.modifier_groups.findIndex(g => g._tempId === i.group_id) : null, })), preference_sets: form.preference_sets.map(ps => ({ name: ps.name, default_choice_index: ps.default_choice_index >= 0 ? ps.default_choice_index : null, shared_subset: ps.shared_subset?.choices?.length ? { name: ps.shared_subset.name || '', choices: ps.shared_subset.choices.map(s => ({ name: s.name, extra_cost: parseFloat(s.extra_cost) || 0, is_default: s.is_default ?? false })), } : null, choices: ps.choices.map(c => ({ name: c.name, extra_cost: parseFloat(c.extra_cost) || 0, disables_subset: c.disables_subset ?? false, is_compact: c.is_compact ?? false, sub_choices: (c.sub_choices || []).map(s => ({ name: s.name, extra_cost: parseFloat(s.extra_cost) || 0, is_default: s.is_default ?? false, is_compact: s.is_compact ?? false })), })), is_favorite: ps.is_favorite ?? false, favorite_sort_order: ps.favorite_sort_order ?? 0, group_id: ps.group_id != null ? form.modifier_groups.findIndex(g => g._tempId === ps.group_id) : null, allow_multi_select: ps.allow_multi_select ?? false, allow_choice_quantity: ps.allow_choice_quantity ?? false, })), sold_at: form.sold_at, digital_available: form.digital_available, digital_name: form.digital_name || null, digital_description: form.digital_description || null, digital_image_url: form.digital_image_url || null, // Phase 2A — cost tracking cost_simple: form.cost_mode === 'simple' && form.cost_simple !== '' ? parseFloat(form.cost_simple) : null, cost_breakdown: form.cost_mode === 'detailed' && form.cost_breakdown.length > 0 ? form.cost_breakdown.map(e => ({ label: e.label, amount: parseFloat(e.amount) || 0 })) : null, // Quick Add quick_add_enabled: form.quick_add_enabled, // Tags tags: form.tags || [], // Service items is_service_item: form.is_service_item ?? false, // Fiscal printer (ΦΗΜ) fiscal_name: form.fiscal_name?.trim() || null, fiscal_vat_group_id: form.fiscal_vat_group_id ? parseInt(form.fiscal_vat_group_id, 10) : null, media_id: form.image?.id ?? null, } } function submit() { onSave(buildBody()) } const isNew = !product.id const canSave = form.name.trim() && (form.is_service_item || (form.base_price !== '' && form.base_price !== null && form.base_price !== undefined)) const favCount = buildFavoritesList(form).length const prefGroups = form.modifier_groups.filter(g => g.modifier_type === 'preference') const tabs = [ { key: 'favorites', label: 'Αγαπημένα', count: favCount, isFavTab: true }, { key: 'quick', label: 'Γρήγορες', count: form.quick_options.length }, { key: 'ingredients', label: 'Υλικά', count: form.ingredients.length }, { key: 'options', label: 'Έξτρα', count: form.options.length }, ...form.preference_sets.map((ps, i) => ({ key: i, label: ps.name || `Προτ. ${i + 1}`, count: ps.choices.length })), { key: '__add_pref__', label: '+ Προτίμηση', isAdd: true }, { key: '__add_pref_group__', label: '+ Ομάδα', isAddGroup: true }, ] const favList = buildFavoritesList(form) return (
{/* Header */}

{isNew ? 'Νέο προϊόν' : `Επεξεργασία — ${product.name}`}

{/* Body */}
{/* LEFT: tabbed product info — 25% normally, 100% for service items */}
{/* Left tab bar */}
{[ { key: 'info', label: 'Πληροφορίες' }, ...(!form.is_service_item ? [ { key: 'digital', label: 'Πού πωλείται' }, { key: 'cost', label: 'Κόστος' }, ...(fiscalEnabled ? [{ key: 'fiscal', label: 'Ταμειακή' }] : []), ] : []), ].map(t => ( ))}
{/* Left tab content */}
{/* Tab 1: Main Info */} {leftTab === 'info' && (<> {/* Service Item toggle */} {form.is_service_item && (

{form.base_price && parseFloat(form.base_price) > 0 ? 'Τυπώνεται σε ξεχωριστή ενότητα SERVICE. Καταγράφεται στα έσοδα.' : 'Τυπώνεται μία φορά — δεν αποθηκεύεται στην παραγγελία, δεν καταγράφεται στα έσοδα.'}

)}
setField('name', e.target.value)} autoFocus placeholder={form.is_service_item ? 'π.χ. Σερβίτσιο' : 'π.χ. Espresso'} />
{!form.is_service_item &&