export default {
  data() {
    return {
      areaOptions: [],
      areaOptionsLoad: false
    }
  },
  created () {
    this.areaOptionsInit()
  },
  computed: {},
  methods: {
    areaOptionsInit () {
      const area = this.$localStorage('areaMYLS')
      if (area) {
        const options = JSON.parse(area)
        if (options.length > 0) {
          this.areaOptionsLoad = true
          this.areaOptions = options
        } else {
          this.areaOptionsGet()
        }
      } else {
        this.areaOptionsGet()
      }
    },
    areaOptionsGet () {
      var script = document.createElement('script')
      script.type = 'text/javascript'
      script.src = `http://static.antls.cn/js/addresscodelinkage.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()
        let options = window.baseArea
        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('areaMYLS', JSON.stringify(options))
        this.areaOptions = options
        this.areaOptionsLoad = true
      }, 3000)
    }
  }
}
