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

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

  var user = requireLogin();
  if(!user) return;

  $("#helloTxt").textContent = "Welcome back, " + user.name;
  $("#logoutBtn").addEventListener("click", logout);

  /* DEVELOPER (PHP): replace with get_architects.php (AJAX or PHP loop) */
  var list = Store.getArchitects();

  /* ---------- stats ---------- */
  $("#statTotal").textContent   = list.length;
  $("#statInvited").textContent = list.filter(function(a){ return a.status === "invited"; }).length;
  $("#statActive").textContent  = list.filter(function(a){ return a.status === "active"; }).length;

  /* ---------- recent table + mobile cards ---------- */
  var rows  = $("#recentRows");
  var cards = $("#recentCards");
  var recent = list.slice(0, 5);

  if(recent.length === 0){
    rows.innerHTML =
      '<tr><td colspan="4" class="empty">No architects yet. Register your first architect to get started.</td></tr>';
    cards.innerHTML =
      '<div class="empty">No architects yet.<br><a class="btn btn-primary" href="register.html">Register your first architect</a></div>';
    return;
  }

  rows.innerHTML = recent.map(function(a){
    return '<tr>' +
      '<td><strong>' + escapeHtml(a.name) + '</strong></td>' +
      '<td>' + escapeHtml(a.mobile) + '</td>' +
      '<td>' + escapeHtml(a.city) + '</td>' +
      '<td><span class="pill ' + a.status + '">' + (a.status === "invited" ? "Invited" : "Active") + '</span></td>' +
    '</tr>';
  }).join("");

  cards.innerHTML = recent.map(function(a){
    return '<article class="arch-card">' +
      '<div class="ac-head">' +
        '<div><div class="ac-name">' + escapeHtml(a.name) + '</div>' +
        '<div class="ac-date">' + escapeHtml(a.date) + '</div></div>' +
        '<span class="pill ' + a.status + '">' + (a.status === "invited" ? "Invited" : "Active") + '</span>' +
      '</div>' +
      '<dl>' +
        '<dt>Contact</dt><dd>' + escapeHtml(a.mobile) + '</dd>' +
        '<dt>City</dt><dd>' + escapeHtml(a.city) + '</dd>' +
      '</dl>' +
    '</article>';
  }).join("");

});
