// enterprise.el -- Enterprise inquiry form submission and headcount filter.
// Compiled with: elc --target=js --bundle --minify --obfuscate
//
// Exposed globals: entHeadcountChange(val)

fn main() -> Void {
    native_js("(function() {
  var form       = document.getElementById('enterprise-form');
  var submitBtn  = document.getElementById('ent-submit');
  var successDiv = document.getElementById('enterprise-success');
  var errorDiv   = document.getElementById('ent-form-error');

  if (!form) return;

  window.entHeadcountChange = function(val) {
    var msgSecondary = document.getElementById('ent-filter-msg-secondary');
    var msgYes       = document.getElementById('ent-filter-msg-yes');
    if (msgSecondary) msgSecondary.style.display = val === 'secondary' ? 'block' : 'none';
    if (msgYes)       msgYes.style.display       = val === 'yes'       ? 'block' : 'none';
    if (submitBtn) {
      submitBtn.disabled     = val === 'yes';
      submitBtn.style.opacity = val === 'yes' ? '0.35' : '1';
      submitBtn.style.cursor  = val === 'yes' ? 'not-allowed' : 'pointer';
    }
  };

  form.addEventListener('submit', function(e) {
    e.preventDefault();
    var headcount = document.getElementById('ent-headcount').value;
    if (headcount === 'yes') {
      var msgYes = document.getElementById('ent-filter-msg-yes');
      if (msgYes) msgYes.style.display = 'block';
      return;
    }

    var name    = document.getElementById('ent-name').value.trim();
    var email   = document.getElementById('ent-email').value.trim();
    var company = document.getElementById('ent-company').value.trim();
    var size    = document.getElementById('ent-size').value;
    var useCase = document.getElementById('ent-use').value.trim();

    if (!name || !email || !company || !size || !useCase || !headcount) {
      if (errorDiv) { errorDiv.textContent = 'Please fill out all fields.'; errorDiv.style.display = 'block'; }
      return;
    }

    if (errorDiv) errorDiv.style.display = 'none';
    if (submitBtn) { submitBtn.textContent = 'Sending...'; submitBtn.disabled = true; }

    fetch('/api/enterprise-inquiry', {
      method: 'POST',
      headers: {'Content-Type': 'application/json'},
      body: JSON.stringify({ name: name, email: email, company: company, size: size, use_case: useCase, headcount: headcount })
    })
    .then(function(r) { return r.json(); })
    .then(function() {
      if (form)       form.style.display       = 'none';
      if (successDiv) successDiv.style.display = 'block';
    })
    .catch(function() {
      if (submitBtn) { submitBtn.textContent = 'Send inquiry →'; submitBtn.disabled = false; }
      if (errorDiv)  { errorDiv.textContent  = 'Something went wrong. Email enterprise@neurontechnologies.ai directly.'; errorDiv.style.display = 'block'; }
    });
  });
})()")
}
