// === Escena 05 — Por partes: varias ventas sobre un presupuesto, hasta «Completado» ===
// DataFlowPresupuestoVenta.CalcularAgregables: por cada línea, unidades del presupuesto menos las ya
// vendidas (por ReferenciaElementoDePresupuestoDeVenta). Agregar venta solo carga lo que queda, y solo
// se ve si queda algo. Ventas pasa a «Completado» cuando no queda nada y ya hay ventas.
// La lista de ventas es TablaHistorialVentasDePresupuestoU: número, fecha, «N uds», importe, abrir.

function EscenaPV05({ t }){
  useVoz(4);
  const G = GUION[4];
  const T = {
    nada:B(G,'No pasa nada'),
    quitas:B(G,'quitas lo que no va'),
    bajas:B(G,'bajas las unidades'),
    guardas:B(G,'la guardas'),
    vuelves:B(G,'Si vuelves al flujo'),
    lista:B(G,'esa venta en la lista'),
    sigue:B(G,'Agregar venta sigue'),
    carga:B(G,'carga solo lo que falta'),
    varias:B(G,'varias ventas'),
    completado:B(G,'Cuando ya no queda'),
    material:B(G,'¿Y si antes'),
    pedido:B(G,'toca un pedido'),
  };
  const pulso = (t0, d = 1.2) => (t >= t0 && t < t0 + d) ? Math.sin((t - t0) / d * Math.PI) : 0;
  const quitada = pr(t, T.quitas + 0.4, 0.6);
  const lineas = [LINEAS[0], LINEAS[1], { ...LINEAS[2], tachar: t >= T.quitas + 0.4, op: 1 - quitada * 0.7 }];
  const eVenta = pr(t, 0.1, 0.8) * (1 - pr(t, T.vuelves, 0.6));
  const eDlg = pr(t, T.vuelves + 0.2, 0.7) * (1 - pr(t, T.material, 0.8) * 0.65);
  const ePrev = pr(t, T.carga, 0.6) * (1 - pr(t, T.varias + 0.6, 0.5));
  const ventas = [
    { num:'F-2026-0142', fecha:'09/10/2026', uds:'2', imp:'1.089,00 €', e:pr(t, T.lista, 0.6) },
  ].concat(t >= T.varias ? [{ num:'F-2026-0157', fecha:'14/10/2026', uds:'1', imp:'121,00 €', e:pr(t, T.varias, 0.6) }] : []);
  const completo = t >= T.completado + 0.6;
  return (
    <SlideCanvas variant="light-soft" bg="#F8FAFC">
      <DemoHeader t={t} eyebrow="4 · POR PARTES" title="Un presupuesto, varias ventas"/>
      <VentanaTPV x={260} y={190} w={1400} h={780} e={eVenta} titulo="Venta / Ingreso" esVenta numero="" fecha="09/10/2026"
        origen={{ num:'P-2026-0031' }} lineas={lineas} focoCol={t >= T.bajas && t < T.guardas ? 'u' : null}
        total={t >= T.quitas + 0.8 ? '1.089,00 €' : '1.210,00 €'} panel={<PanelAgregar/>} focoAceptar={pulso(T.guardas)}/>
      <FlujoPresupuesto e={eDlg} x={400} y={210} w={1120} h={640} ventas={ventas} ePend={1 - pr(t, T.completado + 0.6, 0.3)}
        completado={completo ? pr(t, T.completado + 0.6, 0.6) : 0} agregable={!completo} focoBoton={pulso(T.sigue, 1.4)}/>
      {ePrev > 0 && (
        <div style={{ position:'absolute', left:1240, top:620, width:560, zIndex:40, opacity:ePrev, transform:`translateY(${(1 - ePrev) * 18}px)`,
          background:'#fff', border:`1px solid ${P.borde}`, borderRadius:12, boxShadow:'0 24px 60px rgba(15,23,42,0.18)', padding:'16px 20px' }}>
          <div style={{ display:'flex', alignItems:'center', gap:8 }}><Ico n="point_of_sale" s={22} c={P.gris}/><R s={17} w={500} c={P.gris}>Venta / Ingreso</R></div>
          <div style={{ display:'flex', marginTop:12, padding:'10px 4px', borderTop:`1px solid ${P.borde}` }}>
            <R s={19} w={500} style={{ flex:1 }}>Motor con mando</R><R s={19} style={{ width:70, textAlign:'right' }}>1</R><R s={19} w={600} style={{ width:110, textAlign:'right' }}>100,00</R>
          </div>
          <R s={15} c={P.gris}>Solo lo que falta por facturar</R>
        </div>
      )}
      <Cursor t={t} desde={T.quitas - 0.8}
        puntos={[[T.quitas - 0.8, 1100, 760], [T.quitas, 760, 476], [T.bajas, 760, 476], [T.bajas + 0.6, 1010, 420], [T.guardas, 1585, 938], [T.vuelves + 0.3, 1585, 938], [T.sigue, 962, 650], [T.material, 962, 650], [T.pedido, 1500, 1130]]}
        clics={[T.quitas, T.guardas, T.sigue + 0.6]}/>
      <Rotulo t={t} desde={T.pedido - 0.3}>Si antes hay que prepararlo: <span style={{ color:P.azul }}>pedido.</span></Rotulo>
    </SlideCanvas>
  );
}
Object.assign(window, { EscenaPV05 });
