<template>
  <div class="inst-link ibps-cell-wrapper">
    <van-field
      :title="label"
      :label="label"
      :name="name"
      :size="size"
      :border="false"
      :disabled="disabled"
      :required="required"
      :clearable="clearable"
      :clickable="clickable"
      :is-link="isLink"
      :error="error"
      :error-message="errorMessage"

      :arrow-direction="arrowDirection"
      :label-class="labelClass"
      :label-width="labelWidth"
      :label-align="labelAlign"
      :input-align="inputAlign"
      :error-message-align="errorMessageAlign"
      :autosize="autosize"
      :left-icon="leftIcon"
      :right-icon="rightIcon"
      :icon-prefix="iconPrefix"
      readonly
    >
      <template #label>
        {{ label }}<van-icon v-if="desc && descPosition==='lableIcon'" name="warning" class="ibps-dialog-icon" @click="$action.descDialog({ title:label, message:desc })" />
      </template>
    </van-field>
    <div>
      <div v-for="(item,index) in content" :key="item.id+index">
        <van-cell>
          <h4>
            <span>{{ item['creator'] }}</span>
            <span class="task-name">【{{ item.procDefName }}】</span>
            <van-tag plain :type="item.status|optionsFilter(bpmnStatusOptions,'type')">
              {{ item.status|optionsFilter(bpmnStatusOptions,'label') }}
            </van-tag>
          </h4>
          <p class="alink" @click="showBpmnForm(item)">
            {{ item['subject'] }}
          </p>
          <p class="time">
            发起时间: {{ item.createTime|dateFormat('yyyy-MM-dd HH:mm:ss') }}
            <br>
            结束时间: {{ item.endTime|dateFormat('yyyy-MM-dd HH:mm:ss') }}
          </p>
        </van-cell>
      </div>
    </div>
    <ibps-field-desc :desc="desc" :desc-position="descPosition" />
    <bpmn-formrender-dialog
      :visible="formrenderVisible"
      :title="formrenderTitle"
      :inst-id="instId"
      @close="visible => formrenderVisible = visible"
    />
  </div>
</template>

<script>
import bem from '@/mixins/bem'
import bpmnStatus from '@/mixins/bpmnStatus'
import FieldMixin from '@/mixins/field'

import Vue from 'vue'
Vue.component('bpmn-formrender-dialog', () => import('@/business/platform/bpmn/form/dialog'))

export default {
  name: 'ibps-bpm-link',
  mixins: [bem, bpmnStatus, FieldMixin],
  props: {
    fieldOptions: Object,
    data: Object
  },
  data() {
    return {
      content: [],
      proInstId: '0',

      instId: '',
      formrenderVisible: false,
      formrenderTitle: ''
    }
  },
  watch: {
    data: {
      handler: function(val) {
        if (this.$utils.isEmpty(val)) return
        if (val.currentBpmLink && val.currentBpmLink.length > 0) {
          this.content = val.currentBpmLink
        } else if (val.postBpmLink && val.postBpmLink.length > 0) {
          this.content = val.postBpmLink
        } else {
          this.content = []
        }
      },
      deep: true,
      immediate: true
    }
  },
  mounted() {
    // this.content = this.params
    if (
      this.fieldOptions.bpmLinkType === 'current' &&
      this.params && this.$utils.isNotEmpty(this.params.currentFormInst)
    ) {
      this.content = this.params.currentFormInst
    } else if (
      this.fieldOptions.bpmLinkType === 'postposition' &&
      this.$utils.isNotEmpty(this.params.postFormInst)
    ) {
      this.content = this.params.postFormInst
    }
  },
  methods: {
    showBpmnForm(item) {
      this.instId = item.id
      this.formrenderTitle = item.procDefName
      this.formrenderVisible = true
    }
  }
}
</script>

<style lang="scss" >
.inst-link {
  .time {
    color: #999999;
    font-size: 12px;
  }
  .alink {
    color: #c98eec;
    white-space: nowrap;
  }
}
</style>
