<template>
  <div id="app">
    <router-view />
    <Initial />
    <theme-picker ref="themePickerRef" style="display: none" />
<!--    <process-msg />-->
  </div>
</template>

<script>
import ThemePicker from '@/components/ThemePicker'
import Initial from '@/components/Initial'
import ProcessMsg from '@/components/Initial/ProcessMsg.vue'
import Define from '@/utils/dict/Define'

const ORIGINAL_THEME = '#EF8708'
export default {
  name: 'App',
  components: {
    Initial,
    ProcessMsg,
    ThemePicker
  },
  metaInfo() {
    return {
      title: this.$store.state.settings.dynamicTitle && this.$store.state.settings.title,
      titleTemplate: title => {
        return title ? `${title} - ${process.env.VUE_APP_TITLE}` : process.env.VUE_APP_TITLE
      }
    }
  },
  data() {
    return {
      loadingInstance: null,
      timer: null,
      time: 0
    }
  },
  watch: {
    time(val) {
      // 超过十分钟后自动关闭
      if (Number(val) >= 10 * 1000) {
        this.loadingInstance.close()
        clearTimeout(this.timer)
      }
    }
  },
  created() {
    this.$nextTick(() => {
      if (this.theme !== ORIGINAL_THEME) this.$refs.themePickerRef.setTheme(this.theme)
    })
    this.$bus.on(Define.ON_PLATFORM_TOY_REPEAT, this.setLoading)
  },
  beforeDestroy() {
    this.$bus.off(Define.ON_PLATFORM_TOY_REPEAT)
    clearTimeout(this.timer)
  },
  methods: {
    setLoading(tag) {
      if (tag) {
        clearTimeout(this.timer)
        this.loadingInstance = this.$loading({
          lock: true,
          fullscreen: true,
          text: '正在进行玩具查重，请稍后...'
        })
        this.timer = setInterval(() => {
          this.time += 1
        })
      } else {
        this.$nextTick(() => {
          this.loadingInstance.close()
        })
      }
    }
  }
}
</script>
<style scoped>
#app .theme-picker {
  display: none;
}
</style>
