<template>
  <div class="ibps-bpm-inst-his-wrapper ibps-cell-wrapper">
    <van-field
      v-model="fieldValue"
      :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"
      :rules="editable?rules:null"
      readonly
      @click-input="onClick"
    >
      <template #label>
        {{ label }}<van-icon v-if="desc && descPosition==='lableIcon'" name="warning" class="ibps-dialog-icon" @click="$action.descDialog({ title:label, message:desc })" />
      </template>
      <template #input>
        <template v-if="editable">
          <!-- <van-icon
            slot="left-icon"
            name="add-o"
            class="van-cell__left-icon"
          /> -->
          <div class="van-field__body">
            <input :placeholder="placeholder" class="van-field__control" readonly>
          </div>
        </template>
      </template>
      <template #right-icon>
        <van-icon
          name="arrow"
          @click.native.stop="onClick"
        />
      </template>
    </van-field>
    <div v-if="instList.length >0">
      <div v-for="(item,index) in instList" :key="item.id+index">
        <van-cell>
          <h4>
            <span>{{ item['procDefName'] }}</span>
            <span class="task-name">【{{ item.procDefKey }}】</span>
            <span v-if="!readonly" style="float:right;">
              <i
                class="van-icon van-icon-delete"
                @click="onRemove(index)"
              />
            </span>
          </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>
    <div v-else>
      <h4 v-if="readonly" style="text-align: center;color: gray; padding: 5px 0px;">
        暂无数据
      </h4>
    </div>
    <ibps-field-desc :desc="desc" :desc-position="descPosition" />
    <!-- <div class="table-responsive">
        <table class="table table-bordered">
          <thead>
            <tr>
              <th v-if="!readonly" style="width:50px;">操作</th>
              <th style="width:200px;">实例标题</th>
              <th style="width:100px;">流程名称</th>
              <th style="width:80px;">流程Key</th>
              <th>发起人</th>
              <th>发起时间</th>
              <th>结束时间</th>
            </tr>
          </thead>
          <tbody v-if="instList.length >0">
            <tr v-for="(inst,i) in instList" :key="inst.id">
              <td v-if="!readonly">
                <i
                  class="van-icon van-icon-close"
                  style="color: #f44;"
                  @click="onRemove(i)"/>
              </td>
              <td @click="showBpmnForm(inst)"><a href="javascript:void(0)">{{ inst.subject }}</a></td>
              <td>{{ inst.procDefName }}</td>
              <td>{{ inst.procDefKey }}</td>
              <td>{{ inst.creator }}</td>
              <td>{{ inst.createTime }}</td>
              <td>{{ inst.endTime }}</td>
            </tr>
          </tbody>
          <tbody v-else>
            <tr><td colspan="7">暂无数据</td></tr>
          </tbody>

        </table>
      </div> -->
    <instance-dialog
      v-model="instList"
      :visible="showPopup"
      :params="params"
      @confirm="handleConfirm"
      @close="popup => showPopup= popup"
    />
    <bpmn-formrender-dialog
      :visible="formrenderVisible"
      :title="formrenderTitle"
      :inst-id="instId"
      @close="visible => formrenderVisible = visible"
    />
  </div>
</template>
<script>
import Vue from 'vue'
import bem from '@/mixins/bem'
import FieldMixin from '@/mixins/field'
import InstanceDialog from './dialog'
Vue.component('bpmn-formrender-dialog', () => import('@/business/platform/bpmn/form/dialog'))

export default {
  name: 'ibps-bpm-inst-his',
  components: {
    InstanceDialog
  },
  mixins: [bem, FieldMixin],
  props: {
    placeholder: {
      type: String,
      default: '请选择流程实例'
    },
    params: {
      type: Object
    }
  },
  data() {
    return {
      showPopup: false,
      instList: [],
      fieldValue: '',

      instId: '',
      formrenderVisible: false,
      formrenderTitle: '',

      iconColor: ' #3396FB'
    }
  },
  watch: {
    value: {
      handler: function(val, oldVal) {
        this.instList = this.$utils.isNotEmpty(val) ? this.$utils.parseData(val) : []
      },
      immediate: true
    }
  },
  methods: {
    onClick() {
      if (this.readonly) { return }
      this.showPopup = true
    },
    handleConfirm(data) {
      this.fieldValue = JSON.stringify(data)
      this.handleEmitInput(data)
    },
    handleEmitInput(data) {
      this.$emit('input', this.$utils.isNotEmpty(data) ? JSON.stringify(data) : '')
    },
    showBpmnForm(item) {
      this.instId = item.id
      this.formrenderTitle = item.procDefName
      this.formrenderVisible = true
    },
    onRemove(i) {
      this.instList.splice(i, 1)
      if (this.$utils.isEmpty(this.instList)) {
        this.fieldValue = ''
      }
      this.handleEmitInput(this.instList)
    }
  }
}
</script>

<style lang="scss" scoped>
.ibps-bpm-inst-his-wrapper {
  .time {
    color: #999999;
    font-size: 12px;
  }
  .alink {
    // color: #c98eec;
    color: #0165F2;
    // white-space: nowrap;
  }
}
</style>

