<template>
  <KlNavTab active="工具箱" />
  <!-- 搜索 -->
  <KlSearch @search="search"></KlSearch>
  <div v-loading="loading" class="ma-auto mt-[20px] w-[1440px] flex justify-between">
    <div class="left w-[1000px]">
      <KlWallpaperCategory v-model="pageReq.categoryId" />
      <KlTabBar v-model="pageReq.sourceType" :data="tabBar" class="mt-[20px]" />
      <div class="mt-[4px] box-border border border-[#EEEEEE] rounded-[12px] border-solid bg-[#FFFFFF] px-[28px] py-[17px]" v-loading="loading">
        <div
          @click="handleClick(item)"
          v-for="item in pageRes?.list"
          :key="item.id"
          class="mt-[20px] flex border-b-[1px] border-b-[#eee] border-b-solid pb-[20px] cursor-pointer"
        >
          <div class="h-[142px] w-[200px] text-center">
            <el-image :src="item.iconUrl" alt="" srcset="" class="max-w-[100%] rd-[4px]" fit="cover" />
          </div>
          <div class="ml-[25px] flex-1">
            <div class="text-[15px] text-[#333333] font-normal">{{ item.title }}</div>
            <div class="mt-[8px] text-[14px] text-[#999999] font-normal line-clamp-3" :title="item.description">{{ item.description }}</div>
            <div class="mt-[10px] flex items-center justify-between">
              <div class="flex items-center">
                <div class="flex items-center text-[14px] text-[#666666] font-normal">
                  <img src="~/assets/images/look.png" alt="" srcset="" class="mr-[4px] h-[14px] w-[20px]" />{{ item.previewPoint }}
                </div>
                <div class="ml-[26px] flex items-center text-[14px] text-[#666666] font-normal">
                  <img src="~/assets/images/chat.png" alt="" srcset="" class="mr-[4px] h-[14px] w-[16px]" /> {{ item.commentsPoint }}
                </div>
                <div class="ml-[20px]">
                  <div v-for="(v, index) in item.labels" :key="index" class="mr-[10px] inline-block text-[14px] text-[#1A65FF] font-normal">#{{ v }}</div>
                </div>
              </div>
              <div class="text-[14px] text-[#999999] font-normal">{{ dayjs(item.createTime).format('YYYY-MM-DD HH:mm:ss') }}</div>
            </div>
          </div>
        </div>
        <!-- 暂无数据 -->
        <div v-if="pageRes?.list.length === 0" class="mt-[10px] flex items-center justify-center">
          <!-- <div class="text-16px text-[#999999] font-normal">暂无数据</div> -->
          <el-empty v-if="!pageRes.list.length" :image="emptyImg"></el-empty>
        </div>
        <el-pagination
          v-if="pageRes?.list.length"
          v-model:current-page="pageReq.pageNo"
          :page-size="pageReq.pageSize"
          layout="total, prev, pager, next"
          :total="pageRes?.total"
          :page-sizes="[10, 20, 30]"
          class="mt-[20px]"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <div class="right w-[400px]">
      <div class="box-border border border-[#EEEEEE] rounded-[10px] border-solid bg-[#FFFFFF] pa-[16px]">
        <div class="flex items-center text-[14px] text-[#333333] font-normal">
          <div class="mr-[14px] h-[16px] w-[4px] rounded-[1px] bg-[#1A65FF]"></div>
          热门排行
        </div>
        <div v-for="item in recommendList" :key="item.id" class="mt-[20px] text-[14px] text-[#666] font-normal">{{ item.title }}</div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
  import KlNavTab from '~/components/kl-nav-tab/index.vue'
  import { page } from '~/api/toolbox/index.js'
  import { reactive, ref, watch } from 'vue'
  import KlSearch from '~/pages/toolbox/components/search.vue'
  import { getRelationRecommend } from '~/api/drawe-detail/index'
  import type { ProjectDrawPageRespVO } from '~/api/drawe-detail/types'
  import { parent } from '~/api/upnew/index'
  import emptyImg from '~/assets/images/empty.png'
  import dayjs from 'dayjs'
  import KlWallpaperCategory from '~/components/toolbox-components/kl-wallpaper-category.vue'

  const cascaderProps = { multiple: false, label: 'name', value: 'id', emitPath: false }

  const pageReq = reactive({
    pageNo: 1,
    pageSize: 10,
    title: '',
    categoryId: '-1',
    sourceType: -1,
  })

  const tabBar = ref([
    {
      label: '推荐工具',
      value: -1,
    },
    {
      label: '原创开发',
      value: 0,
    },
    {
      label: '转载分享',
      value: 1,
    },
  ])

  const recommedComputed = computed(() => {
    if (pageReq.sourceType === -1) {
      return true
    }
    return false
  })

  const loading = ref(false)
  const { data: pageRes, refresh: getPage } = await useAsyncData(
    `draw-page-toobox-list`,
    async () => {
      loading.value = true
      const res = await page({
        ...pageReq,
        recommend: recommedComputed,
        sourceType: pageReq.sourceType === -1 ? undefined : pageReq.sourceType,
        categoryId: pageReq.categoryId === '-1' ? undefined : pageReq.categoryId,
      })
      loading.value = false
      if (res.code === 0) {
        return res.data
      }
      return {
        list: [],
        total: 0,
      }
    },
    {
      immediate: true,
    }
  )

  const handleCurrentChange = (page: number) => {
    pageReq.pageNo = page
    getPage()
  }

  const handleSizeChange = (size: number) => {
    pageReq.pageSize = size
    pageReq.pageNo = 1
    getPage()
  }

  const search = (val?: string) => {
    pageReq.pageNo = 1
    pageReq.title = val || ''
    getPage()
  }

  const handleClick = (item: ProjectDrawPageRespVO) => {
    navigateTo(`/toolbox-detail/${item.id}`)
  }

  const { data: recommendList } = await useAsyncData(`draw-recommend-list-getRelationRecommend`, async () => {
    const res = await getRelationRecommend({
      type: 4,
    })
    return res.data
  })

  const { data: projectTypeList } = await useAsyncData(`project-type-list`, async () => {
    const res = await parent({
      type: 3,
      parentId: 0,
    })
    return res.data
  })

  watch([() => pageReq.sourceType, () => pageReq.categoryId], (val) => {
    getPage()
  })
</script>
