import { createApp } from 'vue'
import App from './App.vue'
import dayjs from 'dayjs'
import 'dayjs/locale/zh-cn'
import router from '../router/index-tenant'
import vueFilter from '@/helpers/vueFilter'
import { postMessage, promisefiyPostMessage } from '@/helpers/native-message'

import 'normalize.css'

import '../styles/index.less'
import '../styles/tenant.less'
import { state } from '@/state'
import { browser, setAuth } from '@/helpers/utils'

const app = createApp(App)

// import Vconsole from 'vconsole'
// const vconsole = new Vconsole()
postMessage({ api: 'setBarStatus', content: { status: 0 } })
postMessage({
  api: 'backIconChange',
  content: { backIconHide: true }
})
postMessage(
  {
    api: 'getVersion'
  },
  (res: any) => {
    state.version = res.content.version
  }
)

// 判断是否是管乐团学生端，用来获取基础数据
if (browser().isOrchestraStudent) {
  // await promisefiyPostMessage({
  //   api: 'setCache',
  //   content: {
  //     key: 'h5-colexiu-token',
  //     value: ''
  //   }
  // })

  // 获取管乐团token
  promisefiyPostMessage({ api: 'getUserAccount' }).then((res: any) => {
    const content = res.content
    state.orchestraInfo.token = content.token.split(' ')[1]
    state.orchestraInfo.phone = content.phone
    state.orchestraInfo.nickname = content.nickname
    state.orchestraInfo.avatar = content.avatar
    state.orchestraInfo.unionId = content.unionId || 0
  })

  // 从缓存里面获取token
  promisefiyPostMessage({
    api: 'getCache',
    content: { key: 'h5-colexiu-token' }
  }).then((res: any) => {
    const content = res.content
    if (content.value) {
      setAuth(content.value)
    }
  })
}

if (browser().isTeacher) {
  state.platformType = 'TEACHER'
} else if (browser().isStudent) {
  state.platformType = 'STUDENT'
} else {
  state.platformType = 'STUDENT'
}
if (state.platformType === 'TEACHER') {
  state.platformApi = '/api-teacher'
} else {
  state.platformApi = '/api-student'
}
state.projectType = 'tenant'

dayjs.locale('zh-ch')
app.config.globalProperties.$dayjs = dayjs
app.config.globalProperties.$filters = vueFilter
app.use(router)

app.mount('#app')
