// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'

import './assets/styles/reset.css'
import './assets/styles/common.css'
import 'animate.css'
// import 'element-ui/lib/theme-chalk/index.css'
// import qs from 'qs'
import store from './store'
import Axios from 'axios'
import * as filters from './filters'
// 引入echarts
// import echarts from 'echarts'
import VCharts from 'v-charts'
import plugins from './plugin'
import 'babel-polyfill'

import VueRx from 'vue-rx'

Vue.use(VueRx)
Vue.use(ElementUI)
Vue.use(VCharts)

Object.keys(plugins).forEach(key => {
  const plugin = plugins[key]
  Vue.use(plugin)
})

// 全局过滤器
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})

Axios.defaults.withCredentials = true // 让ajax携带cookie
const AUTH_TOKEN = 'doX8dJusNpe3Tb77mzRhz2JgdCU'
Axios.defaults.headers.common['token'] = AUTH_TOKEN

// axios 全局注入, request
Axios.interceptors.request.use(
  config => {
    if (config.method === 'post') {
      config.headers['Content-Type'] = 'application/json'
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

Axios.interceptors.response.use((response) => {
  const data = response.data
  // 后台返回 10108 code 表示 session 失效
  if (data && data.msgCode === 10108) {
    // 这里时根据自己配置的路由返回首页
    window.location.href = `http://testcenter.bigaka.net/b/dataCenter/statistics/dataOverview.do`
  }
  return response
}, (error) => {
  return Promise.reject(error)
})

Vue.config.productionTip = false
Vue.prototype.axios = Axios

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>',
})
