<template>
  <div class="select-area">
    <el-cascader
      :size="size || 'medium'"
      style="width: 100%"
      filterable
      clearable
      v-model="value"
      :props="props"
      @change="handleChange"
    ></el-cascader>
  </div>
</template>

<script>
import asyncRequest from "@/apis/components/select-area";
export default {
  name: "SelectArea",
  props: ["size", "value"],
  /**
   * 属性集合
   * @param {String}       size       : 组件大小 非必填
   * @param {Array}        value      : 选中值  必填
   */
  /**
   * 事件集合
   * @change             : 选中值变化调用   抛出选中数据
   */
  data() {
    return {
      props: {
        lazy: true, //开启远程加载
        async lazyLoad(node, resolve) {
          const { level, value } = node;
          let list = [];
          let res = {};
          if (level === 0) {
            res = await asyncRequest.provinceList({});
          } else if (level === 1) {
            res = await asyncRequest.cityList({ code: value });
          } else {
            res = await asyncRequest.areaList({ code: value });
          }
          if (res && res.code === 0 && res.data) {
            let { data } = res;
            data.forEach((v1) => {
              let province = {
                value:
                  level === 0
                    ? v1.province_code
                    : level === 1
                    ? v1.city_code
                    : v1.area_code,
                label: v1.name,
                leaf: level >= 2,
              };
              list.push(province);
            });
            resolve(list);
          } else {
            resolve([]);
          }
        },
      },
    };
  },
  methods: {
    handleChange(value) {
      this.$emit("change", value);
    },
  },
};
</script>

<style>
.select-area {
  display: inline-block;
}
</style>