<template>
  <div class="max-w-screen-xl content my-0 mx-auto">
    <template v-for="(item, index) in list">
      <div v-if="item.children?.length" :key="index" class="title-box m-b-[80px]">
        <template v-if="item.children?.length">
          <n-h1 class="home-title 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="20" :cols="browser.isMobile() ? 1 : 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="style-btn 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=" flex flex-row items-center justify-center m-t-[30px] ">
              <div class="swiper-button-prev">
         
                <n-icon :class="{ disable: activeIndex === 0 }" class="icon" size="32" @click="handlePrev"
                  color="#0661C9">
                  <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
                    viewBox="0 0 32 32">
                    <defs></defs>
                    <path
                      d="M16 2a14 14 0 1 0 14 14A14 14 0 0 0 16 2zm8 15H11.85l5.58 5.573L16 24l-8-8l8-8l1.43 1.393L11.85 15H24z">
                    </path>
                  </svg>

                </n-icon>
              </div>
              <div class="swiper-button-next">
                <n-icon :class="{ disable: activeIndex === (item.children.length - item.grid) }" class="icon" size="32"
                  @click="handleNext" color="#0661C9">
                  <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
                    viewBox="0 0 32 32">
                    <defs></defs>
                    <path
                      d="M2 16A14 14 0 1 0 16 2A14 14 0 0 0 2 16zm6-1h12.15l-5.58-5.607L16 8l8 8l-8 8l-1.43-1.427L20.15 17H8z">
                    </path>
                  </svg>
                </n-icon>
  
              </div>
            </div>

          </swiper> -->
            <swiper :grid="browser.isMobile() ? 2.5 : item.grid" :data="item.children"></swiper>
          </template>

          <template v-if="item.styleType === 2">
            <n-grid
              x-gap="100"
              :y-gap="browser.isMobile() ? 30 : 100"
              :cols="browser.isMobile() ? 1 : 2"
              class="device-list"
            >
              <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="cover-product absolute w-[128px] h-[128px] left-0 top-[-40px]"
                  />

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

                    <div
                      class="device-tag 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="browser.isMobile() ? 16 : 100"
              :cols="browser.isMobile() ? 1 : 3"
              class="software-list"
            >
              <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] software-icon" />

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

                  <div class="software-desc 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="browser.isMobile() ? 1 : 2" class="other-list">
              <n-gi v-for="other of otherstyleEnum" :key="other.value">
                <div :class="{ [`style-${item.styleType}`]: true }" class="show-item">
                  <n-h5 class="other-desk 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>
    </template>
  </div>
</template>

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

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

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

const onSwiper = (swiper: unknown) => {
  if (!swiperInstance.value) {
    console.log(swiper)
    swiperInstance.value = swiper
  }
}

const handleToDoc = (child: ArticleDetailType) => {
  const { articleId, categoryId } = child
  console.log('handleToDoc', child)
  if (articleId) {
    router.push({ path: `/showdoc/${articleId}` })
  } else {
    router.push({ path: `/showcate/${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}` })
  }
}
const onSlideChange = (swiper) => {
  console.log('slide change', swiper)
  activeIndex.value = swiper.activeIndex
}
const handlePrev = () => {
  console.log('handlePrev')
  swiperInstance.value.slidePrev()
}
const handleNext = () => {
  console.log('handleNext')
  swiperInstance.value.slideNext()
}
</script>

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

.icon {
  color: #0661c9;
  margin: 0 5px;

  &.disable {
    opacity: 0.5;
    user-select: none;
  }

  &:hover {
    cursor: pointer;
  }
}

.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;
        }
      }
    }
  }
}
[is-mobile] {
  .content {
    margin-top: -2rem;
  }
  .title-box {
    margin-bottom: 0;
    margin-top: 1.0667rem;
    &:first-of-type {
      margin-top: 0;
    }
    .home-title {
      font-weight: bold;
      font-size: 0.4267rem;
      color: #202020;
      line-height: 0.5067rem;
      margin-bottom: 0.5333rem;
    }
  }
  .style-0 {
    width: 8.9333rem;
    height: 3.7867rem;
    margin: 0 auto;
    border-radius: 0.2667rem;
    .style-btn {
      height: 0.8533rem;
      font-size: 0.32rem;
    }
  }
  .device-list {
    padding: 0 0.5333rem;
    box-sizing: border-box;
  }
  .style-2 {
    width: auto;
    height: 1.6rem;
    .cover-product {
      width: 1.92rem;
      height: 1.92rem;
      top: -0.4267rem;
      left: 0.1067rem;
    }
    .tab {
      padding-left: 2.2933rem;
      .device-title {
        font-size: 0.32rem;
        margin-top: 0.3333rem;
      }
      .device-tag {
        font-size: 0.2667rem;
        margin-top: 0.1067rem;
      }
    }
  }
  .software-list {
    padding: 0 0.5333rem;
    box-sizing: border-box;
    .style-3 {
      width: 100%;
      height: 4.2667rem;
      .software-icon {
        width: 1.0133rem;
        height: 1.0133rem;
      }
      .software-title {
        font-size: 0.3733rem;
        line-height: 0.44rem;
      }
      .software-desc {
        font-weight: 400;
        font-size: 0.2667rem;
      }
    }
  }
  .other-list {
    padding: 0 0.5333rem;
    box-sizing: border-box;
    margin-bottom: 1.0667rem;
    .style-4 {
      width: 100%;
      box-sizing: border-box;
      padding: 0.8rem 0.64rem;
      .other-desk {
        font-weight: bold;
        font-size: 0.3733rem;
        line-height: 0.44rem;
      }
      .otherList {
        li {
          span {
            font-weight: 500;
            font-size: 0.32rem;
            line-height: 0.3733rem;
          }
        }
      }
    }
  }
}
</style>
