export default {
  data() {
    return {
      cityAreaOptions: [],
      cityAreaOptionsLoad: false
    }
  },
  created () {
    this.cityAreaOptionsInit()
  },
  computed: {},
  methods: {
    cityAreaOptionsInit () {
      const cityArea = this.$localStorage('cityAreaMYLS')
      if (cityArea) {
        const options = JSON.parse(cityArea)
        if (options.length > 0) {
          this.cityAreaOptionsLoad = true
          this.cityAreaOptions = options
        } else {
          this.cityAreaOptionsGet()
        }
      } else {
        this.cityAreaOptionsGet()
      }
    },
    cityAreaOptionsGet () {
      var script = document.createElement('script')
      script.type = 'text/javascript'
      script.src = `http://static.antls.cn/js/addresscodelinkagecityjson.js?ram=${Math.random()}`
      document.getElementsByTagName('head')[0].appendChild(script)
      const loading = this.$loading({
        lock: true,
        text: '省市扩展资源加载中，请稍等...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      window.setTimeout(() => {
        loading.close()
        const options = window.baseCity
        if (!options || options.length === 0) {
          this.$msg('省市资源加载不完整，请刷新后重试~')
          return
        }
        options.map(one => {
          one.value = String(one.value)
          if (one.children && one.children.length > 0) {
            one.children.map(two => {
              two.value = String(two.value)
              if (two.children && two.children.length > 0) {
                two.children.map(three => {
                  three.value = String(three.value)
                })
              }
            })
          }
        })
        this.$localStorage('cityAreaMYLS', JSON.stringify(options))
        this.cityAreaOptions = options
        this.cityAreaOptionsLoad = true
      }, 3000)
    }
  }
}
