<template>
  <footer
    class="text-white py-8 w-full relative bg-stone-950 border-t border-zinc-800 overflow-hidden"
  >
    <div class="max-w-screen-2xl mx-auto px-4 sm:px-6 lg:px-10 mb-8">
      <div
        class="grid grid-cols-1 md:grid-cols-2 sm:grid-cols-1 lg:grid-cols-4 gap-8 lg:py-14"
      >
        <!-- Logo & 描述 -->
        <div
          class="flex flex-col lg:items-start :lg:justify-start sm:items-center text-center lg:text-left col-span-2 lg:col-span-1"
        >
          <h3 class="mb-4">
            <i class="icon-brand text-white text-2xl"></i>
          </h3>
          <LanguageSwitcher class="mb-8 hidden lg:block" />
        </div>

        <!-- 企业产品分类链接 -->
        <div class="hidden lg:block">
          <h3
            class="justify-start text-zinc-300 text-xl font-normal leading-snug mb-4"
          >
            {{ t("common.footer.productsLinks.business") }}
          </h3>
          <ul class="space-y-4">
            <li v-for="item in menuBusinessProductsItems" :key="item.path">
              <NuxtLink
                :to="item.path"
                class="text-zinc-500 text-sm font-normal hover:text-white transition"
              >
                {{ t(item.label) }}
              </NuxtLink>
            </li>
          </ul>
        </div>
        <!-- 个人产品分类链接 -->
        <div class="hidden lg:block">
          <h3
            class="justify-start text-zinc-300 text-xl font-normal leading-snug mb-4"
          >
            {{ t("common.footer.productsLinks.personal") }}
          </h3>
          <ul class="space-y-4">
            <li v-for="item in menuPersonalProductsItems" :key="item.path">
              <NuxtLink
                :to="item.path"
                class="text-zinc-500 text-sm font-normal hover:text-white transition"
              >
                {{ t(item.label) }}
              </NuxtLink>
            </li>
          </ul>
        </div>
        <!-- 法律与支持 -->
        <div class="hidden lg:block">
          <h3
            class="justify-start text-zinc-300 text-xl font-normal leading-snug mb-4"
          >
            {{ t("common.footer.legalAndSupport.title") }}
          </h3>
          <ul class="space-y-4">
            <li v-for="item in menuLegalAndSupportItems" :key="item.path">
              <NuxtLink
                :to="item.path"
                class="text-zinc-500 text-sm font-normal hover:text-white transition"
              >
                {{ t(item.label) }}
              </NuxtLink>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <div
      class="max-w-screen-2xl mx-auto px-4 sm:px-6 lg:px-10 text-center border-t border-white/10 py-4"
    >
      <p class="text-white opacity-40 text-xs mt-4">
        &copy; {{ new Date().getFullYear() }} Hanye. All rights reserved.
      </p>
      <p class="text-[10px] text-white/20 mt-4">
        {{ t("common.footer.trademark.statement1") }}
        <br />
        {{ t("common.footer.trademark.statement2") }}
      </p>
    </div>
    <!-- 返回顶部按钮 -->
    <BackToTop />
  </footer>
</template>

<script setup lang="ts">
/**
 * 页脚组件
 * 包含网站导航、联系信息和版权信息
 */
import { useI18n } from "vue-i18n";
const { locale, t } = useI18n();
const config = useRuntimeConfig();
const defaultLocale = config.public.i18n?.defaultLocale || "zh";

// 获取产品分类数据
const { data: categoryResponse } = await useAsyncData(
  `footer-categories-${locale.value}`,
  async () => {
    try {
      // 使用queryCollection从content目录获取数据
      const content = await queryCollection("content")
        .where("path", "LIKE", `/categories/${locale.value}/%`)
        .all();

      if (!content || !Array.isArray(content)) {
        console.log("No category content found for footer");
        return [];
      }

      // 转换为需要的格式
      return content
        .map((item: any) => {
          // 从路径中提取ID - 文件名就是ID
          const pathParts = item.path?.split("/");
          const idFile = pathParts?.[pathParts.length - 1] || "";
          // 如果ID在元数据中有提供，优先使用元数据中的ID
          const id = item.id
            ? parseInt(item.id)
            : parseInt(idFile.replace(".md", "")) || 0;

          return {
            title: item.title || "",
            id: id,
            audiences: item.meta?.audiences,
          };
        })
        .sort((a, b) => (a.id || 0) - (b.id || 0));
    } catch (error) {
      console.error("Error loading category data for footer:", error);
      return [];
    }
  }
);

// 使用计算属性处理产品分类数据
const productCategories = computed(() => {
  return categoryResponse.value || [];
});

// 企业产品导航菜单项
const menuBusinessProductsItems = computed(() => {
  // 构建路径前缀
  const prefix = locale.value === defaultLocale ? "" : `/${locale.value}`;

  // 使用修改后的产品分类数据
  return productCategories.value
    .filter((category: any) => category.audiences === 1)
    .map((category: any) => {
      const path = `${prefix}/products?category=${encodeURIComponent(
        category.title
      )}&audiences=${category.audiences}`;
      return {
        label: category.title,
        path: path,
      };
    });
});

// 个人产品导航菜单项
const menuPersonalProductsItems = computed(() => {
  // 构建路径前缀
  const prefix = locale.value === defaultLocale ? "" : `/${locale.value}`;

  // 使用修改后的产品分类数据
  return productCategories.value
    .filter((category: any) => category.audiences === 0)
    .map((category: any) => {
      const path = `${prefix}/products?category=${encodeURIComponent(
        category.title
      )}&audiences=${category.audiences}`;
      return {
        label: category.title,
        path: path,
      };
    });
});

const menuWebsiteItems = computed(() => [
  {
    label: "common.footer.websiteLinks.home",
    path: locale.value === defaultLocale ? "/" : `/${locale.value}`,
  },
  {
    label: "common.footer.websiteLinks.products",
    path:
      locale.value === defaultLocale
        ? "/products"
        : `/${locale.value}/products`,
  },
  {
    label: "common.footer.websiteLinks.faq",
    path: locale.value === defaultLocale ? "/faq" : `/${locale.value}/faq`,
  },
  {
    label: "common.footer.websiteLinks.about",
    path: locale.value === defaultLocale ? "/about" : `/${locale.value}/about`,
  },
  {
    label: "common.footer.websiteLinks.contact",
    path:
      locale.value === defaultLocale ? "/contact" : `/${locale.value}/contact`,
  },
]);

const menuLegalAndSupportItems = computed(() => [
  {
    label: "common.footer.legalAndSupport.support",
    path:
      locale.value === defaultLocale ? "/support" : `/${locale.value}/support`,
  },
  {
    label: "common.footer.legalAndSupport.privacy",
    path:
      locale.value === defaultLocale ? "/privacy" : `/${locale.value}/privacy`,
  },
  {
    label: "common.footer.legalAndSupport.terms",
    path: locale.value === defaultLocale ? "/terms" : `/${locale.value}/terms`,
  },
]);
</script>
