<template>
<div>
  <a-form-model :model="boundaryForm" :label-col="labelCol" :wrapper-col="wrapperCol" @submit.native.prevent>
    <a-form-model-item label="定时器类型" prop="eventDefinitionType" key="eventDefinitionType" v-if="boundaryType==='bpmn:TimerEventDefinition'">
            <a-select v-model="boundaryForm.eventDefinitionType" style="min-width:100px">
              <a-select-option  value="timeDate" >日期</a-select-option>
              <a-select-option  value="timeDuration" >持续时长</a-select-option>
              <a-select-option  value="timeCycle" >循环</a-select-option>
              <a-select-option  value="null" >无</a-select-option>
            </a-select>
          </a-form-model-item>
          <a-form-model-item
            v-if="!!boundaryForm.eventDefinitionType && boundaryForm.eventDefinitionType !== 'null'"
            label="定时器"
            prop="eventTimeDefinitions"
            key="eventTimeDefinitions"
            :rules="{ required: true, trigger: ['blur', 'change'], message: '请填写定时器配置' }"
            >
            <a-input v-model="boundaryForm.eventTimeDefinitions" clearable @change="updateProperties"/>
          </a-form-model-item>
          </a-form-model>
          </div>
</template>

<script>
export default {
  name: 'BoundaryEventModule',
   props: {
    businessObject: Object,
    type: String
  },
   data() {
    return {
        boundaryForm: {},
        boundaryType:"",
        bpmnElementSource:{},
        bpmnElementSourceRef:{},
        labelCol: { span: 6 },
      wrapperCol: { span: 17 }
    }
  },
  watch: {
    businessObject: {
      immediate: true,
      handler() {
        this.$nextTick(() => this.resetBoundaryEvent())
      }
    }
  },
  methods:{
      resetBoundaryEvent() {
      this.bpmnElement = window.bpmnInstances.bpmnElement
      this.bpmnElementSource = this.bpmnElement.source
      this.bpmnElementSourceRef = this.bpmnElement.businessObject.sourceRef
      if(this.bpmnElement.businessObject.eventDefinitions.length>0){
        let eventDef = this.bpmnElement.businessObject.eventDefinitions[0]
        this.boundaryType = eventDef.$type
            let  props = ["timeDuration","timeDate","timeCycle"]
            let that = this
            props.forEach(prop=>{
              if(!!eventDef[prop]){
                that.boundaryForm.eventDefinitionType=prop
                console.log(eventDef[prop].body)
                that.boundaryForm.eventTimeDefinitions=eventDef[prop].body
              }
            })
      }
    },
    updateProperties(){
      //获取添加的定时事件的节点
	  this.timeElement = this.bpmnElement;
	//  //获取节点的子节点 timerEventDefinition
    let timerEventDef = this.timeElement.businessObject.eventDefinitions[0]
      let  props = {
		timeDuration: undefined,
		timeDate: undefined,
		timeCycle: undefined
    }
     var newCondition = window.bpmnInstances.moddle.create('bpmn:FormalExpression', { "xsi:type": "bpmn:tFormalExpression" ,body:this.boundaryForm.eventTimeDefinitions})
    props[this.boundaryForm.eventDefinitionType]= newCondition //五分钟间隔
       window.bpmnInstances.modeling.updateModdleProperties(this.bpmnElement,timerEventDef,props)
    }
  },
   beforeDestroy() {
    this.bpmnElement = null
    this.bpmnElementSource = null
    this.bpmnElementSourceRef = null
  }
}
</script>

<style>

</style>
