import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/pages/layout'

Vue.use(Router)

let router = new Router({
  routes: [
    {
      path: '/',
      name: 'layout',
      component: Layout,
      children: [
        {
          path: '/',
          name: '首页',
          component: () => import('@/pages/home')
        },
        {
          path: '/message',
          name: '消息资讯',
          component: () => import('@/pages/message')
        },
        {
          path: '/join-us',
          name: '加入我们',
          component: () => import('@/pages/join')
        }
      ]
    }
  ]

})

router.afterEach((to, from) => {
  if (to.query.id) {
    let target = document.querySelector('#' + to.query.id)
    if (!target) {
      setTimeout(() => {
        target = document.querySelector('#' + to.query.id)
        target ? you(target, to.query.id) : window.scrollTo(0, 0)
      }, 100)
    } else {
      you(target, to.query.id)
    }
  } else {
    setTimeout(() => {
      you()
    }, 16)
  }
  //  || !~queue.indexOf(to.fullPath)
  // if (queue.length === 2) {
  //   queue.shift()
  // }
  // queue.push(to.fullPath)

  function you (target, id) {
    if (target) {
      let node = target
      let top = 0
      while (node && node !== document.documentElement && node !== document.body) {
        top += node.offsetTop
        node = node.offsetParent
      }
      if (id === 'h2' || id === 'h4' || id === 'h5' || id === 'h6' || id === 'zddh' || id === 'hzhb' || id === 'gywm') {
        setTimeout(() => {
          window.skip = true
          window.scrollTo(0, top - 260)
        })
      } else {
        setTimeout(() => {
          window.skip = true
          window.scrollTo(0, top - 70)
        })
      }
    } else {
      window.scrollTo(0, 0)
    }
  }
})

export default router
