<!--
 * @Author: hisense.liangjunhua
 * @Date: 2022-06-08 11:32:22
 * @LastEditors: hisense.liangjunhua
 * @LastEditTime: 2022-06-20 18:59:01
 * @Description: 开发组件详情页
-->
<template>
  <div class="application-details" :class="{ fixed2: scrollTop >= 600 }">
    <!-- 头部基本信息 -->
    <developer-top-details :dataList="dataList.data"></developer-top-details>
    <!-- 导航 -->
    <developer-navigation
      :dataList="dataList.data"
      :class="{ fixed: scrollTop >= 600 }"
      :selectNow="selectNow"
    ></developer-navigation>
    <!-- 组件展示  视频 -->
    <Developer-presentation
      :dataList="dataList.data"
      id="eveloper-presentation"
      class="scrollBox"
    ></Developer-presentation>
    <!-- 功能介绍-->
    <developer-function-intorduction
      :dataList="dataList.data"
      id="function-introduction"
      class="scrollBox"
    ></developer-function-intorduction>
    <!-- 应用场景 -->
    <developer-application-scenarios
      :dataList="dataList.data"
      id="application-scenarios"
      class="scrollBox"
    ></developer-application-scenarios>
    <!-- 应用案例 -->
    <developer-application-case
      :dataList="dataList.data"
      id="application-case"
      class="scrollBox"
    ></developer-application-case>
    <!-- 组件试用 -->
    <developer-trial
      :dataList="dataList.data"
      id="developer-trial"
      class="scrollBox"
    ></developer-trial>
    <!-- 归属部门与服务商-->
    <developer-owning-department-and-service-provider
      :dataList="dataList.data"
      id="department-and-service-provider"
      class="scrollBox"
    ></developer-owning-department-and-service-provider>
    <!-- 常见问题-->
    <developer-common-problem
      :dataList="dataList.data"
      id="common-problem"
      class="scrollBox"
    ></developer-common-problem>
  </div>
</template>
<script setup>
  import DeveloperApplicationScenarios from '@/views/detailsAll/components/Developer/DeveloperApplicationScenarios.vue' //应用场景
  import DeveloperOwningDepartmentAndServiceProvider from '@/views/detailsAll/components/Developer/DeveloperOwningDepartmentAndServiceProvider.vue' //使用方式
  import DeveloperFunctionIntorduction from '@/views/detailsAll/components/Developer/DeveloperFunctionIntorduction.vue' //功能介绍
  import DeveloperApplicationCase from '@/views/detailsAll/components/Developer/DeveloperApplicationCase' //应用案例
  import DeveloperTopDetails from '@/views/detailsAll/components/Developer/DeveloperTopDetails.vue' //头部基本信息
  import DeveloperNavigation from '@/views/detailsAll/components/Developer/DeveloperNavigation.vue'
  import DeveloperPresentation from '@/views/detailsAll/components/Developer/DeveloperPresentation.vue' //组件展示
  import DeveloperCommonProblem from '@/views/detailsAll/components/Developer/DeveloperCommonProblem' //常见问题
  import DeveloperTrial from '@/views/detailsAll/components/Developer/DeveloperTrial' //组件试用
  import { ref, onMounted, onBeforeUnmount, reactive } from 'vue'
  import { useRouter } from 'vue-router'
  import { updateVisits, selectOne } 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'))
  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)
            )
          })
        }
      })
    } 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: 0;
  }
  .fixed2 > div:nth-of-type(3) {
    margin-top: 0.84rem;
  }
</style>
