<template>
  <div class="quality-page">
    <!-- banner -->
    <section class="banner"></section>

    <section class="section quality-cert-section">
      <div class="container">
        <h2 class="section-title">Quality Control System</h2>
        <p class="section-desc">
          The company has dedicated testing laboratories and a standardized quality management system.
          Products undergo rigorous testing to ensure all-round quality assurance before shipment.
        </p>

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

        <div class="dots" v-if="certPageCount > 1">
          <span v-for="n in certPageCount" :key="n" :class="{ active: certPageIndex === n - 1 }" @click="certPageIndex = n - 1" />
        </div>
      </div>
    </section>

    <section class="section process-section">
      <div class="container">
        <h2 class="section-title">Active Components</h2>
        <div class="process-image-wrap" v-if="activeProcessImage">
          <img :src="activeProcessImage" :alt="activeProcessTitle" />
        </div>
      </div>
    </section>

    <section class="section process-section passive-section">
      <div class="container">
        <h2 class="section-title">{{ passiveProcessTitle }}</h2>
        <div class="process-image-wrap" v-if="passiveProcessImage">
          <img :src="passiveProcessImage" :alt="passiveProcessTitle" />
        </div>
      </div>
    </section>
  </div>
</template>

<script setup lang="ts">
import { computed, ref } from "vue";
import { useAll } from "~/api/pubAll/service";
import pktx1 from "@/assets/img/pktx1.png";
import pktx3 from "@/assets/img/pktx3.png";

const { getContent } = useAll();

const pageData = ref<any[]>([]);
const certPageIndex = ref(0);
const certPageSize = 5;

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

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

const bannerSection = computed(() => pageData.value?.[0]?.[0] || {});
const certSection = computed(() => pageData.value?.[1]?.[0] || {});
const activeProcessSection = computed(() => pageData.value?.[2]?.[0] || {});
const passiveProcessSection = computed(() => pageData.value?.[3]?.[0] || {});

const bannerImage = computed(() => {
  return bannerSection.value?.list?.[0]?.image || bannerSection.value?.image || "";
});

const bannerTitle = computed(() => {
  return bannerSection.value?.title || "Automotive-Grade Production Line";
});

const certTitle = computed(() => {
  return certSection.value?.title || "Quality Control System";
});

const certList = computed(() => {
  const list = certSection.value?.list;
  return Array.isArray(list) ? list : [];
});

const certPageCount = computed(() => {
  return Math.ceil(certList.value.length / certPageSize);
});

const currentCertList = computed(() => {
  const start = certPageIndex.value * certPageSize;
  const end = start + certPageSize;
  return certList.value.slice(start, end);
});

const activeProcessTitle = computed(() => {
  return activeProcessSection.value?.title || "Active Components";
});

const activeProcessImage = computed(() => {
  return pktx1;
});

const passiveProcessTitle = computed(() => {
  return passiveProcessSection.value?.title || "Passive Components";
});

const passiveProcessImage = computed(() => {
  return pktx3;
});
</script>

<style scoped lang="scss">
.quality-page {
  background: #f4f4f4;
  min-height: 100vh;
}

.container {
  margin: 0 auto;
}

.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;
}

.breadcrumb-wrap {
  background: #fff;
  padding: 18px 0;
}

.section {
  padding: 32px 15px;
  background: #fff;
  margin-bottom: 12px;
}

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

.section-desc {
  margin-top: 12px;
  text-align: center;
  color: #666666;
  font-size: 14px;
  line-height: 24px;
}

.quality-cert-section {
  padding-bottom: 48px;
}

.cert-grid {
  margin-top: 44px;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 15px;
}

.cert-card {
  background: #fff;
  border: 1px solid #e8e8e8;
  overflow: hidden;
  transition: all 0.2s ease;
}

.cert-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.08);
}

.cert-img-wrap {
  height: 295px;
  padding: 15px;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff;
}

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

.cert-name {
  height: 30px;
  background: #ff4b4b;
  color: #fff;
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.dots {
  margin-top: 24px;
  text-align: center;
}

.dots span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #cfcfcf;
  display: inline-block;
  margin: 0 6px;
  cursor: pointer;
}

.dots span.active {
  background: #e60012;
}

.process-section {
  padding-top: 32px;
  padding-bottom: 36px;
}

.process-image-wrap {
  margin-top: 36px;
  text-align: center;
}

.process-image-wrap img {
  max-width: 100%;
  display: inline-block;
}

.passive-section {
  padding-top: 32px;
  padding-bottom: 90px;
}

@media (max-width: 1280px) {
  .cert-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (max-width: 900px) {
  .section-title {
    font-size: 18px;
  }

  .cert-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .cert-img-wrap {
    height: 220px;
  }
}
</style>
