const t = window.t || (k => k); const SendFlow = () => { const {plans, setHistory, go, route, user, toast, history} = useApp(); const p = plans.find(x => x.id === route.params?.id); const [stage, setStage] = useState('credentials'); const [progress, setProgress] = useState(0); const [result, setResult] = useState(null); const [icaroUser, setIcaroUser] = useState(''); const [icaroPass, setIcaroPass] = useState(''); const [submitting, setSubmitting] = useState(false); const [errorMsg, setErrorMsg] = useState(''); const [showPass, setShowPass] = useState(false); const doSubmit = async () => { if (!icaroUser.trim() || !icaroPass.trim()) { setErrorMsg(t('send.fill_creds')); return; } const numId = parseInt(p.id, 10); if (isNaN(numId) || numId <= 0) { setErrorMsg(t('send.not_saved')); return; } setErrorMsg(''); setStage('sending'); setProgress(0); setSubmitting(true); // Progress animation let tick = 0; const iv = setInterval(() => { tick += 2; setProgress(Math.min(tick, 95)); }, 100); try { const res = await api.submitFpl(p.id, icaroUser.trim().toUpperCase(), icaroPass.trim().toUpperCase()); clearInterval(iv); setProgress(100); setTimeout(() => { setResult({ok: true, ref: res.ref, raw: res.raw}); setStage('done'); setSubmitting(false); setHistory(prev => [{ id: 'sub_' + (res.submission_id || Date.now()), planId: p.id, planName: p.name || (p.adep + ' - ' + p.ades), from: p.adep, to: p.ades, when: new Date().toISOString(), status: 'ok', fpId: res.ref, error: null, durationMs: 0, // Identificacion arcid: p.arcid || '', rules: p.flight_rules || '', flightType: p.flight_type || '', flightDate: p.flight_date || '', // Aeronave aircraft: p.aircraft_type || '', wake: p.wake || '', reg: p.reg || '', comEquipment: p.com_equipment || '', ssr: p.ssr || '', // Ruta eobt: p.eobt || '', speed: p.speed || '', fl: p.level || '', route: p.route || '', dur: p.eet_total || '', durMin: 0, altn: p.altn || '', altn2: p.altn2 || '', // Suplementario endurance: p.endurance || '', pob: String(p.persons_on_board||''), pic: p.pic_name || '', colors: p.colors || '', emergencyRadio: p.emergency_radio || '', survivalEquipment: p.survival_equipment || '', lifeJackets: p.life_jacket_type || '', // Observaciones rmk: p.rmk || '', opr: p.opr || '', }, ...prev]); }, 400); } catch (e) { clearInterval(iv); setProgress(0); console.error('=== ENAIRE SUBMIT ERROR ===', JSON.stringify(e, null, 2)); const errMap = {trial_date_limit: t('send.trial_date_limit') || 'En periodo de prueba solo puedes programar vuelos en los próximos 7 días.', free_limit_reached: t('send.free_limit') || 'Has alcanzado el límite de envíos gratuitos.'}; setResult({ok: false, err: errMap[e.error] || e.error || 'Error desconocido', detail: e.detail, debug: e.debug, raw: e.raw}); setStage('error'); setSubmitting(false); } }; if (!p) return
; // Stage: CREDENTIALS — pedir usuario y contrasena ENAIRE if (stage === 'credentials') return

{t('send.title')}

{/* Route header — full width */}
{p.adep || p.from}
{AIRPORTS[p.adep || p.from] || ''}
{p.ades || p.to}
{AIRPORTS[p.ades || p.to] || ''}
{RULES_FULL[p.flight_rules || p.rules] || p.flight_rules || '—'} {(p.level || p.fl) && {p.level || p.fl}} {(p.aircraft_type || p.aircraft) && {p.aircraft_type || p.aircraft}} {(p.eet_total) && {p.eet_total}} {(p.flight_date) && {p.flight_date}}
{/* Grid de secciones — 2 col desktop, 1 col movil */}
{/* Identificacion */}
{t('sec.identification')}
{(p.arcid) &&
{t('field.arcid')}{p.arcid}
}
{t('field.rules')}{RULES_FULL[p.flight_rules || p.rules] || p.flight_rules || p.rules || '—'}
{(p.flight_type) &&
{t('field.flight_type')}{({S:t('ftype.S'),N:t('ftype.N'),G:t('ftype.G'),M:t('ftype.M'),X:t('ftype.X')})[p.flight_type] || p.flight_type}
} {(p.flight_date) &&
{t('field.date')}{p.flight_date}
}
{/* Aeronave */}
{t('sec.aircraft')}
{(p.aircraft_type || p.aircraft) &&
{t('field.type')}{p.aircraft_type || p.aircraft}
} {(p.wake) &&
{t('field.wake')}{p.wake}
} {(p.reg) &&
{t('field.reg')}{p.reg}
} {(p.com_equipment) &&
{t('field.com_equipment')}{p.com_equipment}
} {(p.ssr) &&
{t('field.ssr')}{p.ssr}
}
{/* Ruta */}
{t('sec.route')}
{t('field.eobt')}{p.eobt || (p.obt ? p.obt.h + ':' + p.obt.m : '—')}Z
{(p.speed) &&
{t('field.speed')}{p.speed}
} {(p.level || p.fl) &&
{t('field.level')}{p.level || p.fl}
}
{t('field.route')}{p.route || 'DCT'}
{t('field.eet')}{p.eet_total || (p.dur ? p.dur.h + ':' + p.dur.m : '—')}
{(p.altn) &&
{t('field.altn')}{p.altn}
} {(p.altn2) &&
{t('field.altn2')}{p.altn2}
}
{/* Suplementario */} {(p.endurance || p.persons_on_board || p.pic_name || p.colors || p.emergency_radio || p.survival_equipment) &&
{t('sec.supplementary')}
{(p.endurance) &&
{t('field.endurance')}{p.endurance}
} {(p.persons_on_board) &&
{t('field.pob')}{p.persons_on_board}
} {(p.pic_name) &&
{t('field.pic')}{p.pic_name}
} {(p.colors) &&
{t('field.colors')}{p.colors}
} {(p.emergency_radio) &&
{t('field.emergency_radio')}{p.emergency_radio}
} {(p.survival_equipment) &&
{t('field.survival')}{p.survival_equipment}
} {(p.life_jacket_type) &&
{t('field.jackets')}{p.life_jacket_type}
} {(p.dinghies_number) &&
Balsas{p.dinghies_number}{p.dinghies_capacity ? ' x'+p.dinghies_capacity : ''}{p.dinghies_covered ? ' (cubierta)' : ''}{p.dinghies_color ? ' '+p.dinghies_color : ''}
}
} {/* Observaciones */} {(p.rmk || p.opr || p.sts) &&
{t('sec.remarks')}
{(p.rmk) &&
{t('field.rmk')}{p.rmk}
} {(p.opr) &&
{t('field.opr')}{p.opr}
} {(p.sts) &&
{t('field.sts')}{p.sts}
}
}
{/* Credenciales ENAIRE */}

{t('send.creds_title')}

{t('send.creds_subtitle')}
setIcaroUser(e.target.value.toUpperCase())} placeholder="Ej: EFLEMU" autoComplete="off"/>
setIcaroPass(e.target.value)} placeholder="Tu contraseña de Ícaro" autoComplete="off" style={{paddingRight: 42}}/>
{errorMsg &&
{errorMsg}
}
; // Stage: SENDING if (stage === 'sending') return

{t('send.sending')}

{p.adep || p.from} -> {p.ades || p.to} · {RULES_FULL[p.flight_rules || p.rules] || ''}

{progress}%
; // Stage: DONE if (stage === 'done') { const isFree = user.plan === 'free'; const monthSent = history.filter(h => h.status === 'ok' && new Date(h.when).getMonth() === new Date().getMonth()).length; return

{t('send.accepted')}

{t('send.confirmed')}

{t('send.ref')}
{result.ref || '—'}
{/* Usage counter */}
{isFree ? <> = 3 ? 'var(--err)' : 'var(--warn)'}}>{monthSent}/3 {t('send.monthly_sends') || 'envíos este mes'} {monthSent >= 3 && Límite} : <> {monthSent} {t('send.monthly_sends') || 'envíos este mes'} · ∞ ilimitados }
; } // Stage: ERROR return

{t('send.failed')}

{result?.err}

{result?.detail &&

{result.detail}

} {result?.debug &&
Debug
{result.debug}
}
; }; window.SendFlow = SendFlow;