<!--
 * @Author: hisense.liangjunhua
 * @Date: 2022-06-08 11:32:22
 * @LastEditors: hisense.liangjunhua
 * @LastEditTime: 2022-06-20 18:59:18
 * @Description: 应用详情页
-->
<template>
  <div class="application-details" :class="{ fixed2: scrollTop >= 600 }">
    <!-- 头部基本信息 -->
    <application-top-details
      :dataList="dataList.data"
    ></application-top-details>
    <!-- 导航 -->
    <application-navigation
      :dataList="dataList.data"
      :class="{ fixed: scrollTop >= 600 }"
      :selectNow="selectNow"
    ></application-navigation>
    <!-- 应用展示  视频 -->
    <application-presentation
      :dataList="dataList.data"
      id="application-presentation"
      class="scrollBox"
    ></application-presentation>
    <!-- 关联组件 -->
    <application-associated-components
      :dataList="dataList.data"
      id="application-associated-components"
      class="scrollBox"
    ></application-associated-components>
    <!-- 关联能力 -->
    <application-associated-ability
      :associatedComponents="associatedComponents"
      id="application-associated-ability"
      class="scrollBox"
    ></application-associated-ability>
    <!-- 功能介绍-->
    <application-function-intorduction
      :dataList="dataList.data"
      id="function-introduction"
      class="scrollBox"
    ></application-function-intorduction>
    <!-- 使用能力 -->
    <application-ability-toise
      :dataList="dataList.data"
      id="ability-to-use"
      class="scrollBox"
    ></application-ability-toise>
    <!-- 部署与安全-->
    <application-deployment-and-security
      :dataList="dataList.data"
      id="deployment-and-security"
      class="scrollBox"
    ></application-deployment-and-security>
    <!-- 归属部门与服务商-->
    <application-owning-department-and-service-provider
      :dataList="dataList.data"
      id="department-and-service-provider"
      class="scrollBox"
    ></application-owning-department-and-service-provider>
    <!-- 常见问题-->
    <application-common-problem
      :dataList="dataList.data"
      id="common-problem"
      class="scrollBox"
    ></application-common-problem>
  </div>
</template>
<script setup>
  import ApplicationAbilityToise from '@/views/detailsAll/components/Application/ApplicationAbilityToise.vue'
  import ApplicationAssociatedComponents from '@/views/detailsAll/components/Application/ApplicationAssociatedComponents.vue'
  import ApplicationAssociatedAbility from '@/views/detailsAll/components/Application/ApplicationAssociatedAbility.vue'
  import ApplicationOwningDepartmentAndServiceProvider from '@/views/detailsAll/components/Application/ApplicationOwningDepartmentAndServiceProvider.vue'
  import ApplicationFunctionIntorduction from '@/views/detailsAll/components/Application/ApplicationFunctionIntorduction.vue'
  import ApplicationDeploymentAndSecurity from '@/views/detailsAll/components/Application/ApplicationDeploymentAndSecurity.vue'
  import ApplicationTopDetails from '@/views/detailsAll/components/Application/ApplicationTopDetails.vue'
  import ApplicationNavigation from '@/views/detailsAll/components/Application/ApplicationNavigation.vue'
  import ApplicationPresentation from '@/views/detailsAll/components/Application/ApplicationPresentation.vue'
  import ApplicationCommonProblem from '@/views/detailsAll/components/Application/ApplicationCommonProblem' //常见问题
  import { ref, onMounted, onBeforeUnmount, reactive } from 'vue'
  import { useRouter } from 'vue-router'
  import { updateVisits, selectOne, queryPartAppByKeyId } from '@/api/home'
  import mybus from '@/myplugins/mybus'
  const associatedComponents = ref([
    { type: '组件服务', dataList: [] },
    { type: '数据资源', dataList: [] },
    { type: '基础设施', dataList: [] },
  ])
  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-50)
    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)
            )
          })
        }
      })
      associatedComponents.value.map((item, index) => {
        let queryPartAppByKeyIdParams = {
          keyId: id,
          type: item.type,
        }
        queryPartAppByKeyId(queryPartAppByKeyIdParams).then((res) => {
          associatedComponents.value[index].dataList = res.data.data
        })
      })
    } 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>
