<template>
  <div>
    <div v-if="taskInfo !== null">
      <h1>{{ taskInfo.name }}</h1>
      <p style="white-space: pre-line"> {{ taskInfo.description }}</p>
    </div>
    
    <p>
      <a-button v-if="trainVideo !== null" type="default" @click="startTrain">{{ $t('lang.PdfPrint.watchTrainingVideo') }}</a-button>
    </p>
    
    <a-button type="primary" style="width: 100%;" @click="openPdf">{{ $t('lang.PdfPrint.viewReport') }}</a-button>
    <a-button key="console" style="width: 100%;" type="primary" @click="finishTask">{{ $t('lang.PdfPrint.operationComplete') }}</a-button>

    <Modal v-model:show="qrCodeModal" :force="true">
      <template #header>
        {{ $t('lang.PdfPrint.scanQrCodes') }}
      </template>
      <div>
        <h4>{{ $t('lang.PdfPrint.describe1') }}</h4>
        <div style="text-align: center;">
          <img :src="qrCodeImgUrl" />
        </div>
      </div>
    </Modal>
  </div>
</template>

<script>
import Common from '../common/Common.js';
import { Notify, Uuid } from 'pc-component-v3';
import WorkflowResource from '../api/workflow/WorkflowResource.js';
import FileResource from '../api/dic/FileResource.js';

export default {
  components: {},

  data: function () {
    return {
      taskInfo: null,
      pdfSrc : null,
      trainVideo: null,
      qrCodeModal: false,
    };
  },

  mounted: function () {
    this.pdfSrc = this.$route.query.pdfSrc;
    this.trainVideo = this.$route.query.trainVideo;
    this.uniqueTaskInfo();
  },

  methods: {
    openPdf: function(){
      window.open(this.pdfSrc);
    },
    /**
     * 查询任务的信息
     */
    uniqueTaskInfo: function(){
      let _self = this;
      let taskId = this.$route.query.taskId;
      if(taskId == null){
        Notify.error(_self.$t('lang.Notify.error'), _self.$t('lang.PdfPrint.missingTaskId'), true);
        return;
      }

      WorkflowResource.uniqueTaskInfoByTaskIdUserId(taskId).then(successData => {
        if (successData.errorCode == 0) {
          _self.taskInfo = successData.data;
        }else{
          _self.taskInfo = null;
          Notify.error(_self.$t('lang.Notify.error'), successData.errorMessage, true);
        }
        
      }, errorData => {
        Common.processException(errorData);
      });
    },

    /**
     * 完成任务
     */
    finishTask: function(){
      let _self = this;
      let taskId = this.$route.query.taskId;
      if(taskId == null){
        Notify.error(_self.$t('lang.Notify.error'), _self.$t('lang.PdfPrint.missingTaskId'), true);
        return;
      }

      WorkflowResource.finishTask(taskId).then(successData => {
        if(successData.errorCode == 0){
          let content = Common.getHostPageBaseURL() + '/app.html';
          _self.qrCodeImgUrl = '/api/BarcodeResource/getQRCode?content=' + content;
          _self.qrCodeModal = true;
        }else{
          Notify.error(_self.$t('lang.Notify.error'), successData.errorMessage, true);
        }
      }, errorData => {
        Common.processException(errorData);
      });
    },

    /**
     * 学习培训视频
     */
    startTrain: function(){
      if(this.trainVideo !== null){
        window.open(this.trainVideo);
      }
    },

  },
};
</script>
<style scoped>
</style>
