
import store from '../store/storeAll'
import axios from 'axios'
import hswebapp2frame from 'hswebapp2frame'

function filterTo(to) {
  const path_list = ['/login', '/logout', '/', '/register']
  if (path_list.includes(to.path)) return
  const row = {
    meta: to.meta,
    routerName: to.name,
    params: to.params,
    path: to.path,
    query: to.query
  }
  if (to.meta.tabAttr === 'constant') {
    const common_data = sessionStorage.subs_menu ? JSON.parse(sessionStorage.subs_menu) : []
    const smt = common_data.find(item => {
      return '/' + item.index === to.path
    })
    if (smt) {
      row.name = smt.name
    } else {
      row.name = to.name
    }
    pushHeaderTab(row, to)
  } else if (to.meta.tabAttr === 'variable') {
    if (to.params.type && to.params.id) {
      to.meta.tabName.forEach(item => {
        if (to.params.type === item.type) {
          row.name =
            to.params.id === '0' || !to.params.id ? item.addName : item.editName
          pushHeaderTab(row, to)
        }
      })
    } else if (to.params.id && !to.params.type) {
      row.name =
        to.params.id === '0'
          ? to.meta.tabName['0'].addName
          : to.meta.tabName['0'].editName
      pushHeaderTab(row, to)
    }
  } else {
    row.name = '数据有误'
    pushHeaderTab(row, to)
  }
}

function pushHeaderTab(tab) {
  store.commit('pushHeaderTab', tab)
}
function beforeEach(to, from, next) {
  const { matched } = to
  if (matched.length > 1) {
    localStorage['page_path'] = matched[1].path
  } else if (matched.length === 1) {
    localStorage['page_path'] = matched[0].path
  } else {
    localStorage['page_path'] = '/error/404'
  }
  axios.defaults.headers.common['page_path'] = localStorage['page_path']
  if (sessionStorage.user_info) {
    axios.defaults.headers.common['user_info'] = sessionStorage.user_info
  }
  // 清除 debug_sql
  store.commit('clean_bug_sql_info')
  axios.defaults.headers.common['debug_sql'] = false
  sessionStorage['debug_sql'] = false
  const token = window.sessionStorage.getItem('header')
  if (token) axios.defaults.headers.common['Authorization'] = token
  if (to.query.fromMenu !== '1') {
    const obj = {
      item_key: to.path,
      item_url: to.path,
      item_name: to.name,
      app_url: `http://${location.host}`
    }
    hswebapp2frame.pubFun.sendTabToParent(obj)
    let path_str = ''
    if (from.path.indexOf('?') > -1) {
      path_str = from.path + `&fromMenu=1`
    } else {
      path_str = from.path + `?fromMenu=1`
    }
    return next({ path: path_str, query: from.query })
  } else {
    filterTo(to)
    document.title = to.name ? to.name : 'HSCC'
    if (to.path === '/login' && (token || token !== null)) {
      if (from.path) {
        next({
          path: from.path
        })
      } else {
        next({
          path: '/dashboard'
        })
      }
    } else if (to.path === '/dashboard' || to.path === '/error/404') {
      next()
    } else {
      if (to.matched.length === 0) {
        next({
          path: '/error/404'
        })
      } else {
        next()
      }
      if (to.path === '/') {
        next({
          path: '/dashboard'
        })
      }
    }
  }
}
export default beforeEach
