import pCascaderZone from './index'

export default {
  name: 'pCascaderZoneWms',
  mixins: [pCascaderZone],
  props: {
    url: { type: String, default: '/api/upcloud-base-common/region/children2' },
    placeholder: { type: String, default: '省/市/区' },
    method: { type: String, default: 'get' },
    paramKey: { type: String, default: 'fatherName' },
    paramValue: { type: String | Array },
    baseId: { type: String, default: '中国' },
    fieldNames: {
      type: Object,
      default() {
        return { label: 'regionsName', value: 'regionsName', children: 'children' }
      }
    }
  },

  methods: {
    getZone(params) {
      let _this = this
      const { url, method } = this
      if (this.disabled || !url) return

      let config = {
        url,
        method
      }
      if (method == 'post') {
        config.data = params
      } else if (method == 'get') {
        config.params = params
      }
      this.loading = true
      return this.axios(config)
        .then(respose => {
          this.loading = false
          if (respose.success) {
            respose.data.forEach(item => {
              if (item.levelId < 3) {
                item.isLeaf = false
              } else {
                item.isLeaf = true
              }
            })
            return respose.data
          } else {
            _this.$error({ content: respose.message })
          }
        })
        .catch(err => {
          this.loading = false

          //   _this.$error({ content: JSON.stringify(err) })
        })
    }
  }
}
