<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>
        <el-breadcrumb class="breadcrumb" separator="/">
          <el-breadcrumb-item class="text-md opacity-50">
            <NuxtLink :to="$localePath('/')">{{
              $t('navigation.home')
            }}</NuxtLink>
          </el-breadcrumb-item>
          <el-breadcrumb-item class="text-md opacity-50">
            <NuxtLink :to="$localePath('/support')">{{
              $t('navigation.support')
            }}</NuxtLink>
          </el-breadcrumb-item>
          <el-breadcrumb-item class="text-md opacity-50">
            <NuxtLink :to="$localePath('/support/documents')">{{
              $t('navigation.documents')
            }}</NuxtLink>
          </el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div class="document-category">
        <el-select v-model="selectedType" placeholder="选择产品类型" clearable>
          <el-option
            v-for="type in productTypeOptions"
            :key="type.id"
            :label="type.name"
            :value="type.id"
          />
        </el-select>
        <el-select
          v-model="selectedProduct"
          placeholder="选择系列产品"
          clearable
        >
          <el-option
            v-for="product in productOptions"
            :key="product.id"
            :label="product.name"
            :value="product.id"
          />
        </el-select>
        <el-input
          v-model="keyword"
          placeholder="输入关键词..."
          clearable
          :prefix-icon="Search"
        />
      </div>
      <div class="page-content">
        <document-list :documents="filteredDocuments" />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { Search } from '@element-plus/icons-vue';

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

  const documents = computed(
    () => data?.value.map((item) => toDocumentListView(item)) ?? []
  );

  const keyword = ref('');

  const selectedType = ref<number | null>(null);
  const selectedProduct = ref<number | null>(null);

  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 (!selectedType.value) return [];
    const products: DocumentListProduct[] = [];

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

    return products;
  });

  const filteredDocuments = computed(() =>
    documents.value.filter((doc: DocumentListView) => {
      const matchProduction = selectedProduct.value
        ? doc.products?.some(
            (product: DocumentListProduct) =>
              product.id === selectedProduct.value
          )
        : selectedType.value
          ? doc.products?.some(
              (product: DocumentListProduct) =>
                product.type?.id === selectedType.value
            )
          : true;

      const matchKeyword = keyword.value
        ? doc.title && doc.title.includes(keyword.value)
        : true;

      return matchProduction && matchKeyword;
    })
  );

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

  watch(documents, (value) => {
    console.log(value);
  });

  watch(error, (value) => {
    if (value) {
      console.error('数据获取失败: ', value);
    }
  });
</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 {
    display: flex;
    padding: 0rem 2rem;
    gap: 4px;
  }

  .page-content {
    padding: 1rem 2rem 2rem;
  }
</style>
