<template>
  <div>
    <ibps-radio
      v-model="curNodeId"
      :label="jumpType==='select'?'选择路径:':'目标节点'"
      :options="nodeOptions"
      :clearable="false"
      @change="changeNode"
    />
    <ibps-user-selector
      v-if="nodeTypeMap[curNodeId] !=='end'"
      v-model="executors"
      label="选择执行人"
      placeholder="请选择执行人"
      type="user"
      store="array"
      :rules="[{ required: true, message: '必填' }]"
      required
      multiple
      clearable
    />
  </div>
</template>
<script>
/* 用户选择器 */
import IbpsUserSelector from '@/business/platform/org/selector'
export default {
  components: {
    IbpsUserSelector
  },
  props: {
    jumpType: String,
    outgoingNodes: Array,
    outgoingNodesUsersMap: Object
  },
  data() {
    return {
      curNodeId: '',
      executors: []
    }
  },
  computed: {
    nodeOptions() {
      const options = []
      this.outgoingNodes.forEach(node => {
        options.push({
          value: node.nodeId,
          label: this.$utils.isNotEmpty(node.name) ? node.name : node.nodeId,
          type: node.type.key
        })
      })
      return options
    },
    nodeTypeMap() {
      const nodeTypeMap = {}
      this.nodeOptions.forEach((node) => {
        nodeTypeMap[node.value] = node.type
      })
      return nodeTypeMap
    }
  },
  watch: {
    jumpType: {
      handler(val) {
        this.initData()
      },
      immediate: true
    }
  },
  methods: {
    initData() {
      if (this.$utils.isEmpty(this.outgoingNodes)) {
        return
      }
      this.executors = []
      this.curNodeId = this.outgoingNodes[0].nodeId
      this.changeNode()
    },
    changeNode() {
      this.executors = this.outgoingNodesUsersMap[this.curNodeId]
    },
    getData() {
      const destination = this.curNodeId || ''
      const nodeUsers = []
      if (this.$utils.isEmpty(this.executors)) {
        this.$notify({
          message: '请选择执行人',
          type: 'warning'
        })
        return
      }
      const executors = []
      this.executors.forEach((d) => {
        executors.push({
          id: d.id,
          name: d.name,
          type: 'employee',
          groupType: ''
        })
      })
      nodeUsers.push({
        jumpType: this.jumpType,
        nodeId: this.curNodeId,
        executors: executors
      })
      return {
        destination,
        nodeUsers: JSON.stringify(nodeUsers)
      }
    }
  }
}
</script>
