import { useState } from 'react'
import OrderDrawer from './OrderDrawer'
function CategoriesIcon({ width = 20, height = 20 }) {
return (
)
}
function hexToRgba(hex, alpha) {
if (!hex) return null
const h = hex.replace('#', '')
const r = parseInt(h.substring(0, 2), 16)
const g = parseInt(h.substring(2, 4), 16)
const b = parseInt(h.substring(4, 6), 16)
return `rgba(${r},${g},${b},${alpha})`
}
function ProductGrid({ products, onOpen }) {
if (products.length === 0) return null
return (
{products.map(product => {
const initials = product.name
.trim()
.split(/\s+/)
.slice(0, 2)
.map(w => w[0])
.join('')
.toUpperCase()
return (
)
})}
)
}
// Builds the ordered list of sections for a top-level category:
// interleaves direct products (as a "General" section) and sub-categories
// according to general_sort_order and each sub's sort_order.
function buildSections(parent, subcategories, directProducts) {
const sections = []
if (directProducts.length > 0) {
sections.push({ _isGeneral: true, sort_order: parent.general_sort_order, products: directProducts })
}
for (const sub of subcategories) {
sections.push({ ...sub, _isGeneral: false, sort_order: sub.sort_order })
}
return sections.sort((a, b) => a.sort_order - b.sort_order)
}
export default function ProductPicker({ categories, products, onAdd, viewAllOpen, setViewAllOpen }) {
const topLevel = categories.filter(c => !c.parent_id).sort((a, b) => a.sort_order - b.sort_order)
const initialCatId = topLevel[0]?.id ?? null
const [activeCat, setActiveCat] = useState(initialCatId)
const [drawerProduct, setDrawerProduct] = useState(null)
// Track which sub-category sections are expanded (by sub-cat id or '__general__')
const [expandedSubs, setExpandedSubs] = useState(() => {
if (!initialCatId) return {}
const subs = categories.filter(c => c.parent_id === initialCatId)
const state = {}
subs.forEach(s => { if (s.auto_expanded) state[String(s.id)] = true })
return state
})
const activeParent = categories.find(c => c.id === activeCat)
const subcategories = activeParent
? categories.filter(c => c.parent_id === activeCat).sort((a, b) => a.sort_order - b.sort_order)
: []
const hasSubcats = subcategories.length > 0
// Products directly on this top-level category (no sub-cat)
const directProducts = products.filter(p => p.category_id === activeCat)
// Products for the flat view (no sub-cats)
const flatProducts = products.filter(p => p.category_id === activeCat)
// Build sections for accordion view
const sections = hasSubcats ? buildSections(activeParent, subcategories, directProducts) : []
function buildDefaultExpanded(catId) {
const subs = categories.filter(c => c.parent_id === catId)
const state = {}
subs.forEach(s => { if (s.auto_expanded) state[String(s.id)] = true })
return state
}
function selectCategory(id) {
setActiveCat(id)
setViewAllOpen(false)
setExpandedSubs(buildDefaultExpanded(id))
}
function toggleSub(key) {
setExpandedSubs(prev => ({ ...prev, [key]: !prev[key] }))
}
function openDrawer(product) { setDrawerProduct(product) }
function closeDrawer() { setDrawerProduct(null) }
return (
{topLevel.map(cat => {
const isActive = activeCat === cat.id
const bg = cat.color
? isActive ? cat.color : hexToRgba(cat.color, 0.35)
: isActive ? 'var(--accent)' : 'var(--bg3)'
const color = cat.color
? isActive ? '#fff' : 'rgba(255,255,255,0.65)'
: isActive ? 'var(--accent-fg)' : 'var(--muted)'
return (
)
})}
{/* Product area — flat grid or accordion depending on sub-cats */}
{!hasSubcats ? (
// No sub-categories: original flat grid
<>
{flatProducts.length === 0 && (
Δεν υπάρχουν προϊόντα
)}
>
) : (
// Has sub-categories: accordion view
{sections.map(section => {
const key = section._isGeneral ? '__general__' : String(section.id)
const isOpen = !!expandedSubs[key]
const sectionProducts = section._isGeneral
? section.products
: products.filter(p => p.category_id === section.id)
if (sectionProducts.length === 0) return null
// General products appear flat — no collapsible header
if (section._isGeneral) {
return (
)
}
const accentColor = section.color ?? activeParent?.color ?? null
return (
{isOpen && (
)}
)
})}
)}
{/* View All modal — top-level categories only */}
{viewAllOpen && (
setViewAllOpen(false)}>
e.stopPropagation()}
>
Κατηγορίες
{topLevel.map(cat => {
const isActive = activeCat === cat.id
const bg = cat.color || 'var(--bg3)'
const overlay = isActive ? 'rgba(255,255,255,0.18)' : 'rgba(0,0,0,0.35)'
return (
)
})}
)}
{ onAdd(item); closeDrawer() }}
/>
)
}