<template>
  <div id="app">
    <map-component/>
  </div>
</template>

<script>
import Vue from 'vue'
import VueLoading from 'vue-loading-overlay'
import 'vue-loading-overlay/dist/vue-loading.css'
import RMMGlobal from '@/util/RMMGlobal'
import DeviceOnApis from '@/util/DeviceOnApis'
import mapComponent from './components/OverviewDeviceMap'
import moment from 'moment'

let self = {}
export default {
  name: 'App',
  computed: {},
  components: {
    loading: VueLoading,
    mapComponent
  },
  data () {
    return {
      isLoading: false,
      fullPage: true,
      valid: false,
    }
  },
  mounted () {
    self = this
    window.addEventListener('message', this.eventCallBack)
    this.onLine()
  },
  watch: {
  },
  methods: {
    eventCallBack (e) {
      let cmd = e.data
      switch (cmd.type) {
        case 'language':
          Vue.i18n.set(cmd.value)
          break
        case 'changeThemeColor':
          self.$bus.$emit('changeThemeColor', { themeColor: cmd.value })
          break
        case 'changeTextColor':
          self.$bus.$emit('changeTextColor', { TextColor: cmd.value })
          break
      }
    },
    onLine (callback) {
      // 透過載入圖片測試是否連網
      var img = new Image()
      img.src = 'https://www.baidu.com/favicon.ico?_t=' + Date.now()
      var oRMM = RMMGlobal.get()
      // onload / onerror
      img.onload = function () {
        oRMM.online = true
        RMMGlobal.set(oRMM)
      }
      img.onerror = function () {
        // 載入Baidu 離線API
        let s = document.createElement('script')
        s.setAttribute('type', 'text/javascript')
        s.setAttribute('src', '/static/offlineMap/BaiduMap/amap.min.js')
        document.body.appendChild(s)
        setTimeout(() => {
          let a = document.createElement('script')
          a.setAttribute('type', 'text/javascript')
          a.setAttribute('src', '/static/offlineMap/BaiduMap/modules/MarkerClusterer_min.js')
          document.body.appendChild(a)
          let b = document.createElement('script')
          b.setAttribute('type', 'text/javascript')
          b.setAttribute('src', '/static/offlineMap/BaiduMap/modules/TextIconOverlay_min.js')
          document.body.appendChild(b)
        }, 200)
        oRMM.online = false
        RMMGlobal.set(oRMM)
      }
    }
  },
  beforeDestroy () {}
}
</script>

<style lang="scss">
@import "sass/main";
</style>
