const t = window.t || (k => k); const Dashboard = () => { const {plans, setPlans, go, user, toast, history, confirm} = useApp(); const [q, setQ] = useState(''); // Compatibilidad: planes API usan adep/ades/flight_rules/level/eet_total, seed usa from/to/rules/fl/dur const pFrom = (p) => p.adep || p.from || ''; const pTo = (p) => p.ades || p.to || ''; const pRules = (p) => p.flight_rules || p.rules || ''; const pLevel = (p) => p.level || p.fl || ''; const pEet = (p) => { if (p.eet_total) return p.eet_total; if (p.dur) return (p.dur.h || '00') + ':' + (p.dur.m || '00'); return ''; }; const pDate = (p) => { if (p.updated_at) return new Date(p.updated_at * 1000); if (p.lastUsed) return new Date(p.lastUsed); return new Date(); }; const filtered = plans.filter(p => !q || (p.name||'').toLowerCase().includes(q.toLowerCase()) || pFrom(p).includes(q.toUpperCase()) || pTo(p).includes(q.toUpperCase())); const sorted = [...filtered].sort((a,b) => (b.favorite?1:0)-(a.favorite?1:0) || pDate(b)-pDate(a)); const toggleFav = async (id) => { const plan = plans.find(x => x.id === id); if (!plan) return; const newFav = !plan.favorite; setPlans(p => p.map(x => x.id === id ? {...x, favorite: newFav ? 1 : 0} : x)); try { await api.favoritePlan(id, newFav); } catch(e) {} }; const duplicate = async (p) => { try { const res = await api.duplicatePlan(p.id); if (res && res.id) { const np = {...p, id: res.id, name: (p.name||'Plan') + ' (copia)', favorite: 0}; setPlans(x => [np, ...x]); toast(t('dash.plan_duplicated')); } } catch(e) { // Fallback local const np = {...p, id: 'pl_' + Math.random().toString(36).slice(2,8), name: (p.name||'Plan') + ' (copia)', favorite: false}; setPlans(x => [np, ...x]); toast(t('dash.plan_duplicated')); } }; const remove = async (id) => { const plan = plans.find(x => x.id === id); const ok = await confirm(t('dash.delete_confirm'), {title: plan?.name || t('dash.delete_plan'), yes: t('dash.delete'), no: t('dash.cancel')}); if (!ok) return; setPlans(p => p.filter(x => x.id !== id)); try { await api.deletePlan(id); } catch(e) {} toast(t('dash.plan_deleted')); }; const monthSent = history.filter(h => h.status === 'ok' && new Date(h.when).getMonth() === new Date().getMonth()).length; return
{plans.length}{user.plan==='free'?'/3':''} {t('dash.templates')} ยท {monthSent}{user.plan==='free'?'/3':''} {t('dash.sends_month')}