<template>
  <div class="page-container">
    <div class="page-header">
      <h1 class="page-title">{{ $t('our-productions') }}</h1>
      <el-breadcrumb class="breadcrumb">
        <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('/productions')">{{
            $t('navigation.productions')
          }}</NuxtLink>
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div v-if="!pending" class="page-content">
      <div class="productions-container">
        <el-collapse v-model="activeNames" class="production-collapse">
          <el-collapse-item
            v-for="(group, type) in groupedProductions"
            :key="type"
            :title="type || '未分类'"
            :name="type || 'no-category'"
          >
            <div class="group-list">
              <production-card
                v-for="production in group"
                :key="production.id"
                :slug="production.id.toString()"
                :image-url="getImageUrl(production.cover.toString())"
                :name="production.name"
                :description="production.summary || ''"
              />
            </div>
          </el-collapse-item>
        </el-collapse>
      </div>
    </div>
    <div v-else>
      <el-skeleton :rows="6" animated />
    </div>
  </div>
</template>

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

  const { data, pending, error } = useProductList();

  const activeNames = ref<string[]>([]);

  const productionsRaw = computed(() => data.value ?? []);
  const productions = computed(() =>
    productionsRaw.value.map((item) => toProductListView(item))
  );

  // 按类型分组
  // 兼容 production_type 既可能为对象也可能为字符串
  const groupedProductions = computed(() => {
    const groups: Record<string, ProductListView[]> = {};
    for (const prod of productions.value) {
      let typeKey = '';
      if (typeof prod.product_type === 'string') {
        typeKey = prod.product_type;
      } else if (
        prod.product_type &&
        typeof prod.product_type === 'object' &&
        'name' in prod.product_type
      ) {
        typeKey = prod.product_type || '';
      }
      if (!groups[typeKey]) groups[typeKey] = [];
      groups[typeKey]?.push(prod);
    }
    console.log(groups);
    return groups;
  });

  watch(groupedProductions, () => {
    if (groupedProductions.value) {
      activeNames.value = [
        ...Object.keys(groupedProductions.value),
        'no-category',
      ];
    }
  });

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

  watch(pending, (value) => {
    if (!value) {
      console.log('AsyncData: ', data.value);
    }
  });

  onMounted(async () => {
    if (groupedProductions.value) {
      activeNames.value = [
        ...Object.keys(groupedProductions.value),
        'no-category',
      ];
    }
  });
</script>

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

  .page-header {
    display: flex;
  }

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

  .breadcrumb {
    margin-left: auto;
  }

  .productions-container {
    display: flex;
    flex-direction: column;
    gap: 40px;
  }

  .production-group {
    margin-bottom: 32px;
  }

  .group-title {
    font-size: 1.5rem;
    font-weight: bold;
    margin-bottom: 16px;
    color: var(--el-color-primary);
  }

  .group-list {
    display: flex;
    flex-wrap: wrap;
    padding-top: 1rem;
    gap: 20px;
    justify-content: flex-start;
  }

  :deep(.el-collapse-item__header) {
    padding: 30px auto;
    font-size: 1.5rem;
  }
</style>
