import Vue from 'vue'
import 'normalize.css/normalize.css' // A modern alternative to CSS resets

import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import '@/styles/element-variables.scss'
import locale from 'element-ui/lib/locale/lang/zh-CN' // lang i18n
Vue.use(ElementUI, { locale })
import '@/styles/index.scss' // global css

import '@/icons' // icon
import '@/permission' // permission control

import { setWsUrl } from './utils/auth'

// 是否需要mock 不需要注掉下面两行
const { mockXHR } = require('../mock')
mockXHR()

// 引入动画
import animated from 'animate.css'
Vue.use(animated)

// 引入高德地图
// import VueAMap from 'vue-amap'
// Vue.use(VueAMap)
// VueAMap.initAMapApiLoader({
//   key: 'b6c27a2051691fcb386543c800356e05', // key
//   plugin: ['Autocomplete', 'PlaceSearch', 'Scale', 'OverView', 'ToolBar', 'MapType', 'PolyEditor', 'AMap.CircleEditor', 'MarkerClusterer'], // 插件
//   // uiVersion: '1.1', // ui组件库版本号
//   v: '1.4.4' // sdk版本
// })
Vue.config.productionTip = false

// 引入v-charts
import VCharts from 'v-charts'
Vue.use(VCharts)
// 引入音频文件
import audio from '@/assets/audio/alarm.mp3'

// 方法1：注册播放音频事件到Vue实例上
Vue.prototype.playAudio = (audioName) => {
  const buttonAudio = document.getElementById('eventAudio')
  buttonAudio.setAttribute('src', audio)
  console.log(buttonAudio)
  const playPromise = buttonAudio.play()
  if (playPromise) {
    playPromise.then(() => {
      // 音频加载成功
      console.log('音频加载成功')
    }).catch((e) => {
      // 音频加载失败
      console.error(e.message)
    })
  }
  if (buttonAudio.paused === true) {
    buttonAudio.load()
  } else {
    setTimeout(() => {
      buttonAudio.pause()
      buttonAudio.play()
    }, 10)
  }
}
// 注册全局自定义布局组件
import NormalTable from '@/components/NormalTable/index'
import SearchArea from '@/components/SearchArea/SearchArea'
import SearchItem from '@/components/SearchArea/SearchItem'
import AppContainer from '@/components/layout/AppContainer'

Vue.component('app-container', AppContainer)
Vue.component('normal-table', NormalTable)
Vue.component('search-area', SearchArea)
Vue.component('search-item', SearchItem)

import resize from '@/directive/resize/resize' // resize directive
Vue.directive('resize', resize)

import { hasPermission, showWellType, showDeviceType, showIpConfig, isOperation, isAdministrator } from './utils/permission'
Vue.prototype.hasPerm = hasPermission
Vue.prototype.showWellType = showWellType
Vue.prototype.showDeviceType = showDeviceType
Vue.prototype.showIpConfig = showIpConfig
Vue.prototype.isOperation = isOperation
Vue.prototype.isAdministrator = isAdministrator

Vue.config.productionTip = false

import axios from 'axios'
import service from '@/utils/request'

axios.get('./config/project.config.json').then((result) => {
  console.log('get config')
  Vue.prototype.baseConfig = result.data
  document.title = result.data.title // 首先设置网页的title
  service.defaults.baseURL = result.data.baseUrl // 设置默认请求网址
  setWsUrl(result.data.wsUrl)
  new Vue({
    router,
    store,
    render: h => h(App)
  }).$mount('#app')
}).catch((error) => {
  console.error('get baseConfig error...' + error)
})
