<template>
  <div>
    <div class="progress-top">
      <div class="progress-top-inner">
        <el-image
          class="progress-top-image"
          alt=""
          :src="baseUrl+ '/activiti/process/read-resource/' + instanceId">
          <div slot="placeholder" style="text-align: center;">
            <i class="el-icon-loading" style="font-size: 20px"></i>
            <div style="padding: 5px">加载中...</div>
          </div>
          <div slot="error">
            <img :src="require('@/assets/images/empty.png')" width="256" alt="未找到数据">
          </div>
        </el-image>
      </div>
    </div>
    <div class="progress-bottom">
      <h4 class="h4">审批流程历史</h4>
      <el-table
        v-loading="loading"
        stripe
        width="100%"
        :data="historyList">
        <el-table-column label="任务名称" prop="activityName" align="center"></el-table-column>
        <el-table-column label="流程实例ID" prop="processInstanceId" align="center"></el-table-column>
        <el-table-column label="开始时间" prop="startTime" align="center">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="结束时间" prop="endTime" align="center">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{i}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="耗时" prop="durationInMillis" align="center">
          <template slot-scope="scope">
            <span>{{ resolveDuration(scope.row.durationInMillis) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="处理人" prop="assigneeName" align="center"></el-table-column>
        <el-table-column label="处理意见" prop="comment" align="center"></el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import {listHistory} from "@/api/progress";

export default {
  name: "Progress",
  props: {
    instanceId: {
      type: [String, Number],
      default: ''
    }
  },
  data() {
    return {
      baseUrl: [process.env.VUE_APP_BASE_API],
      progressUrl: '',
      historyList: [],
      loading: false
    }
  },

  computed: {
    resolveDuration() {
      return duration => {
        // const temp = 24 * 3600 * 1000;
        // const day = Math.floor(duration / temp);
        // const hour = !!day ? Math.floor((duration - day * temp) / (3600*1000)) : Math.floor(duration / (3600*1000));
        // const minute = !!hour ?
        const days = Math.floor(duration / (1000 * 60 * 60 * 24));
        const hours = Math.floor((duration % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        const minutes = Math.floor((duration % (1000 * 60 * 60)) / (1000 * 60));
        let result = '';
        !!days && (result += days + '天');
        !!hours && (result += hours + '时');
        !!minutes && (result += minutes + '分');
        return result;
      }
    }
  },
  created() {
    this.setProgressUrl();
    this.getHistoryList();
  },
  methods: {
    /** 设置流程图src */
    setProgressUrl() {
      this.progressUrl = this.baseUrl + '/activiti/process/read-resource?pProcessInstanceId=' + this.instanceId;
    },

    /** 获取审批历史 */
    getHistoryList() {
      this.loading = true;
      listHistory({instanceId: this.instanceId}).then(response => {
        this.historyList = response.data.filter((item) => {
          if (item.activityName != null) {
            return true
          }
        })
        this.loading = false;
      }).catch(error => {
        this.loading = false;
      })
    }
  }

}
</script>

<style lang="scss" scoped>
.progress-top {
  min-height: 256px;
  overflow-y: hidden;
  border-bottom: 1px solid #ccc;

  .progress-top-inner {
    display: flex;
    justify-content: center;
  }
}

.progress-bottom {

  .h4 {
    font-size: 18px;
    color: #6379bb;
    border-bottom: none;
  }
}
</style>
