﻿﻿﻿﻿﻿﻿﻿﻿<template>
  <a-tree
    :tree-data="treeData"
    :load-data="onLoadData"
    :fieldNames="{ children: 'children', title: 'adName', key: 'adCode' }"
    @select="onSelect"
    style="max-height:100%;overflow:auto;"
  />
</template>

<script>
import { getAdData } from '../api/duChaAPI.js';
export default {
  data() {
    return {
      treeData: [],
      rootAdCode: '000000000000',
    };
  },
  mounted() {
    this.getAdDataList();
  },
  methods: {
    getAdDataList() {
      getAdData(this.rootAdCode).then((res) => {
        if (res.data) {
          this.treeData = res.data;
          // 默认加载第一个节点的数据
          if (this.treeData.length) {
            this.onSelect([this.treeData[0].adCode]);
          }
        }
      });
    },
    onSelect(data) {
      // alert(data.code+data.name+data.type);
      // 修改逻辑 如果是行政区划 给两个code 一个完整code  一个likecode 用于like查询 省的就是两位 市县的四位 以此类推
      let likeCode = data[0];
      // if(data.type == 2){ //行政区
      likeCode = likeCode.endsWith('0000000000')
        ? likeCode.substr(0, likeCode.length - 10)
        : likeCode;
      likeCode = likeCode.endsWith('00000000')
        ? likeCode.substr(0, likeCode.length - 8)
        : likeCode;
      likeCode = likeCode.endsWith('000000')
        ? likeCode.substr(0, likeCode.length - 6)
        : likeCode;
      likeCode = likeCode.endsWith('000')
        ? likeCode.substr(0, likeCode.length - 3)
        : likeCode;
      // }
      this.$emit('provTreeSelectChange', {
        code: data[0],
        name: data.adName,
        type: data.type,
        likeCode: likeCode,
      });
    },
    onLoadData(treeNode) {
      return new Promise((resolve) => {
        const str = treeNode.dataRef.adCode;
        getAdData(str).then((res) => {
          if (res.data) {
            const { data } = res;
            treeNode.dataRef.children = data;
            resolve();
          }
        });
      });
    },
  },
};
</script>

<style lang="less" scoped></style>
