﻿// IncomeWidget.jsx — Dark navy income calculator widget

Object.assign(window, { IncomeWidget });

function IncomeWidget() {
  const [period, setPeriod] = React.useState(0);
  const [investment, setInvestment] = React.useState(250);

  const MIN = 250;
  const MAX = 50000;
  const STEP = 250;

  const periods = ["1 Mes", "3 Meses", "6 Meses", "12 Meses"];
  const multipliers = [120, 360, 720, 1440];
  const earned = investment * multipliers[period];
  const pct = multipliers[period] * 100;
  const sliderPct = ((investment - MIN) / (MAX - MIN)) * 100;

  return (
    <div style={{ background: "#0f172a", borderRadius: 16, boxShadow: "0px 10px 30px rgba(0,0,0,0.5)", padding: "28px 20px", width: "100%", fontFamily: "'Inter', sans-serif" }}>
      <style>{`
        .income-slider {
          -webkit-appearance: none;
          appearance: none;
          width: 100%;
          height: 6px;
          border-radius: 3px;
          outline: none;
          cursor: pointer;
          border: none;
          display: block;
        }
        .income-slider::-webkit-slider-thumb {
          -webkit-appearance: none;
          appearance: none;
          width: 20px;
          height: 20px;
          border-radius: 50%;
          background: #3b82f6;
          border: 4px solid #60a5fa;
          box-shadow: 0 0 10px rgba(59,130,246,0.6);
          cursor: pointer;
          margin-top: -7px;
        }
        .income-slider::-webkit-slider-runnable-track {
          height: 6px;
          border-radius: 3px;
        }
        .income-slider::-moz-range-thumb {
          width: 20px;
          height: 20px;
          border-radius: 50%;
          background: #3b82f6;
          border: 4px solid #60a5fa;
          box-shadow: 0 0 10px rgba(59,130,246,0.6);
          cursor: pointer;
        }
        .income-slider::-moz-range-track {
          height: 6px;
          border-radius: 3px;
          background: transparent;
        }
        @media (max-width: 600px) {
          .inc-period-btn { padding: 6px 8px !important; font-size: 11px !important; }
        }
      `}</style>

      <div className="inc-heading" style={{ fontWeight: 700, color: "#60a5fa", textAlign: "center", marginBottom: 12 }}>Tus ingresos</div>
      <div className="inc-earned" style={{ fontWeight: 700, color: "#fff", textAlign: "center", letterSpacing: 1, marginBottom: 14 }}>
        €{earned.toLocaleString("es-ES")}
      </div>

      <div style={{ marginBottom: 20 }}>
        <input
          type="range"
          min={MIN}
          max={MAX}
          step={STEP}
          value={investment}
          onChange={(e) => setInvestment(Number(e.target.value))}
          className="income-slider"
          style={{ background: `linear-gradient(90deg, #2596fa ${sliderPct}%, #1e293b ${sliderPct}%)` }}
        />
      </div>

      <div className="inc-label" style={{ color: "#94a3b8", textAlign: "center", marginBottom: 4 }}>Cantidad de inversión</div>
      <div className="inc-invest" style={{ fontWeight: 700, color: "#fff", textAlign: "center", marginBottom: 16 }}>
        €{investment.toLocaleString("es-ES")}
      </div>

      <div className="inc-label" style={{ color: "#94a3b8", textAlign: "center", marginBottom: 8 }}>Periodo de inversión</div>
      <div style={{ display: "flex", gap: 6, justifyContent: "center", flexWrap: "wrap", marginBottom: 16 }}>
        {periods.map((p, i) => (
          <button
            key={i}
            className="inc-period-btn"
            onClick={() => setPeriod(i)}
            style={{ background: period === i ? "#2596fa" : "#1e293b", color: period === i ? "#fff" : "#94a3b8", border: period === i ? "1px solid #2596fa" : "1px solid #334155", borderRadius: 8, padding: "6px 12px", fontSize: 12, cursor: "pointer", fontFamily: "'Inter', sans-serif", transition: "all 0.15s" }}
          >
            {p}
          </button>
        ))}
      </div>

      <div className="inc-label" style={{ color: "#94a3b8", textAlign: "center", marginBottom: 6 }}>Porcentaje de ingresos</div>
      <div className="inc-pct" style={{ fontWeight: 700, color: "#45be2f", textAlign: "center" }}>+{pct.toLocaleString("es-ES")}%</div>
    </div>
  );
}
