import Vue from 'vue'
import router from './router'
import 'normalize.css/normalize.css' // a modern alternative to CSS resets
import 'element-ui/lib/theme-chalk/display.css'
import Viewer from 'v-viewer'
import {
  getToken, removeMenu,
  removeBtn, removeToken
} from '@/utils/auth'
import ElementUI from 'element-ui'
import 'viewerjs/dist/viewer.css'

import 'element-ui/lib/theme-chalk/display.css'
import './styles/element-variables.scss'

import FlowChart from '@/components/flow-chart'
import './styles/index.scss' // global css
import globalComponents from './components/globalComponents'
import App from './App'
import store from './store'
import base from './assets/utils/prototype/index'

import dataV from '@jiaminghi/data-view'
// 按需引入vue-awesome图标
import Icon from 'vue-awesome/components/Icon'
import 'vue-awesome/icons/chart-bar.js'
import 'vue-awesome/icons/chart-area.js'
import 'vue-awesome/icons/chart-pie.js'
import 'vue-awesome/icons/chart-line.js'
import 'vue-awesome/icons/align-left.js'
import echarts from 'echarts'
import * as filters from './filters' // global filters


// import { CredentialsNode, NormalNode} from "@/components/approval-node"
// Vue.component('credentials-node',CredentialsNode)
// Vue.component('normal-node',NormalNode)


Vue.component('icon', Icon)
Vue.use(dataV)
Vue.component('flow-chart', FlowChart)

Vue.prototype.$echarts = echarts

window.Array.prototype.remove = function(val) {
  var index = this.indexOf(val)
  if (index > -1) {
    this.splice(index, 1)
  }
}
// 兼容
if (typeof String.prototype.startsWith !== 'function') {
  window.String.prototype.startsWith = function(prefix) {
    return this.slice(0, prefix.length) === prefix
  }
}

Vue.use(globalComponents)
Vue.use(Viewer, {
  defaultOptions: {
    zIndex: 9999
  }
})

Vue.use(base)
import PuzzleVerification from 'vue-puzzle-verification'
import { registerEvent } from '@/utils/watchToken'
import { setupComponets } from './plugins/components'

setupComponets(Vue)
Vue.use(PuzzleVerification)

registerEvent()

// register global utility filters
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})

Vue.config.productionTip = false
Vue.use(ElementUI)

router.beforeEach((to, from, next) => {
  if (to.path === '/login' || from.path === '/accept') {
    next()
  } else if (getToken() === null || getToken() === undefined || getToken() === '') {
    removeMenu()
    removeBtn()
    removeToken()
    // router.replace({ name: 'login' })
    next('/login')
  } else {
    next()
  }
})

window.vm = new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
