<template>
  <Modal v-model:show="showModal" :show-ok-button="false" :show-canel-button="false" :large="true">
    <template #header> {{ selectedTask != null ? selectedTask.title : "" }} </template>
    <div>
      <component :is="currentComponent" @finish-task="finishTask" />
    </div>
    <template #footer />
  </Modal>
</template>


<script>

import Common from '../common/Common.js';
import PrivacyStatement from '../confirm/privacy-statement.vue';
import StudyVideo from '../confirm/StudyVideo.vue';
import ResetPassword from '../confirm/ResetPassword.vue';
import KnowledgeTrain from '../confirm/KnowledgeTrain1.vue';
import WorkflowResource from '../api/workflow/WorkflowResource.js';
import { Notify, Uuid } from 'pc-component-v3';

export default {

  components: {
    PrivacyStatement,
    StudyVideo,
    ResetPassword,
    KnowledgeTrain,
  },

  props: {
    taskId: {
      type: String,
      default: null,
    },
  },

  data: function () {
    return {
      selectedTask: null,
      currentComponent: null,
      showModal: false,
    };
  },

  watch: {
    taskId: {
      handler: function(newValue, oldValue){
        this.fecthTask();
      },
      immediate: false,
    },
  },


  methods: {
    show: function(){
      this.fecthTask();
      this.showModal = true;
    },
    /**
     * 获取任务
     */
    fecthTask:function(){
      let _self = this;
      if(this.taskId == null){
        _self.selectedTask = null;
        _self.currentComponent = null;
        _self.showModal = false;
        _self.$emit('closed');
        return;
      }
      WorkflowResource.uniqueTaskInfoByTaskIdUserId(this.taskId).then(successData => {
        if(successData != null){
          _self.selectedTask = successData;
          if(_self.selectedTask.formKey == 'ConfirmPrivacy'){
            _self.currentComponent = 'PrivacyStatement';
          }else if(_self.selectedTask.formKey == 'TrainApprove'){
            _self.currentComponent = 'StudyVideo';
          }else if(_self.selectedTask.formKey == 'ResetPassword'){
            _self.currentComponent = 'ResetPassword';
          }else if(_self.selectedTask.formKey == 'KnowledgeTrain'){
            _self.currentComponent = 'KnowledgeTrain';
          }
          _self.showModal = true;
        }else{
          _self.selectedTask = null;
          _self.currentComponent = null;
          _self.showModal = false;
          _self.$emit('closed');
        }
      }, errorData => {
        console.error(errorData);
      });
    },

    /**
     * 完成任务
     */
    finishTask: function(){
      let _self = this;
      if(_self.selectedTask == null){
        Notify.error('错误', '请先选择任务。', true);
        return;
      }

      WorkflowResource.finishTask(_self.selectedTask.id).then(successData => {
        if(successData.errorCode == 0){
          _self.showModal = false;
          _self.$emit('closed');
        }else{
          Notify.error('错误', successData.errorMessage, true);
        }
      }, errorData => {
        Common.processException(errorData);
      });
    },
  },
};

</script>