<template>
  <div v-if="task.taskid">
    <Realtime v-if="task.status === 0 || task.status === 3" :task="task" @refresh="loadTask" />
    <Result v-if="task.status === 2" :task="task" @refresh="loadTask" />
  </div>
</template>

<script>
import {getAchievement, getLatestTask} from '@/api/analysis/achievement';
import Realtime from './realtime/realtime';
import Result from './result/result';

export default {
  components: {
    Realtime,
    Result,
  },
  data() {
    return {
      siteId: 0,
      task: {},
    }
  },
  mounted() {
    const { siteId } = this.$route.params;
    this.siteId = siteId;
    this.loadTask();
  },
  methods: {
    loadTask() {
      const { taskid } = this.$route.query;
      const promise = taskid ? getAchievement(taskid) : getLatestTask(this.siteId);
      promise.then((res) => {
        if (res.data === null) {
          this.$message.error("测流任务不存在");
        } else {
          if (![0, 2, 3].includes(res.data.status)) {
            this.$router.back();
          }
          this.task = res.data;
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
