import Vue from 'vue'

import 'normalize.css/normalize.css' // A modern alternative to CSS resets
import 'jquery'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import locale from 'element-ui/lib/locale/lang/en' // lang i18n
import '@/styles/index.scss' // global css

import App from './App'
import store from './store'
import router from './router'

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

/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online ! ! !
 */
//if (process.env.NODE_ENV === 'production') {
//const { mockXHR } = require('../mock')
//mockXHR()
//}

Vue.prototype.base_url = process.env.VUE_APP_BASE_API

// set ElementUI lang to EN
//Vue.use(ElementUI, { locale })
// 如果想要中文版 element-ui，按如下方式声明
Vue.use(ElementUI)

Vue.config.productionTip = false

// 注册 EventBus
Vue.prototype.$event = new Vue()

// 注册全局自定义快速读取 excel `v-read-excel`
Vue.directive('read-excel', {
  inserted: (el, { value }) => {
    const id = Date.now()
    const input = document.createElement('input')
    el['read-excel-id'] = id
    input.id = id
    input.type = 'file'
    input.accept = '.xlsx, .xls'
    input.style.display = 'none'
    document.body.appendChild(input)
    input.onchange = ({ target: { files: [excel] } }) => {
      try {
        const XLSX = require('xlsx')
        const reader = new FileReader()
        reader.onload = async ({ target: { result } }) => {
          const workbook = XLSX.read(result, { type: 'array' })
          value && value(XLSX.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]]))
        }
        reader.readAsArrayBuffer(excel)
      } catch (e) {
        console.log(e)
        this.$message.error('上传文件失败,请刷新重试')
      }
    }
    el.addEventListener('click', () => {
      input.value = ''
      input.click()
    })
  },
  unbind: el => document.getElementById(el['read-excel-id']).remove()
})


new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
