<template>
  <el-cascader class="wp100" ref="cascader" v-model="model" :props="props" :show-all-levels="false" @change="modeChange"></el-cascader>
</template>
<script>
import { getFlowList, getFlowGroupList } from '@/api/work/workFlow'
export default {
  name: 'mgGroup',
  props: {
    item: {
      type: Object,
      required: true
    },
    faNode: {
      type: Object,
      required: true
    },
    nodeKey: {
      type: String,
      required: true
    },
    type: {
      type: String,
      default: 'input'
    },
    disabled: {
      type: Boolean,
      default: false
    },
    label: {
      type: String,
      default: ''
    },
    placeholder: {
      type: String,
      default() {
        return `请选择${this.item.label}`
      }
    }
  },
  watch: {
    faNode() {
      if (this.faNode[this.nodeKey]) {
        this.model = this.faNode[this.nodeKey].split(',')
      }
      this.$refs.cascader.panel.lazyLoad()
    }
  },
  data() {
    return {
      model: [],
      isSelected: true,
      props: {
        lazy: true,
        lazyLoad(node, resolve) {
          const { level } = node
          if (level == 0) {
            getFlowGroupList({
              parentId: 0,
              status: '',
              isPromoter: false
            }).then(res => {
              const nodes = []
              res.data.forEach(item => {
                nodes.push({
                  value: item.id + '',
                  label: item.name,
                  leaf: level >= 1
                })
              })
              resolve(nodes)
            })
          } else {
            getFlowList({
              groupId: node.value,
              status: 1,
              isPromoter: false
            }).then(res => {
              const nodes = []
              res.data.forEach(item => {
                nodes.push({
                  value: item.groupId + '',
                  label: item.groupName,
                  leaf: level >= 1
                })
              })
              resolve(nodes)
            })
          }
        }
      }
    }
  },
  mounted() {},
  methods: {
    modeChange(val) {
      this.model = val
      this.faNode[this.nodeKey] = val.join(',')
    }
  }
}
</script>
<style scoped>
.wp100 {
  width: 100%;
}
</style>
