<template>
  <div>
    <div
      v-for="({nodeId,name}) in outgoingNodes"
      :key="nodeId"
      class="ibps-cell-wrapper ibps-pl-5 ibps-pr-5"
    >
      <van-field label="路径" :value="name" />
      <ibps-user-selector
        v-model="executors[nodeId]"
        label="选择执行人"
        placeholder="请选择执行人"
        type="user"
        store="array"
        :rules="[{ required: true, message: '必填' }]"
        required
        multiple
        clearable
      />
    </div>
  </div>
</template>
<script>
import IbpsUserSelector from '@/business/platform/org/selector'
export default {
  components: {
    IbpsUserSelector
  },
  props: {
    jumpType: String,
    outgoingNodes: Array,
    outgoingNodesUsersMap: Object
  },
  data() {
    return {
      executors: {}
    }
  },
  watch: {
    outgoingNodesUsersMap: {
      handler(val) {
        this.initData()
      },
      immediate: true
    }
  },
  methods: {
    initData() {
      if (this.$utils.isEmpty(this.outgoingNodes)) {
        return
      }
      const executors = {}
      for (let i = 0; i < this.outgoingNodes.length; i++) {
        const node = this.outgoingNodes[i]
        const nodeId = node.nodeId
        executors[nodeId] = this.outgoingNodesUsersMap[nodeId] || []
      }
      this.executors = executors
    },
    getData() {
      const nodeUsers = []
      for (const nodeId in this.executors) {
        const users = this.executors[nodeId]
        if (this.$utils.isEmpty(users)) {
          this.$notify({
            message: '请选择执行人',
            type: 'warning'
          })
          return
        }
        const executors = []
        users.forEach((user) => {
          executors.push({
            id: user.id,
            name: user.name,
            type: 'employee',
            groupType: ''
          })
        })

        nodeUsers.push({
          jumpType: this.jumpType,
          nodeId: nodeId,
          executors: executors
        })
      }

      return {
        destination: '',
        nodeUsers: JSON.stringify(nodeUsers)
      }
    }
  }
}
</script>
