<template>
    <main>
      <section class="visual--section">
        <div class="img--wrap">
          <picture>
            <img src="/img/company/img--about1.jpg" alt="" />
          </picture>
        </div>
        <div class="title--wrap color--white">
          <h2>Company</h2>
        </div>
      </section>
      <div class="detail--container">
        <section>
          <div class="desc--section full--type">
            <h3 class="big--title">EVENT</h3>
          </div>
        </section>
        <div class="content--wrap">
          <div class="event--wrap">
            <!-- 현재 페이지의 이벤트만 표시 -->
            <div
              v-for="event in paginatedEvents"
              :key="event.id"
              class="event--card"
            >
              <div class="img--wrap">
                <img :src="event.image" :alt="event.title">
              </div>
              <h3 class="ellipsis2">{{ event.title }}</h3>
              <NuxtLink :to="event.link">자세히 알아보기 <i class="ico"></i></NuxtLink>
            </div>
          </div>

          <!-- 페이지네이션 -->
          <div class="pagination--wrap">
            <button
              class="pagination--btn prev"
              @click="goToPage(currentPage - 1)"
              :disabled="currentPage === 1"
            >
              <svg fill="none" xmlns="http://www.w3.org/2000/svg" width="24px" height="24px">
                <use href="/img/ico--back--s.svg#main"></use>
              </svg>
            </button>

            <div class="pagination--numbers">
              <button
                v-for="page in displayPages"
                :key="page"
                class="pagination--number"
                :class="{ active: page === currentPage }"
                @click="goToPage(page)"
              >
                {{ page }}
              </button>
            </div>

            <button
              class="pagination--btn next"
              @click="goToPage(currentPage + 1)"
              :disabled="currentPage === totalPages"
            >
              <svg fill="none" xmlns="http://www.w3.org/2000/svg" width="24px" height="24px">
                <use href="/img/ico--forward--s.svg#main"></use>
              </svg>
            </button>
          </div>
        </div>
      </div>
    </main>
</template>

<script setup>
  const events = ref([
    {
      id: 1,
      title: 'Evolution x Vision: 아우디의 빠트로 드라이브',
      image: '/img/stories/technology/img--tech1.jpg',
      link: '/company/view'
    },
    {
      id: 2,
      title: 'Evolution x Vision: 아우디의 까트로 드라이브',
      image: '/img/stories/technology/img--tech1.jpg',
      link: '/company/view'
    },
    {
      id: 1,
      title: 'Evolution x Vision: 아우디의 염트로 드라이브',
      image: '/img/stories/technology/img--tech1.jpg',
      link: '/company/view'
    },
    {
      id: 1,
      title: 'Evolution x Vision: 아우디의 콰트로 드라이브',
      image: '/img/stories/technology/img--tech1.jpg',
      link: '/company/view'
    },
    {
      id: 1,
      title: 'Evolution x Vision: 아우디의 콰트로 드라이브',
      image: '/img/stories/technology/img--tech1.jpg',
      link: '/company/view'
    },
    {
      id: 1,
      title: 'Evolution x Vision: 아우디의 콰트로 드라이브',
      image: '/img/stories/technology/img--tech1.jpg',
      link: '/company/view'
    },
    {
      id: 1,
      title: 'Evolution x Vision: 아우디의 콰트로 드라이브',
      image: '/img/stories/technology/img--tech1.jpg',
      link: '/company/view'
    },
    {
      id: 1,
      title: 'Evolution x Vision: 아우디의 콰트로 드라이브',
      image: '/img/stories/technology/img--tech1.jpg',
      link: '/company/view'
    },
    {
      id: 1,
      title: 'Evolution x Vision: 아우디의 콰트로 드라이브',
      image: '/img/stories/technology/img--tech1.jpg',
      link: '/company/view'
    },
    {
      id: 1,
      title: 'Evolution x Vision: 아우디의 콰트로 드라이브',
      image: '/img/stories/technology/img--tech1.jpg',
      link: '/company/view'
    },
  ]);

  const currentPage = ref(1);
  const itemsPerPage = 9;

  // 전체 페이지 수 계산
  const totalPages = computed(() => {
    return Math.ceil(events.value.length / itemsPerPage);
  });

  // 현재 페이지에 표시할 이벤트
  const paginatedEvents = computed(() => {
    const start = (currentPage.value - 1) * itemsPerPage;
    const end = start + itemsPerPage;
    return events.value.slice(start, end);
  });

  // 표시할 페이지 번호 (최대 5개)
  const displayPages = computed(() => {
    const pages = [];
    const maxDisplay = 5;
    let startPage = Math.max(1, currentPage.value - 2);
    let endPage = Math.min(totalPages.value, startPage + maxDisplay - 1);

    if (endPage - startPage < maxDisplay - 1) {
      startPage = Math.max(1, endPage - maxDisplay + 1);
    }

    for (let i = startPage; i <= endPage; i++) {
      pages.push(i);
    }
    return pages;
  });

  // 페이지 이동
  const goToPage = (page) => {
    if (page >= 1 && page <= totalPages.value) {
      currentPage.value = page;
      // 페이지 이동 시 스크롤 최상단으로
      window.scrollTo({ top: 0, behavior: 'smooth' });
    }
  };
</script>