<template>
  <section class="package-section">
    <section class="banner"></section>
    <div class="container">
      <h2 class="title">{{ packageTitle }}</h2>

      <div class="tabs">
        <button class="tab-btn" :class="{ active: typeNavIdx == 0 }" @click="typeNavIdxFun(0)">
          Active Component Packaging
        </button>
        <button class="tab-btn" :class="{ active: typeNavIdx == 1 }" @click="typeNavIdxFun(1)">
          Passive Component Packaging
        </button>
      </div>

      <div class="grid" v-if="packageList.length">
        <div v-for="(item, index) in packageList" :key="item.id || index" class="card">
          <div class="img-wrap">
            <img :src="item.image" :alt="item.title" />
          </div>
          <div class="name">{{ item.title }}</div>
        </div>
      </div>

      <div v-else class="empty">No data</div>
    </div>
  </section>
</template>

<script setup lang="ts">
import { computed, ref } from "vue";
import { useAll } from "~/api/pubAll/service";

const { getContent } = useAll();

const pageData = ref<any[]>([]);
const typeNavIdx = ref(0);

const { data: ssrGetContent } = await useAsyncData<any>("getContentEncapsulation", () =>
  getContent({
    language: 2,
    code: 2,
  })
);

if (ssrGetContent.value?.httpCode == 200) {
  pageData.value = ssrGetContent.value?.data || [];
}

const packageSection = computed(() => {
  return pageData.value?.[4]?.[0] || {};
});

const packageTitle = computed(() => {
  return packageSection.value?.title || "Package Types";
});

const rawList = computed(() => {
  return packageSection.value?.list || [];
});

const activePackageList = computed(() => {
  return rawList.value.filter((item: any) => item.sort == 0);
});

const passivePackageList = computed(() => {
  return rawList.value.filter((item: any) => item.sort == 1);
});

const packageList = computed(() => {
  return typeNavIdx.value == 1 ? passivePackageList.value : activePackageList.value;
});

function typeNavIdxFun(idx: number) {
  typeNavIdx.value = idx;
}
</script>

<style scoped lang="scss">
.package-section {
  background: #f3f3f3;
  padding: 0px 0 56px;
}

.banner {
  height: 200px;
  background-position: center;
  background-repeat: no-repeat;
  background: url(~/assets/img/banner/banner_pub.png) no-repeat center center;
  background-size: 100% auto;
}

.container {
  width: 100%;
  max-width: 100%;
  margin: 0 auto;
  padding: 40px 15px 0;
  box-sizing: border-box;
}

.title {
  margin: 0;
  text-align: center;
  font-size: 18px;
  font-weight: 700;
  color: #333333;
  line-height: 32px;
}

.tabs {
  margin-top: 24px;
  display: flex;
  gap: 12px;
}

.tab-btn {
  flex: 1;
  min-width: 0;
  height: 40px;
  padding: 0 12px;
  border: 1px solid #d6001c;
  border-radius: 8px;
  background: #ffffff;
  color: #666666;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.tab-btn.active {
  background: #e60012;
  color: #ffffff;
  border-color: #e60012;
}

.grid {
  margin-top: 24px;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.card {
  background: #ffffff;
  border: 1px solid #e6e6e6;
  border-radius: 8px;
  overflow: hidden;
}

.img-wrap {
  height: 116px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  box-sizing: border-box;
  background: #ffffff;
}

.img-wrap img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  display: block;
}

.name {
  min-height: 44px;
  border-top: 1px solid #efefef;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px;
  box-sizing: border-box;
  font-size: 14px;
  line-height: 20px;
  color: #555555;
  background: #ffffff;
  text-align: center;
}

.empty {
  text-align: center;
  color: #999999;
  font-size: 14px;
  margin-top: 24px;
}

@media (min-width: 768px) {
  .grid {
    grid-template-columns: repeat(3, 1fr);
  }
}
</style>
