// ==========================
// MODALES LOGIN / REGISTER
// ==========================

function showLoginModal(role = "") {
  const modal = document.getElementById("loginModal")

  // AHORA FUNCIONA CON TU CSS (.modal.show)
  modal.classList.add("show")
  document.body.style.overflow = "hidden"

  if (role) {
    document.getElementById("userType").value = role
  }
}

function closeLoginModal() {
  const modal = document.getElementById("loginModal")
  modal.classList.remove("show")
  document.body.style.overflow = "auto"
}

// ==========================
// LOGIN
// ==========================

function handleLogin(event) {
  event.preventDefault()

  const userType = document.getElementById("userType").value
  const username = document.getElementById("username").value
  const password = document.getElementById("password").value

  console.log("[v0] Login attempt:", { userType, username })

  sessionStorage.setItem("userType", userType)
  sessionStorage.setItem("username", username)

  if (userType === "admin" || userType === "comite") {
    window.location.href = "administracion.html"
  } else if (userType === "estudiante" || userType === "aspirante") {
    window.location.href = "student.html"
  }
}

function showRecoveryModal() {
  alert("Funcionalidad de recuperación de contraseña - Por implementar")
  closeLoginModal()
}

// ==========================
// REGISTER
// ==========================

function showRegisterModal() {
  closeLoginModal()
  const modal = document.getElementById("registerModal")

  // AHORA FUNCIONA CON TU CSS (.modal.show)
  modal.classList.add("show")
  document.body.style.overflow = "hidden"
}

function closeRegisterModal() {
  const modal = document.getElementById("registerModal")
  modal.classList.remove("show")
  document.body.style.overflow = "auto"
}

function handleRegister(event) {
  event.preventDefault()

  const userType = document.getElementById("registerUserType").value
  const fullName = document.getElementById("registerFullName").value
  const email = document.getElementById("registerEmail").value
  const password = document.getElementById("registerPassword").value
  const passwordConfirm = document.getElementById("registerPasswordConfirm").value
  const termsAccepted = document.getElementById("registerTerms").checked

  if (!userType || !fullName || !email || !password || !passwordConfirm) {
    alert("Por favor completa todos los campos obligatorios")
    return
  }

  if (password !== passwordConfirm) {
    alert("Las contraseñas no coinciden")
    return
  }

  if (!termsAccepted) {
    alert("Debes aceptar los términos y condiciones")
    return
  }

  alert("¡Registro exitoso! Ahora puedes iniciar sesión")
  closeRegisterModal()

  setTimeout(() => {
    showLoginModal()
    document.getElementById("username").value = email
  }, 300)
}

// ==========================
// SCROLL SECTIONS
// ==========================

function scrollToSection(sectionId) {
  const section = document.getElementById(sectionId)
  if (section) {
    section.scrollIntoView({ behavior: "smooth" })
  }
}

// ==========================
// CERRAR MODAL HACIENDO CLICK FUERA
// ==========================

window.onclick = (event) => {
  const login = document.getElementById("loginModal")
  const register = document.getElementById("registerModal")

  // Cerrar login
  if (event.target === login) {
    closeLoginModal()
  }

  // Cerrar register
  if (event.target === register) {
    closeRegisterModal()
  }
}

// ==========================
// HEADER SCROLL
// ==========================

window.addEventListener("scroll", () => {
  const header = document.querySelector(".landing-header")
  if (window.scrollY > 50) {
    header.classList.add("scrolled")
  } else {
    header.classList.remove("scrolled")
  }
})

// ==========================
// ROTADOR DE IMÁGENES HERO
// ==========================

let currentImg = 0;
const imgs = document.querySelectorAll(".hero-gallery .gallery-img");

if (imgs.length > 0) {
  setInterval(() => {
    imgs[currentImg].classList.remove("active");
    currentImg = (currentImg + 1) % imgs.length;
    imgs[currentImg].classList.add("active");
  }, 5000);
}
