<template>
  <custom-layout v-loading="loading" :is_menu="false">
    <page :linkList="linkList" :friendList="friendList" :recordList="recordList" :incubatorList="incubatorList" :achieveList="achieveList" :list="newsList" @toActive="toActive"></page>
  </custom-layout>
</template>

<script setup>
import { menuList } from '@/layout/site'
import { onBeforeRouteLeave } from 'vue-router'
// 组件
import page from './page.vue'
const $checkRes = inject('$checkRes')
const route = useRoute()
import { UserStore } from '@/store/user'
const userStore = UserStore()
const user = computed(() => userStore.user)
import { cloneDeep, get } from 'lodash-es'
// 接口
import { NewsStore } from '@/store/api/platform/news'
import { DesignStore } from '@/store/api/platform/design'
import { FriendStore } from '@/store/api/platform/friend'
import { IncubatorStore } from '@/store/api/user/incubator'
import { AchievementStore } from '@/store/api/platform/achievement'
import { UtilStore } from '@/store/api/util'
const utilStore = UtilStore()
const newsStore = NewsStore()
const designStore = DesignStore()
const incubatorStore = IncubatorStore()
const friendStore = FriendStore()
const achievementStore = AchievementStore()
// 加载中
const loading = ref(false)
const carouselList = ref([])
const friendList = ref([])
const incubatorList = ref([])
const achieveList = ref([])
const recordList = ref([])
// 导航
const isIncubator = ref(false)
const hasbrain = ref(false)
const linkList = ref([])
// 新闻
const newsList = ref([])
// 分类
const typeList = ref([
  { title: '热门高校', list: [] },
  { title: '政府部门', list: [] },
  { title: '科研机构', list: [] }
])
const active = ref('0')
// 请求
onMounted(async () => {
  loading.value = true
  await search()
  await searchOther()
  await searchNew()
  loading.value = false
})
const search = async () => {
  for (const val of menuList) {
    if (route.name === val.route) val.hover = true
    else val.hover = false
  }
  let menus = cloneDeep(menuList)
  // 判断, 如果没有孵化基地角色, 则不显示孵化基地菜单
  if (user.value) {
    const hasIncubator = get(user.value, 'role', []).find((f) => f === 'Incubator')
    if (hasIncubator) isIncubator.value = true
  }
  if (!isIncubator.value) menus = menus.filter((f) => f.key !== '11')

  // 特定用户查看产业大脑
  if (user.value && user.value.id == 17) hasbrain.value = true
  else hasbrain.value = false
  if (!hasbrain.value) menus = menus.filter((f) => f.key !== '12')
  linkList.value = menus
}
const searchOther = async () => {
  let res
  // 合作伙伴
  res = await friendStore.list({ is_use: '0' })
  if (res.errcode == '0') friendList.value = res.data
  // 孵化器列表
  res = await incubatorStore.query({ skip: 0, limit: 4, status: '1', is_show: '0' })
  if (res.errcode == '0') incubatorList.value = res.data
  // 成果列表
  res = await achievementStore.query({ skip: 0, limit: 16, status: '1', is_use: '0' })
  if (res.errcode == '0') achieveList.value = res.data
  // 数据总数
  res = await utilStore.toTotal()
  if (res.errcode == '0') recordList.value = res.data
  // 基础设置
  res = await designStore.query({})
  if ($checkRes(res)) {
    carouselList.value = res.data[0].carouselUrl || []
    const friendship = res.data[0].friendship || []
    for (const val of friendship) {
      for (const tag of typeList.value) {
        if (val.type == tag.title) {
          tag.list.push(val)
        }
      }
    }
  }
}
const searchNew = async () => {
  const info = { skip: 0, limit: 8, type: active.value, is_use: '0', status: '1', is_show: '0' }
  let res
  // 政策新闻
  res = await newsStore.query(info)
  if (res.errcode == '0') newsList.value = res.data
}
// 新闻类型选择
const toActive = async (item) => {
  active.value = item
  await searchNew()
}
const scrollTop = ref(0)

onActivated(() => {
  // 配置参数依赖于浏览器
  document.documentElement.scrollTop = scrollTop.value
})
onBeforeRouteLeave((to, from, next) => {
  scrollTop.value = document.documentElement.scrollTop || document.body.scrollTop
  next()
})
</script>
<style scoped lang="scss"></style>
