/* ============================================================
   views/avisos.js — Avisos / Anuncios
   ============================================================ */

function renderAvisos(el) {
  apiGet('/avisos', function(data) {
    var avisos = data || [];

    el.innerHTML =
      '<div class="page-header">' +
        '<div class="section-title" style="margin:0">Avisos</div>' +
        '<span style="font-size:13px;color:var(--text-muted)">' + avisos.length + ' aviso(s) activo(s)</span>' +
      '</div>';

    if (avisos.length === 0) {
      el.innerHTML += '<div class="empty-state" style="background:var(--white);border-radius:var(--radius);border:1px solid var(--border);padding:48px">' +
        '<div class="empty-icon">📢</div><h3>Sin avisos</h3><p>No hay avisos activos en este momento.</p>' +
      '</div>';
      return;
    }

    var html = '';
    for (var i = 0; i < avisos.length; i++) {
      var a = avisos[i];
      html += '<div class="aviso-card">' +
        '<div class="aviso-header">' +
          '<span style="font-size:20px">📢</span>' +
          '<span class="aviso-title">' + escHtml(a.titulo) + '</span>' +
          '<span class="aviso-date">' + formatDate(a.fechaInicio) + ' – ' + formatDate(a.fechaFin) + '</span>' +
        '</div>' +
        '<div class="aviso-body">' + escHtml(a.mensaje).replace(/\n/g, '<br>') + '</div>' +
      '</div>';
    }
    el.innerHTML += html;

    // Update topbar badge
    var badge = document.getElementById('topbarBadge');
    if (badge && avisos.length > 0) {
      badge.textContent = avisos.length;
      badge.style.display = 'inline-block';
    }

  }, function(err) {
    el.innerHTML = errorState('Error al cargar avisos', err);
  });
}
