<template>
  <div style="margin-top: 16px">
    <!-- 处理用户、候选用户、候选分组插槽 -->
    <a-form-model-item label="处理用户">
      <slot v-bind:assignee="userTaskForm.assignee" name="assignee">
        <a-select v-model="userTaskForm.assignee" @change="updateElementTask('assignee')">
          <template v-for="(item,i) in assignee">
            <a-select-option :key="i" :value="item.username" >{{item.realname}}</a-select-option>
          </template>
        </a-select>
      </slot>
    </a-form-model-item>
    <a-form-model-item label="候选用户">
      <slot v-bind:candidateUsers="userTaskForm.candidateUsers" name="candidateUsers">     
        <a-select v-model="userTaskForm.candidateUsers" multiple collapse-tags @change="updateElementTask('candidateUsers')">
          <template v-for="(item,i) in candidateUsers">
            <a-select-option :key="i" :value="item.username" >{{item.realname}}</a-select-option>
          </template>
        </a-select>
      </slot>
    </a-form-model-item>
    <a-form-model-item label="候选分组">
      <slot v-bind:candidateGroups="userTaskForm.candidateGroups" name="candidateGroups">   
        <a-select v-model="userTaskForm.candidateGroups" multiple collapse-tags @change="updateElementTask('candidateGroups')">
          <template v-for="(item,i) in candidateGroups">
            <a-select-option :key="i" :value="item.roleCode" >{{item.roleName}}</a-select-option>
          </template>
        </a-select>
      </slot>
    </a-form-model-item>
    <a-form-model-item label="到期时间">
      <a-input v-model="userTaskForm.dueDate" clearable @change="updateElementTask('dueDate')" />
    </a-form-model-item>
    <a-form-model-item label="跟踪时间">
      <a-input v-model="userTaskForm.followUpDate" clearable @change="updateElementTask('followUpDate')" />
    </a-form-model-item>
    <a-form-model-item label="优先级">
      <a-input v-model="userTaskForm.priority" clearable @change="updateElementTask('priority')" />
    </a-form-model-item>
  </div>
</template>

<script>
export default {
  name: 'UserTask',
  props: {
    id: String,
    type: String,
    businessObject: Object
  },
  inject: ['candidateGroups','candidateUsers','assignee'],
  data() {
    return {
      defaultTaskForm: {
        assignee: '',
        candidateUsers: '',
        candidateGroups: '',
        dueDate: '',
        followUpDate: '',
        priority: ''
      },
      userTaskForm: {},
      mockData: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    }
  },
  watch: {
    id: {
      immediate: true,
      handler() {
        this.bpmnElement = window.bpmnInstances.bpmnElement
        this.$nextTick(() => this.resetTaskForm())
      }
    },
    businessObject: {
      immediate: true,
      handler(val) {
        this.bpmnElement = window.bpmnInstances.bpmnElement
        this.resetTaskForm()
      }
    }
  },
  methods: {
    resetTaskForm() {
      for (const key in this.defaultTaskForm) {
        let value
        // if (key === 'candidateUsers' || key === 'candidateGroups') {
        //   value = this.bpmnElement?.businessObject[key] ? this.bpmnElement.businessObject[key].split(',') : []
        // } else {
          value = this.bpmnElement?.businessObject[key] || this.defaultTaskForm[key]
        // }
        this.$set(this.userTaskForm, key, value)
      }
    },
    updateElementTask(key) {
      const taskAttr = Object.create(null)
      // if (key === 'candidateUsers' || key === 'candidateGroups') {
      //   taskAttr[key] = this.userTaskForm[key] && this.userTaskForm[key].length ? this.userTaskForm[key].join() : null
      // } else {
        taskAttr[key] = this.userTaskForm[key] || null
      // }
      window.bpmnInstances.modeling.updateProperties(this.bpmnElement, taskAttr)
    }
  },
  beforeDestroy() {
    this.bpmnElement = null
  }
}
</script>
