window.addEventListener("scroll", () => {
  const header = document.querySelector(".header-container");

  if (window.scrollY > 50) {
    header.classList.add("scrolled");
  } else {
    header.classList.remove("scrolled");
  }
});

const openBtn = document.querySelector(".open-menu-btn");
const backdrop = document.querySelector(".backdrop");

if (openBtn && backdrop) {
  openBtn.addEventListener("click", () => {
    backdrop.classList.add("is-open");
  });

  document.querySelectorAll(".mob-menu a").forEach((link) => {
    link.addEventListener("click", () => {
      backdrop.classList.remove("is-open");
    });
  });
}

const counters = document.querySelectorAll(".counter");

let value = Number(localStorage.getItem("counterValue")) || 383;

function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}
function updateCounter() {
  const increment = getRandomInt(1, 3);
  value += increment;

  // if (value >= 1000) {
  //   value = 1000;

  //   counters.forEach((counter) => {
  //     counter.textContent = value;
  //   });

  //   localStorage.setItem("counterValue", value);
  //   return;
  // }

  counters.forEach((counter) => {
    counter.textContent = value;
  });
  localStorage.setItem("counterValue", value);

  const delay = getRandomInt(8000, 12000);
  setTimeout(updateCounter, delay);
}

updateCounter();

let endTime =
  Number(localStorage.getItem("endTime")) ||
  Date.now() + (1 * 24 * 60 * 60 + 16 * 60 * 60 + 51 * 60 + 3) * 1000;

localStorage.setItem("endTime", endTime);

const daysEls = document.querySelectorAll(".days");
const hoursEls = document.querySelectorAll(".hours");
const minutesEls = document.querySelectorAll(".minutes");
const secondsEls = document.querySelectorAll(".seconds");

function updateTimer() {
  let diff = Math.floor((endTime - Date.now()) / 1000);

  // if (diff < 0) diff = 0;

  if (diff <= 0) {
    // перезапуск таймера
    endTime =
      Date.now() + (1 * 24 * 60 * 60 + 16 * 60 * 60 + 51 * 60 + 3) * 1000;
    diff = Math.floor((endTime - Date.now()) / 1000);
  }

  const days = Math.floor(diff / (24 * 60 * 60));
  const hours = Math.floor((diff % (24 * 60 * 60)) / 3600);
  const minutes = Math.floor((diff % 3600) / 60);
  const seconds = diff % 60;

  daysEls.forEach((el) => {
    el.textContent = String(days).padStart(2, "0");
  });

  hoursEls.forEach((el) => {
    el.textContent = String(hours).padStart(2, "0");
  });

  minutesEls.forEach((el) => {
    el.textContent = String(minutes).padStart(2, "0");
  });

  secondsEls.forEach((el) => {
    el.textContent = String(seconds).padStart(2, "0");
  });
}

updateTimer();
setInterval(updateTimer, 1000);

document.querySelectorAll(".accordion-item").forEach((item) => {
  const header = item.querySelector(".accordion-header");

  header.addEventListener("click", () => {
    item.classList.toggle("active");
  });
});

const openBtns = document.querySelectorAll(".modal-btn");
const backmodal = document.querySelector(".backmodal");

openBtns.forEach((btn) => {
  btn.addEventListener("click", () => {
    if (backmodal) backmodal.classList.add("is-open");
  });
});

if (backmodal) {
  backmodal.addEventListener("click", (e) => {
    if (e.target === backmodal) {
      backmodal.classList.remove("is-open");
    }
  });
}

document.addEventListener("keydown", (e) => {
  if (e.key === "Escape" && backmodal) {
    backmodal.classList.remove("is-open");
  }
});

const steps = document.querySelectorAll(".step");
const progressItems = document.querySelectorAll(".progress-item");

let currentStep = 0;

function showStep(index) {
  steps.forEach((step) => {
    step.classList.remove("active");
  });

  progressItems.forEach((item) => {
    item.classList.remove("active");
  });

  steps[index].classList.add("active");
  progressItems[index].classList.add("active");
}

openBtns.forEach((btn) => {
  btn.addEventListener("click", () => {
    currentStep = 0;
    showStep(0);

    if (backmodal) backmodal.classList.add("is-open");
  });
});

document.addEventListener("click", (e) => {
  if (e.target.classList.contains("next-btn")) {
    if (currentStep < steps.length - 1) {
      currentStep++;
      showStep(currentStep);
    }
  }

  if (e.target.classList.contains("prev-btn")) {
    if (currentStep > 0) {
      currentStep--;
      showStep(currentStep);
    }
  }
});
