/* =============================================================
   File: assets/js/login.js   —  used by index.html only
   ============================================================= */

document.addEventListener("DOMContentLoaded", function(){

  var form = $("#loginForm");

  form.addEventListener("submit", function(e){
    e.preventDefault();

    var email = $("#aid_email").value.trim();
    var pass  = $("#aid_password").value.trim();

    var ok = true;
    ok = Validate.mark("f-email", Validate.email(email)) && ok;
    ok = Validate.mark("f-pass",  Validate.filled(pass))  && ok;
    if(!ok){ toast("Please check your email ID and password"); return; }

    /* ---------------------------------------------------------
       DEVELOPER (PHP): replace this block with a call to login.php

       fetch("login.php", { method:"POST", body:new FormData(form) })
         .then(r => r.json())
         .then(res => {
            if(res.success) window.location.href = "dashboard.html";
            else toast(res.message);
         });
    --------------------------------------------------------- */
    var displayName = email.split("@")[0]
      .replace(/[._-]/g, " ")
      .replace(/\b\w/g, function(c){ return c.toUpperCase(); });

    Store.setUser({ email: email, name: displayName });
    window.location.href = "dashboard.html";
  });

  $("#linkForgot").addEventListener("click", function(e){
    e.preventDefault();
    toast("Password reset will be handled by the backend");
  });

  $("#linkHelp").addEventListener("click", function(e){
    e.preventDefault();
    toast("Please contact your Lauritz Knudsen representative for support");
  });

});
