// CRM → Customers (plan step 6.13b; replaces the v1 CustomersPage): the venue's regular
// customers. Search by name, nickname, phone, e-mail or notes (case and Greek accents ignored);
// each with visits, what they spent (counted as in the reports: refunds off, tab lines in) and
// their last visit. A tap opens their card: contact, notes (allergies, likes), their open tab,
// every visit. New, edit, remove (a removed customer stays on old orders). Live: `customers.changed`.
import { useDeferredValue, useState } from 'react'
import { useMutation } from '@tanstack/react-query'
import toast from 'react-hot-toast'
import { Pencil, Plus, Search, Trash2, Users } from 'lucide-react'
import { errorMessage } from '@xenia/api'
import { useFormat, useTranslation } from '@xenia/i18n'
import { Button, Dialog, EmptyState, IconButton, Input, Spinner } from '@xenia/ui'
import client from '../../api/client'
import { useLiveQuery } from '../../live/useLiveQuery'
import { Avatar } from '../overview/parts'
import { Panel, Table } from '../reports/kit/parts'
import CustomerCard from './CustomerCard'
import CustomerDialog from './CustomerDialog'
export default function CustomersPage() {
const { t } = useTranslation('manager')
const fmt = useFormat()
const [q, setQ] = useState('')
const search = useDeferredValue(q.trim())
const [dialog, setDialog] = useState(null) // { kind: 'new' | 'edit' | 'card' | 'remove', customer }
const { data, isPending, refetch } = useLiveQuery({
queryKey: ['customers', search], placeholderData: prev => prev,
queryFn: () => client.get('/api/customers/', { params: search ? { search } : {} }).then(r => r.data),
events: ['customers.changed', 'order_closed', 'order_paid'],
})
const rows = data ?? []
const close = () => setDialog(null)
const saved = () => { close(); refetch() }
const remove = useMutation({
mutationFn: c => client.delete(`/api/customers/${c.id}`),
onSuccess: (_r, c) => { toast.success(t('customers.removed', { name: c.name })); saved() },
onError: err => toast.error(errorMessage(err, t)),
})
const stop = fn => ev => { ev.stopPropagation(); fn() }
const columns = [
{ key: 'name', label: t('customers.name'), render: c => (
{t('customers.subtitle')}