<!--
 * @Author: hisense.gaoyuanwei
 * @Date: 2022-06-20 10:12:22
 * @LastEditors: hisense.wuhongjian
 * @LastEditTime: 2022-07-18 18:49:01
 * @Description: 业务组件详情页
-->
<template>
  <div class="business-details" :class="{ fixed2: scrollTop >= 600 }">
    <!-- 头部基本信息 -->
    <business-top-details :dataList="dataList.data"></business-top-details>
    <!-- 导航 -->
    <business-navigation
      :dataList="dataList.data"
      :associatedComponents="associatedComponents"
      :class="{ fixed: scrollTop >= 600 }"
      :selectNow="selectNow"
    ></business-navigation>
    <!-- 关联能力 -->
    <business-associated-ability
      v-if="!loading"
      :associatedComponents="associatedComponents"
      id="business-associated-ability"
      class="scrollBox"
    ></business-associated-ability>
    <!-- 组件展示 -->
    <business-presentation
      :dataList="dataList.data"
      id="business-presentation"
      class="scrollBox"
    ></business-presentation>
    <!-- 功能介绍-->
    <business-function-intorduction
      :dataList="dataList.data"
      id="function-introduction"
      class="scrollBox"
    ></business-function-intorduction>
    <!-- 应用场景 -->
    <business-application-scenarios
      :dataList="dataList.data"
      id="application-scenarios"
      class="scrollBox"
    ></business-application-scenarios>
    <!-- 应用案例 -->
    <business-application-case
      :dataList="dataList.data"
      id="application-case"
      class="scrollBox"
    ></business-application-case>
    <!-- 使用方式 -->
    <business-usage-mode
      :dataList="dataList.data"
      id="business-usage-mode"
      class="scrollBox"
    ></business-usage-mode>
    <!-- 常见问题-->
    <business-common-problem
      :dataList="dataList.data"
      id="common-problem"
      class="scrollBox"
    ></business-common-problem>
  </div>
</template>
<script setup>
  import BusinessApplicationCase from '@/views/detailsAll/components/Business/BusinessApplicationCase.vue' // 应用案例
  import BusinessAssociatedAbility from '@/views/detailsAll/components/Business/BusinessAssociatedAbility.vue'
  import BusinessApplicationScenarios from '@/views/detailsAll/components/Business/BusinessApplicationScenarios.vue' // 应用场景
  import BusinessFunctionIntorduction from '@/views/detailsAll/components/Business/BusinessFunctionIntorduction.vue' // 功能介绍
  import BusinessTopDetails from '@/views/detailsAll/components/Business/BusinessTopDetails.vue' // 头部基本信息
  import BusinessNavigation from '@/views/detailsAll/components/Business/BusinessNavigation.vue' //导航条
  import BusinessPresentation from '@/views/detailsAll/components/Business/BusinessPresentation.vue' //组件展示
  import BusinessUsageMode from '@/views/detailsAll/components/Business/BusinessUsageMode.vue' //使用方式
  import BusinessCommonProblem from '@/views/detailsAll/components/Business/BusinessCommonProblem' //常见问题
  import { ref, onMounted, onBeforeUnmount, reactive } from 'vue'
  import { useRouter } from 'vue-router'
  import {
    updateVisits,
    selectOne,
    queryPartAppByKeyId2,
    browsingInsert,
  } from '@/api/home'
  import mybus from '@/myplugins/mybus'
  const router = useRouter()
  const scrollTop = ref(0)
  const domArr = ref([])
  const selectNow = ref('')
  const dataList = reactive({ data: {} })
  const id = router.currentRoute.value.query.id
  const obj = JSON.parse(window.sessionStorage.getItem('preview'))
  const associatedComponents = ref([{ type: '应用资源', dataList: [] }])
  let loading = ref(true)
  document.documentElement.style.transition = 'all 0.3s ease'
  document.documentElement.scrollTop = 0
  document.body.style.transition = 'all 0.3s ease'
  document.body.scrollTop = 0
  mybus.on('flyToView', (id) => {
    let top = document.querySelector('#' + id).offsetTop - 50
    // console.log(top, document.querySelector('#' + id).offsetTop)
    document.documentElement.scrollTop = top
    document.body.scrollTop = top
  })
  onMounted(() => {
    // console.clear()
    window.addEventListener('scroll', () => {
      domArr.value = document.querySelectorAll('.scrollBox')
      scrollTop.value =
        document.documentElement.scrollTop || document.body.scrollTop
      for (let i = 0; i < domArr.value.length; i++) {
        if (i === 0) {
          if (scrollTop.value <= domArr.value[i + 1].offsetTop - 50) {
            selectNow.value = domArr.value[i].id
          }
        } else if (i == domArr.value.length - 1) {
          if (scrollTop.value >= domArr.value[i].offsetTop - 50) {
            selectNow.value = domArr.value[i].id
          }
        } else {
          if (
            scrollTop.value >= domArr.value[i].offsetTop - 50 &&
            scrollTop.value <= domArr.value[i + 1].offsetTop - 50
          ) {
            selectNow.value = domArr.value[i].id
          }
        }
      }
    })
  })

  const init = (id) => {
    if (id) {
      selectOne(id).then((res) => {
        // console.clear()
        dataList.data = res.data.data
        console.log('初始化详情页=========================>', dataList.data)
        const arrList = ref([])
        arrList.value = JSON.parse(window.sessionStorage.getItem('visits'))
        if (arrList.value && arrList.value.indexOf(id) === -1) {
          arrList.value.push(id)
          updateVisits({
            id: res.data.data.id,
            visits: res.data.data.visits || '0',
          }).then(() => {
            window.sessionStorage.setItem(
              'visits',
              JSON.stringify(arrList.value)
            )
          })
        }
        // 浏览记录
        browsingInsert({
          resourceId: res.data.data.id,
        }).then(() => {
          console.log('浏览记录+1')
        })
      })
      associatedComponents.value.map((item, index) => {
        let queryPartAppByKeyIdParams = {
          keyId: id,
        }
        queryPartAppByKeyId2(queryPartAppByKeyIdParams).then((res) => {
          associatedComponents.value[index].dataList = res.data.data
          loading.value = false
        })
      })
    } else if (obj) {
      dataList.data = obj
      console.log('预览==============', obj)
    }
  }
  init(id)
  onBeforeUnmount(() => {
    mybus.off('flyToView')
  })
</script>
<style lang="less" scoped>
  .fixed {
    position: fixed;
    z-index: 2000;
    top: 0;
    left: 50%;
    margin-left: -9.56rem;
  }

  .fixed2 > div:nth-of-type(3) {
    margin-top: 0.84rem;
  }
</style>
