<template>
  <Modal v-model:show="showModal" :show-ok-button="false" :show-canel-button="false" :large="true" :force="true" :show-top-right-close-button="false">
    <template #header>
      {{ selectedTask == null ? "" : selectedTask.name == '培训-审批' ? $t("lang.LoginNotice.describe1") : 
        selectedTask.name == '确认隐私声明' ? $t("lang.LoginNotice.describe2") :
        selectedTask.name == '固定资产知识培训' ? $t("lang.LoginNotice.describe3") :
        selectedTask.name == '重置密码' ? $t("lang.LoginNotice.describe4") :
        selectedTask.name == '部门经理填写部门、存放地点的资产管理员' ? $t("lang.LoginNotice.describe5") : selectedTask.name }}
    </template>
    <div>
      <component :is="currentComponent" @finish-task="finishTask" @close="closeModal" />
    </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,
  },

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

  mounted: function(){
    let _self = this;
    _self.fecthTask();
  },

  methods: {
    /**
     * 获取任务
     */
    fecthTask:function(){
      let _self = this;
      WorkflowResource.listByAssigneeIdCategory('NoticeOnLogin').then(successData => {
        if(successData.datas != null && successData.datas.length > 0){
          _self.selectedTask = successData.datas[0];
          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;
        }
      }, errorData => {
        console.error(errorData);
      });
    },

    closeModal: function(){
      let _self = this;
      _self.showModal = false;
    },
    /**
     * 完成任务
     */
    finishTask: function(){
      let _self = this;
      if(_self.selectedTask == null){
        Notify.error(_self.$t('lang.Notify.prompt'), _self.$t('lang.Notify.pleaseSelectTaskFirst'), true);
        return;
      }

      WorkflowResource.finishTask(_self.selectedTask.id).then(successData => {
        if(successData.errorCode == 0){
          // 继续获取下一个任务
          _self.fecthTask();
        }else{
          Notify.error(_self.$t('lang.Notify.prompt'), successData.errorMessage, true);
          _self.fecthTask();
        }
      }, errorData => {
        Common.processException(errorData);
      });
    },
  },
};

</script>