import { dealerTypeTreeList } from '@/api/dealer.js'
const AllocateType = {
  template: `
      <a-cascader
        :value="defaultVal"
        :allowClear="allowClear"
        :change-on-select="changeOnSelect"
        @change="handleChange"
        :options="list"
        expand-trigger="hover"
        :placeholder="placeholder"
        :id="id"
        :fieldNames="{ label: 'name', value: 'code', children: 'childrenList' }"/>
    `,
  props: {
    value: {
      type: Array,
      defatut: function(){
        return []
      }
    },
    id: {
      type: String,
      default: ''
    },
    placeholder:{
      type: String,
      default: '请选择商户类型'
    },
    changeOnSelect:{
      type: Boolean,
      default: false
    },
    allowClear:{
      type: Boolean,
      default: true
    },
    level:{
      type: [String,Number],
      default: ''
    }
  },
  data() {
    return {
      defaultVal: this.value,
      list: []
    };
  },
  mounted() {
    this.getTree()
  },
  watch: {
    value(newValue, oldValue) {
      this.defaultVal = newValue
    }
  },
  methods: {
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    handleChange(value,opts) {
      this.defaultVal = value;
      this.$emit('input', value);
      this.$emit('change', value,opts);
    },
    //  价格类型
    getTree () {
      dealerTypeTreeList({}).then(res => {
        console.log(res)
        if (res.status == 200) {
          if(this.level){
            this.list = this.getBylevel(res.data)
          }else{
            this.list = res.data
          }
        } else {
          this.list = []
        }
      })
    },
    getBylevel(data){
      const ret = []
      // 显示一级分类
      if(this.level == 1){
        data.map(item => {
          if(item.childrenList){
            delete item.childrenList
          }
        })
      }
      return data
    }
  },
};

export default AllocateType