import _ from 'lodash'
import Vue from 'vue'
import axios from 'axios'
import ELEMENT from 'element-ui'

import config from './config/index.js'
import Privilege from './common/privilege.js'
import components from './components/index.js'
import directives from './directives/index.js'
import filters from './filters/index.js'
import MixinELEMENT from './mixins/index.js'

import main from './pages/special/specialDesignerDialog.vue'

// 设置编辑器内的弹出层效果
const dialogMixin = {
  install: Vue => {
    Object.assign(ELEMENT.Dialog.props, {
      top: {
        type: String,
        default: '8vh'
      },
      appendToBody: {
        type: Boolean,
        default: true
      }/* ,
      zIndex: {
        type: [Number, String],
        default: 4000
      } */
    })
  }
}

Vue.use(MixinELEMENT)
Vue.use(dialogMixin)
Vue.use(ELEMENT)
Vue.use(components)
Vue.use(directives)
Vue.use(filters)

if (process.env.NODE_ENV === 'development') {
  Vue.config.devtools = true
}
_.extend(axios.defaults, config.axios)
axios.interceptors.response.use(
  res => {
    return res
  },
  err => {
    if (err.message.includes('code 401')) {
      window.location.href = 'index.html'
      return
    }
    return Promise.reject(new Error(err))
  }
)

window.appInit = async function () {
  let res = await axios.get('/ui/application/privs')
  let privilege = new Privilege((res && res.data && res.data.data) || {})
  if (!res || !res.data) {
    console.warn('没有通过接口 /ui/application/privs 取到当前用户的权限配置', privilege)
  }
  res = await axios.get('/ui/sites')
  window.vueapp = new Vue({
    data: {
      siteID: res.data.siteID || 0,
      prefix: res.data.prefix || '',
      platformAttributes: res.data.platformAttributes
    },
    el: '#specialDesignerDialog',
    render: h => h(main)
  })

  // 空vue对象，用于跨组件通讯
  window.eventHub = new Vue()
}
window.appInit()

// 当前打开dialog的编辑器实例为ownerDialog
// window.addEventListener('load', _ => {
//   window.ownerDialog.onOK = function () {
//     window.app.$children[0].save()
//   }
// }, false)
