<!-- 省市区 -->
<template>
  <div>
    <el-cascader :disabled="disabled"
                 v-model="value"
                 @change="handleChange"
                 :props="props"></el-cascader>
  </div>
</template>

<script>
export default {
  name: "cascaderAddress",
  data () {
    return {
      props: {
        lazy: true,
        lazyLoad: (node, resolve) => {
          this.getProvince(node, resolve)
        }
      }

    };
  },
  computed: {
    value () {
      return this.addressValue
    }
  },
  props: ["addressValue", "disabled"],
  methods: {
    handleChange (value) {
      this.$emit("addressChange", value)
    },
    getProvince (node, resolve) {
      let me = this;
      let params = node.data ? { pid: node.data.pid } : null
      me.$axios
        .get(this.$modelConfig.public.findComAddressList, { params })
        .then(res => {
          let data = res.data;
          if (data && data.code === "0") {
            let { level } = node;
            let nodes = data.data
              .map(item => ({
                value: item.id,
                pid: item.id,
                label: item.name,
                leaf: level >= 2
              }));
            resolve(nodes);
          } else {
            me.$message.error(data.msg || "查询失败");
          }
          setTimeout(() => { me.$emit("addressLoadingState", true) }, 500)
        })
        .catch(() => {
          me.$message.error("服务器错误");
          me.$emit("addressLoadingState", true)
        });
    }
  }
}
</script>
<style lang='scss' scoped>
</style>