/* login.jsx — acceso individual del producto. */
(function () {
  window.CR = window.CR || {};
  window.CR.screens = window.CR.screens || {};
  var React = window.React;
  var h = React.createElement;
  var useState = React.useState;

  function Login(props) {
    var emailState = useState(''); var email = emailState[0], setEmail = emailState[1];
    var passwordState = useState(''); var password = passwordState[0], setPassword = passwordState[1];
    var busyState = useState(false); var busy = busyState[0], setBusy = busyState[1];
    var errorState = useState(props.error || ''); var error = errorState[0], setError = errorState[1];

    function submit(event) {
      event.preventDefault();
      if (busy) return;
      setBusy(true); setError('');
      window.CR.backend.login(email, password).then(props.onLogin).catch(function (err) {
        setError(err.status === 429 ? 'Demasiados intentos. Esperá unos minutos.' : 'Email o contraseña incorrectos.');
      }).finally(function () { setBusy(false); });
    }

    return h('main', { className: 'login-shell' },
      h('form', { className: 'card login-card', onSubmit: submit },
        h('img', { className: 'login-mark', src: props.modulo === 'last-mile' ? 'assets/icons/app/last-mile.svg' : 'assets/icons/app/craneo-rutas.svg', alt: '', width: 52, height: 52 }),
        h('a', { href: '#/', className: 'muted' }, '← Elegir módulo'),
        h('h1', null, props.modulo === 'last-mile' ? 'Ingresá a Last Mile' : 'Ingresá a Cráneo Rutas'),
        h('p', { className: 'muted' }, 'Usá tu cuenta individual de logística.'),
        error ? h('div', { className: 'alerta alerta--alta', role: 'alert' }, error) : null,
        h('label', null, h('span', null, 'Email'), h('input', { className: 'input', type: 'email', autoComplete: 'username', required: true, value: email, onChange: function (e) { setEmail(e.target.value); } })),
        h('label', null, h('span', null, 'Contraseña'), h('input', { className: 'input', type: 'password', autoComplete: 'current-password', required: true, value: password, onChange: function (e) { setPassword(e.target.value); } })),
        h('button', { className: 'btn btn--primary', type: 'submit', disabled: busy }, busy ? 'Ingresando…' : 'Ingresar')));
  }

  window.CR.screens.Login = Login;
})();
