<template>
  <el-cascader
    v-model="selected"
    :options="options"
    :props="cascaderProps"
    v-bind="$attrs"
    v-on="$listeners"
  />
</template>

<script>
import { fetchAreaList } from '@/api/common/index'

export default {
  props: {
    value: {
      type: [String, Array],
      default: ''
    },
    cascaderProps: {
      type: Object,
      default: () => ({
        // 是否多选
        multiple: false,
        // 在选中节点改变时，是否返回由该节点所在的各级菜单的值所组成的数组，若设置 false，则只返回该节点的值
        emitPath: false,
        label: 'name',
        value: 'id'
      })
    }
  },
  data() {
    return {
      options: []
    }
  },
  computed: {
    selected: {
      get() {
        return this.value
      },
      set(v) {
        this.$emit('input', v)
      }
    }
  },
  created() {
    this.queryAreaList()
  },
  methods: {
    // 查询地区
    queryAreaList() {
      fetchAreaList().then(res => {
        this.options = this.handleTree(res.data, 'id', 'parentId', 'children', '0')
      })
    }
  }
}
</script>

<style>

</style>
