<template>
  <div class="quality-page">
    <!-- banner -->
    <section class="banner" :style="bannerStyle">
    </section>
    <!-- 面包屑 -->
    <div class="breadcrumb-wrap">
      <div class="breadcrumb">
        Current Location: <NuxtLink to="/">Manufacturing</NuxtLink>&nbsp;>&nbsp;Quality System
      </div>
    </div>

    <!-- 品控体系 -->
    <section class="section quality-cert-section">
      <div class="container">
        <h2 class="section-title">Quality Control System</h2>
        <p class="section-desc">The company operates dedicated testing laboratories and has established a standardized, process-driven quality management system.<br>Products undergo rigorous testing to ensure quality in every aspect, and only qualified products are released from the factory.</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;

/**
 * 这里把 code 改成“品质体系”这个页面对应的值
 */
const { data: ssrGetContent } = await useAsyncData<any>(
  "getQualityContent",
  () =>
    getContent({
      language: 2,
      code: 3,
    })
);

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

/**
 * 这里的结构按你前面页面的老逻辑习惯来写：
 * 0: banner
 * 1: 证书区
 * 2: 主动器件流程
 * 3: 被动器件流程
 *
 * 如果你的真实接口顺序不是这个，改下面这几个 computed 即可
 */

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] || {});

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

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

const bannerDesc1 = computed(() => {
  return (
    bannerSection.value?.remark ||
    bannerSection.value?.content?.split("\n")?.[0] ||
    ""
  );
});

const bannerDesc2 = computed(() => {
  const content = bannerSection.value?.content || "";
  if (!content) return "";
  const arr = content.split("\n");
  return arr[1] || "";
});

const bannerStyle = computed(() => {
  return {
    backgroundImage: `url(${bannerImage.value})`,
  };
});

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

const certDesc = computed(() => {
  return certSection.value?.content || "";
});

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: #fff;
  min-height: 100vh;
}

.container {
  max-width: 1600px;
  width: 90%;
  margin: 0 auto;
  padding: 0 20px;
}

.banner {
  height: 350px;
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
  background-size: auto 350px;
}

.banner-mask {
  height: 100%;
  background: rgba(214, 0, 28, 0.45);
}

.banner-content {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  color: #fff;
}

.banner-content h1 {
  margin: 0 0 18px;
  font-size: 58px;
  font-weight: 700;
  line-height: 1.1;
}

.banner-content p {
  margin: 0;
  font-size: 30px;
  line-height: 1.7;
}

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

.section {
  padding: 100px 0;
}

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

.section-desc {
  margin: 22px auto 0;
  max-width: 900px;
  text-align: center;
  color: #666;
  font-size: 16px;
  line-height: 1.9;
}

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

.cert-grid {
  margin-top: 44px;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 20px;
  background: #f7f7f7;
  padding: 30px;
  border-radius: 5px;
}

.cert-card {
  background: #fff;
  border: 1px solid #e8e8e8;
  overflow: hidden;
  transition: all 0.2s ease;
  border-radius: 10px;
  overflow: hidden;
  padding-bottom: 40px;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

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

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

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

.cert-name {
  background: #e6252c;
  color: #fff;
  font-size: 15px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: absolute;
  bottom: 0px;
  color: #fff;
  width: 100%;
}

.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: 80px;
  padding-bottom: 36px;
}

.process-image-wrap {
  margin-top: 36px;
  text-align: center;
  padding: 20px;
  border: 1px solid #e8e8e8;
  border-radius: 5px;
}

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

.passive-section {
  padding-top: 110px;
  padding-bottom: 90px;
}
</style>
