<template>
  <div class="page-container">
    <div class="page-header">
      <h1 class="page-title">{{ $t('learn-our-solutions') }}</h1>
      <app-breadcrumb class="breadcrumb" :items="breadcrumbItems" />
    </div>
    <div v-if="!pending" class="solutions-container">
      <el-tabs v-model="activeName" class="solutions-tabs">
        <el-tab-pane :label="$t('all')" name="all">
          <div class="solution-list">
            <solution-card
              v-for="solution in solutions"
              :key="solution.id"
              :title="solution.title"
              :summary="solution.summary || ''"
              :cover-url="getImageUrl(solution.cover || '')"
              :document-id="solution.id.toString()"
            />
          </div>
        </el-tab-pane>
        <el-tab-pane
          v-for="(group, type) in groupedSolutions"
          :key="type"
          :label="type || '未分类'"
          :name="type || 'no-category'"
        >
          <div class="solution-list">
            <solution-card
              v-for="solution in group"
              :key="solution.id"
              :document-id="solution.id.toString()"
              :cover-url="getImageUrl(solution.cover || '')"
              :title="solution.title"
              :summary="solution.summary || ''"
            />
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
    <div v-else>
      <el-skeleton :rows="6" animated />
    </div>
  </div>
</template>

<script setup lang="ts">
  const localePath = useLocalePath();
  const { getImageUrl } = useDirectusImage();

  const breadcrumbItems = [
    { label: $t('navigation.home'), to: localePath('/') },
    { label: $t('navigation.solutions') },
  ];

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

  const solutionsRaw = computed(() => data.value ?? []);
  const solutions = computed(() =>
    solutionsRaw.value.map((item) => toSolutionListView(item))
  );

  const activeName = ref<string>('all');

  // 按类型分组
  const groupedSolutions = computed(() => {
    const gourps: Record<string, SolutionListView[]> = {};
    for (const sol of solutions.value) {
      let typeKey = '';
      if (typeof sol.solution_type === 'string') {
        typeKey = sol.solution_type;
      } else if (
        sol.solution_type &&
        typeof sol.solution_type === 'object' &&
        'type' in sol.solution_type
      ) {
        typeKey = sol.solution_type || '';
      }
      if (!gourps[typeKey]) gourps[typeKey] = [];
      gourps[typeKey]?.push(sol);
    }
    return gourps;
  });

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

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

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

  .page-header {
    display: flex;
  }

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

  .breadcrumb {
    margin-left: auto;
  }

  .solution-list {
    display: flex;
    flex-wrap: wrap;
    padding: 1rem;
    margin-bottom: 2rem;
    gap: 40px;
  }
</style>
