<template>
  <a-cascader v-if="merchantType == 1" :value="selectValue" :options="mccYsMirco" placeholder="选择行业编码"
              @change="changeFunc" :getPopupContainer="(triggerNode) => (triggerNode.parentElement)" />
  <a-cascader v-else-if="merchantType == 2" :value="selectValue" :options="mccYsIndividual" placeholder="选择行业编码"
              @change="changeFunc" :getPopupContainer="(triggerNode) => (triggerNode.parentElement)"/>
  <a-cascader v-else-if="merchantType == 3" :value="selectValue" :options="mccYsCompany" placeholder="选择行业编码"
              @change="changeFunc" :getPopupContainer="(triggerNode) => (triggerNode.parentElement)"/>
</template>
<script lang="ts" setup>
import {defineProps, defineEmits, computed} from 'vue'

import mccYsCompany from './mccYsCompany.json'
import mccYsIndividual from './mccYsIndividual.json'
import mccYsMirco from './mccYsMicro.json'

// 定义组件的传入参数
const props = defineProps({
  value: {type: String, default: ''},
  merchantType: {type: Number, default: 3},
})

// emit： 向父组件进行通讯。
const emit = defineEmits(['update:value'])

// 计算属性
const selectValue = computed(() => {
  if (props.value) {
    return [queryOne(props.value), props.value]
  }

  return null
})

// 当属性发生了变化， 需要通过函数向父组件通信 --》 父组件再通知子组件进行数据的变化。
function changeFunc(value) {
  emit('update:value', value[1])
}

// 查找一级分类
function queryOne(val) {
  var allList;
  switch (props.merchantType) {
    case 1:
      allList = mccYsMirco
      break;
    case 2:
      allList = mccYsIndividual
      break;
    case 3:
      allList = mccYsCompany
      break;
  }

  let result = allList.filter((v) => {
    if (v.children) {
      return v.children.filter((v2) => v2.value == val).length > 0
    }
    return false
  })

  return result.length > 0 ? result[0].value : null
}

</script>

