<template>
  <!-- 选择框 -->
  <MCascade
    width="180"
    v-model="value"
    :data="mapList(selectData)"
    @change="change"
  />
</template>

<script>
export default {
  props: {
    selectData: [],
    initValue: {
      type: [String, Number, Array, Object],
      default: () => {
        return [];
      },
    },
  },
  data() {
    return {
      value: this.initValue === "" ? [] : [],
    };
  },
  methods: {
    change(result) {},
    mapList(list) {
      let _list = list.map((i) => {
        return {
          id: i.code,
          name: i.name,
          parentCode: i.parentCode,
        };
      });
      let tree = this.listToTree(_list);
      return tree;
    },
    listToTree(list) {
      var map = {},
        node,
        tree = [],
        i;
      for (i = 0; i < list.length; i++) {
        map[list[i].id] = list[i];
        list[i].children = [];
      }
      for (i = 0; i < list.length; i += 1) {
        node = list[i];
        if (node.parentCode) {
          map[node.parentCode].children.push(node);
        } else {
          tree.push(node);
        }
      }
      return tree;
    },
  },
};
</script>
