<template>
  <div style="overflow: hidden;width: 100%;">
    <a-select
      v-model:value="currentNodeKey"
      placeholder="请选择"
      ref="selectTree"
      :allowClear="level == '1'"
      @change="clear">
      <a-select-option v-for="item in data" :key="item.adCode" :value="item.adCode" class="filter-tree">
        {{ item.adName }}
      </a-select-option>
    </a-select>
  </div>
</template>

<script>
import { getAdData } from '../api/duChaAPI.js';
export default {
  props: ['level'],
  data() {
    return {
      opValue: '',
      treeLabel: '',
      treeCode: '',
      initTreeLoading: true,
      data: [],
      currentNodeKey: '',
      province: '360000000000',
    };
  },
  // watch: {
  //   currentNodeKey (id) {
  //     console.log(id, 'currentNodeKey')
  //     this.$nextTick(() => {
  //       // if (id.toString()) {
  //       //   this.$refs['treeCols'].setCurrentKey(id)
  //       // } else {
  //       //   this.$refs['treeCols'].setCurrentKey(null)
  //       // }
  //     })
  //   }
  // },
  mounted() {
    this.getAdDataList();
  },
  methods: {
    clear(value, option) {
      if (value) {
        this.treeLabel = option.context.treeLabel;
        this.treeCode = value;
        this.$emit('addvSelect', value);
      } else {
        if (this.level == '1') {
          this.$emit('addvSelect', {
            adCode: this.treeCode.substring(0, 4) + '00000000',
          });
          this.getAdDataList(this.treeCode.substring(0, 4) + '00000000');
        }
      }
    },
    setCheck(id, nm) {
      this.treeCode = id;
      this.currentNodeKey = id;
      this.treeLabel = nm;
    },
    getAdDataList(adCode) {
      this.treeLabel = '';
      if (!adCode && this.level == '1') {
        this.data = [];
        this.initTreeLoading = false;
        return;
      }
      if (!adCode) {
        adCode = this.province;
      }
      getAdData(adCode).then((res) => {
        if (res.data) {
          this.data = res.data;
          this.initTreeLoading = false;
        }
      });
    },
    // handleClick (data, e) {
    //   console.log(data, e)
    //   this.treeLabel = e.node.title
    //   this.treeCode = data[0]
    //   this.$emit('addvSelect', data)
    //   // this.$refs.selectTree.handleClose()
    // }
  },
};
</script>

<style lang='less' scoped>

</style>
