import { useState } from 'react'; import { ActivityIndicator, Alert, Pressable, View } from 'react-native'; import { router } from 'expo-router'; import { useStore } from '../../../src/session'; import { useCard } from '../../../src/useCard'; import { api, CardInfo } from '../../../src/api'; import { describe, ErrorScreen } from '../../../src/errors'; import { Banner, Button, Card, Screen, T, unit } from '../../../src/ui'; import { font, useColors } from '../../../src/theme'; import { Icon } from '../../../src/icons'; function ago(iso: string) { const min = Math.round((Date.now() - new Date(iso).getTime()) / 60000); if (min < 1) return "à l'instant"; if (min < 60) return `il y a ${min} min`; const h = Math.round(min / 60); if (h < 24) return `il y a ${h} h`; return `le ${new Date(iso).toLocaleDateString('fr-FR', { day: 'numeric', month: 'long' })}`; } /** Fiche client après scan (maquette 4). */ export default function ClientCard() { const c = useColors(); const store = useStore(); const { serial, card, setCard, error, reload } = useCard(); const [giving, setGiving] = useState(null); if (error) return ; if (!card) return ; const mode = store.design.pointsMode; const reachable = store.design.rewards.filter((r) => r.cost <= card.balance).length; const SOURCE = { BIRTHDAY: 'Anniversaire', REFERRAL: 'Parrainage', REFERRAL_FRIEND: 'Bienvenue (parrainage)' } as const; const give = (g: CardInfo['gifts'][number]) => Alert.alert('Remettre le cadeau ?', `« ${g.label} » à ${card.firstName}`, [ { text: 'Annuler', style: 'cancel' }, { text: 'Remis', onPress: async () => { setGiving(g.id); try { setCard(await api.post(`/api/staff/cards/${encodeURIComponent(serial)}/gifts/${g.id}/use`)); } catch (e) { Alert.alert('Impossible', describe(e).detail); } finally { setGiving(null); } }, }, ]); // Prochain palier : la première récompense pas encore atteinte (sinon la plus chère). const sortedRewards = [...store.design.rewards].sort((a, b) => a.cost - b.cost); const target = sortedRewards.find((r) => r.cost > card.balance) ?? sortedRewards[sortedRewards.length - 1]; const progress = target ? Math.min(card.balance / target.cost, 1) : 0; const fg = store.design.foregroundColor; const memberSince = new Date(card.memberSince).toLocaleDateString('fr-FR', { month: 'short', year: 'numeric' }); return ( {/* En-tête : initiale, prénom, ancienneté */} {card.firstName.charAt(0).toUpperCase()} {card.firstName} {card.lastCreditAt ? `Dernière visite ${ago(card.lastCreditAt)}` : 'Première visite'} · client depuis {memberSince} {/* Carte aux couleurs du magasin : solde et progression */} {store.design.programName.toUpperCase()} {card.balance} {unit(mode, card.balance)} {target && ( {reachable > 0 ? reachable === 1 ? '1 récompense disponible' : `${reachable} récompenses disponibles` : `Encore ${target.cost - card.balance} ${unit(mode, target.cost - card.balance)} avant « ${target.label} »`} )} {card.nextCreditAt && ( Déjà créditée récemment : nouveau crédit possible à{' '} {new Date(card.nextCreditAt).toLocaleTimeString('fr-FR', { hour: '2-digit', minute: '2-digit' })}. )} {/* Cadeaux en nature à remettre en caisse */} {card.gifts?.length > 0 && ( Cadeaux à remettre {card.gifts.map((g) => ( {g.label} {SOURCE[g.source]} · {new Date(g.createdAt).toLocaleDateString('fr-FR', { day: 'numeric', month: 'short' })} {giving === g.id ? ( ) : ( give(g)}> Remettre )} ))} )}