<template>
  <div class="max-w-screen-xl content my-0 mx-auto">
    <div v-for="(item, index) in list" :key="index" class="m-b-[80px]">
      <template v-if="item.children?.length">
        <n-h1 class="text-center mb-[80px] font-size-[40px] font-500">{{ item.title }}</n-h1>

        <template v-if="item.styleType === 0">
          <n-grid x-gap="100" y-gap="100" :cols="2">
            <n-gi v-for="child of item.children" :key="child.id">
              <div class="show-item b-rd-3xl relative" :class="{ [`style-${item.styleType}`]: true }"
                :style="{ backgroundImage: `url(${child.cover})` }"
                @click="handleToDoc(child as any as ArticleDetailType)">
                <div
                  class="w-full h-full flex flex-col absolute top-0 left-0 -mx-auto justify-end overflow-hidden shadow-blueGray">
                  <div
                    class="w-full h-[60px] title text-black font-size-[20px] flex justify-center items-center bg-white bg-op-50">
                    {{ child.title }}
                  </div>
                </div>
              </div>
            </n-gi>
          </n-grid>
        </template>

        <template v-if="item.styleType === 1">
          <swiper :slides-per-view="item.grid" :space-between="50" @swiper="onSwiper" @slideChange="onSlideChange">
            <swiper-slide v-for="child of item.children" :key="child.id"
              @click="handleToDoc(child as any as ArticleDetailType)">
              <div class="cover w-full h-[400px] overflow-hidden b-rd-xl"
                :style="{ backgroundImage: `url(${child.cover})` }"></div>
              <div
                class="w-full h-full flex flex-col absolute top-0 left-0 -mx-auto justify-end overflow-hidden shadow-blueGray">
                <div
                  class="w-full h-[50px] title text-black font-size-[20px] flex justify-center items-center bg-white bg-op-50">
                  {{ child.title }}
                </div>
              </div>

            </swiper-slide>
            <!-- <div class="swiper-button-prev">
              <button @click="swiperInstance.slidePrev()">Prev</button>
              </div>
              <div class="swiper-button-next">
                <button @click="swiperInstance.slideNext()">next</button>
              </div> -->
          </swiper>
        </template>

        <template v-if="item.styleType === 2">
          <n-grid x-gap="100" y-gap="100" :cols="2">
            <n-gi v-for="child of item.children" :key="child.id">
              <div @click="handleToDoc(child as any as ArticleDetailType)"
                :class="{ [`style-${item.styleType}`]: true }" class="show-item b-rd-3xl relative w-full h-full">
                <img :src="child.cover" alt="" class="absolute w-[128px] h-[128px] left-0 top-[-40px]" />

                <div class="pl-[128px] w-[calc(100%-128px)] overflow-hidden">
                  <div
                    class="font-size-[20px] whitespace-nowrap text-ellipsis font-bold mt-[25px] w-[calc(100%-20px)] overflow-hidden">
                    {{ child.title }}
                  </div>

                  <div
                    class="text-size-base whitespace-nowrap text-ellipsis w-[calc(100%-20px)] overflow-hidden color-[#909090]">
                    {{ child.description }}
                  </div>
                </div>
              </div>
            </n-gi>
          </n-grid>
        </template>

        <template v-if="item.styleType === 3">
          <n-grid x-gap="100" y-gap="100" :cols="3">
            <n-gi v-for="child of item.children" :key="child.id">
              <div @click="handleToDoc(child as any as ArticleDetailType)"
                :class="{ [`style-${item.styleType}`]: true }"
                class="show-item b-rd-3xl relative w-full h-full flex flex-col align-center items-center justify-center">
                <img :src="child.cover" alt="" class="w-[40px] h-[40px]" />

                <div class="font-size-[20px] font-bold my-[16px]">{{ child.title }}</div>

                <div class="color-[#909090] w-[calc(100%-30px)] text-center">
                  {{ child.description }}
                </div>
              </div>
            </n-gi>
          </n-grid>
        </template>
        <template v-if="item.styleType === 4">
          <!-- {{ otherstyleEnum }} -->
          <n-grid x-gap="100" y-gap="20" :cols="2">
            <n-gi v-for="other of otherstyleEnum" :key="other.value">

              <div :class="{ [`style-${item.styleType}`]: true }" class="show-item">
                <n-h5 class="font-size-[16px] font-800">
                  {{ other.value === 0 ? $t('developer') : $t('recommend_reading') }}
                </n-h5>
                <ul class="otherList">
                  <li class="font-size-[14px]" @click="handleToDoc(child as any as ArticleDetailType)"
                    v-for="child of item.children.filter(c => c.otherType === other.value).slice(0, 3)" :key="child.id">
                    <span>{{ child.title }}</span>
                  </li>
                  <li v-if="item.children.filter(c => c.otherType === other.value).length > 3"
                    class="font-size-[14px] more" @click="handleToMore(item as any as ArticleDetailType)">
                    <span class="font-medium">{{ $t('more') }}>></span>
                  </li>

                </ul>
              </div>

            </n-gi>
            <!-- <n-gi v-for="child of item.children" :key="child.id">
              <div
                @click="handleToDoc(child as any as ArticleDetailType)"
                :class="{ [`style-${item.styleType}`]: true }"
                class="show-item b-rd-3xl relative w-full h-full flex flex-col align-center items-center justify-center"
              >
                <img :src="child.cover" alt="" class="w-[40px] h-[40px]" />

                <div class="font-size-[20px] font-bold my-[16px]">{{ child.title }}</div>

                <div class="color-[#909090] w-[calc(100%-30px)] text-center">
                  {{ child.description }}
                </div>
              </div>
            </n-gi> -->
          </n-grid>
        </template>

      </template>
    </div>
  </div>
</template>

<route>
{
name: "index",
meta: {
layout: "default"
}
}
</route>
<script setup lang="ts">
import { NH1, NH5, NGrid, NGi } from 'naive-ui'
import { getMenuList, type ArticleDetailType } from '@/api'
import type { MenuItem } from '@/api'
import { Swiper, SwiperSlide } from 'swiper/vue'
import 'swiper/css'
import router from '@/plugins/router.ts'
const { t } = useI18n()
// import { useSwiper } from 'swiper/vue';

const otherstyleEnum = [
  {
    value: 0,
    label: t('developer'),
  },
  {
    value: 1,
    label: t('recommend_reading'),
  },
]

const list = ref<MenuItem[]>([])
const swiperInstance = ref();
getMenuList().then((data) => {
  if (data.data) {
    list.value = data.data
  }
})

const onSwiper = (swiper: unknown) => {
  console.log(swiper)
  swiperInstance.value = swiper
}
const onSlideChange = (index: number) => {
  console.log('slide change', index)
}

const handleToDoc = (child: ArticleDetailType) => {
  const { articleId, categoryId } = child
  console.log(articleId, categoryId)
  if (articleId) {
    router.push({ path: `/showdoc/${articleId}` })
  } else {
    // router.push({ path: '/showdoc', query: { cid: categoryId } })
  }
}
const handleToMore = (item: ArticleDetailType) => {
  if (item && item.children && item.children.length > 0) {
    const cateId = item.children[0].categoryId;
    console.log('cateId', cateId)
    router.push({ path: `/showcate/${cateId}` })
  }
}
</script>

<style lang="scss" scoped>
.trans-white-50 {
  background: rgba(255, 255, 255, 0.5);
}

.cover {
  background-repeat: no-repeat;
  background-position: center top;
  background-size: cover;
}

.show-item {
  cursor: pointer;
  background-repeat: no-repeat;
  background-position: center bottom;
  background-size: cover;

  &.style-0 {
    width: 614px;
    height: 346px;
  }

  &.style-2 {
    width: 610px;
    height: 104px;
    background: #f5f9ff;
    border-radius: 10px 10px 10px 10px;
  }

  &.style-3 {
    width: 411px;
    height: 240px;
    background: #ffffff;
    box-shadow: 0px 0px 10px 0px rgba(6, 97, 201, 0.2);
    border-radius: 10px 10px 10px 10px;
  }

  &.style-4 {
    width: 482px;
    min-height: 200px;
    background: #F5F9FF;
    border-radius: 10px 10px 10px 10px;
    padding: 30px 64px;
    cursor: default;
  }

  .otherList {
    padding: 0;

    li {
      padding: 5px 0;
      text-decoration: none;
      list-style: none;

      &:hover {
        cursor: pointer;
      }

      &.more {
        color: #0661C9;

        &:hover {
          color: #107bf5;
        }
      }
    }
  }
}
</style>
