<template>
  <div class="page-container">
    <div v-if="pending">
      <el-skeleton :rows="5" animated />
    </div>
    <div v-else>
      <support-tabs model-value="documents" />
      <div class="page-header">
        <h1 class="page-title">{{ $t('navigation.documents') }}</h1>
        <app-breadcrumb class="breadcrumb" :items="breadcrumbItems" />
      </div>
      <div class="page-content">
        <product-filter
          v-model="filters"
          :product-type-options="productTypeOptions"
          :product-options="productOptions"
        />
        <!-- <document-list :documents="filteredDocuments" /> -->
        <document-list :documents="paginatedDocuments" />
        <el-pagination
          v-model:current-page="page"
          class="justify-center pagination-container"
          layout="prev, pager, next"
          hide-on-single-page
          :page-size="documentsPerPage"
          :total="filteredDocuments.length"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
  const localePath = useLocalePath();
  const breadcrumbItems = [
    { label: $t('navigation.home'), to: localePath('/') },
    { label: $t('navigation.support'), to: localePath('/support') },
    { label: $t('navigation.documents') },
  ];

  const filters = reactive({
    selectedType: null as string | null,
    selectedProduct: null as string | null,
    keyword: '',
  });

  const page = ref(1);
  const documentsPerPage = 10;

  const { data: documents, pending, error } = await useDocumentList();

  const productTypeOptions = computed(() => {
    const types: DocumentListProductType[] = [];
    documents.value.forEach((doc: DocumentListView) => {
      doc.products?.forEach((product: DocumentListProduct) => {
        const productType = product.type;
        if (!types.some((item) => item.id === productType.id)) {
          types.push(productType);
        }
      });
    });

    return types;
  });

  const productOptions = computed(() => {
    if (!filters.selectedType) return [];
    const products: DocumentListProduct[] = [];

    documents.value.forEach((doc: DocumentListView) => {
      doc.products?.forEach((product: DocumentListProduct) => {
        if (
          product.type.id === filters.selectedType &&
          !products.some((item) => item.id === product.id)
        ) {
          products.push(product);
        }
      });
    });

    return products;
  });

  const filteredDocuments = computed(() => {
    const fuzzyMatchedDocuments = fuzzyMatch(documents.value, {
      keyword: filters.keyword,
      keys: ['title'],
      threshold: 0.6,
    });
    return fuzzyMatchedDocuments.filter((doc: DocumentListView) => {
      const matchProduct = filters.selectedProduct
        ? doc.products?.some(
            (product: DocumentListProduct) =>
              product.id === filters.selectedProduct
          )
        : filters.selectedType
          ? doc.products?.some(
              (product: DocumentListProduct) =>
                product.type?.id === filters.selectedType
            )
          : true;

      return matchProduct;
    });
  });

  const paginatedDocuments = computed(() => {
    return filteredDocuments.value.slice(
      (page.value - 1) * documentsPerPage,
      page.value * documentsPerPage
    );
  });

  watch(
    () => filters.selectedType,
    () => {
      filters.selectedProduct = null;
    }
  );

  watch(error, (value) => {
    if (value) {
      logger.error('数据获取失败: ', value);
    }
  });

  const pageTitle = $t('page-title.documents');
  usePageSeo({
    title: pageTitle,
  });
</script>

<style scoped>
  .page-container {
    max-width: 1200px;
    margin: 0 auto;
  }

  .page-header {
    display: flex;
    padding: 2rem 2rem 0rem;
  }

  .page-title {
    font-size: 2rem;
    font-weight: bold;
    color: var(--el-color-primary);
    margin-bottom: 1rem;
  }

  .breadcrumb {
    margin-left: auto;
  }

  .document-category {
    padding: 0rem 2rem;
    gap: 4px;
    margin-bottom: 0.5rem;
  }

  .page-content {
    padding: 1rem 2rem 2rem;
  }

  .select-label {
    color: var(--el-text-color-secondary);
    font-size: 0.9rem;
  }

  :deep(.el-select__wrapper),
  :deep(.el-input__wrapper) {
    height: 40px;
    font-size: 0.9rem;
  }

  .pagination-container {
    margin-top: 2rem;
  }

  :deep(.el-pagination) {
    .btn-prev,
    .btn-next {
      .el-icon {
        font-size: 24px;
      }
    }

    .el-pager {
      gap: 0.5rem;
    }
    .el-pager li {
      font-size: 1rem;
      /* border: 1px solid #409eff; */
      border-radius: 50%;

      &:hover {
        background-color: #ecf5ff;
      }

      &.is-active {
        background-color: var(--el-color-primary);
        color: #fff;
      }
    }
  }
</style>
