
import WorkflowResource from '../api/workflow/WorkflowResource.js';
import Common from '../common/Common.js';
import { Uuid} from 'pc-component-v3';
import i18n  from '../locales/locale.js';
const  t  = i18n.global.t;

export default {

  /**
     * 打开任务
     */
  openTask: function(taskInfo){
    var _self = this;
    var windowNo = taskInfo.windowNo;
    var tabIndex = taskInfo.tabIndex;
    var recordId = taskInfo.recordId;
    
    return new Promise((resolve, reject) => {
      // 1. 首先判断category, category 如果等于 'CurdWindow' 说明是 CURD 窗口，否则是自定义的窗口
      // 2. 判断formKey是否存在
      // 2.1 如果存在那么就直接打开
      // 2.2 如果不存在那么，判断formKeyApp是否存在，如果不存在，那么表示本任务存在异常，如果存在，表示该任务需要在APP上执行，那么进行正确的提示。
      if(taskInfo.category === 'CurdWindow' || taskInfo.category != 'MandatoryTask'){
        if(taskInfo.formKey === null || taskInfo.formKey === ''){
          WorkflowResource.canEditByWorkflow(null, windowNo, taskInfo.id).then(successData => {
            let url = null;
            let type = null;
            if(successData == false){
              type = 'view';
              url = '/desktop/window1/window-read/'
                          + type + '/'
                          + windowNo + '/'
                          + tabIndex + '/'
                          + recordId
                          + '?workflowType=approve&taskInfoId=' + taskInfo.id
                          + '&currIndex=1&totalCount=1&canGoBack=false&uuid=' + Uuid.createUUID()
                          + '&title=' + taskInfo.title
                          + '&subTitle=' + taskInfo.description;
            }else{
              type = 'edit';
              url = '/desktop/window1/window-edit/'
                          + type + '/'
                          + windowNo + '/'
                          + tabIndex + '/'
                          + recordId
                          + '?workflowType=approve&taskInfoId=' + taskInfo.id
                          + '&currIndex=1&totalCount=1&canGoBack=false&uuid=' + Uuid.createUUID()
                          + '&title=' + taskInfo.title
                          + '&subTitle=' + taskInfo.description;
            }
  
            let newUrl = Common.getRedirectUrl('#' + url);
            resolve({
              type: 'newWindow',
              url: newUrl,
            });
          }, errorData => {
            Common.processException(errorData);
            reject();
          });
        }else{
          let newUrl = null;
          if(taskInfo.formKey.indexOf('?') > 0){
            newUrl = taskInfo.formKey + '&taskId=' + taskInfo.id + '&recordId=' + taskInfo.recordId
                + '&title=' + taskInfo.title
                + '&subTitle=' + taskInfo.description;
          }else{
            newUrl = taskInfo.formKey + '?taskId=' + taskInfo.id + '&recordId=' + taskInfo.recordId
                + '&title=' + taskInfo.title
                + '&subTitle=' + taskInfo.description;
          }
          resolve({
            type: 'newWindow',
            url: newUrl,
          });
        }
      }else{
        if(taskInfo.formKey === null || taskInfo.formKey === ''){
          if(taskInfo.formKeyApp !== null && taskInfo.formKeyApp !== ''){
            reject({
              title: t('lang.taskOpenUtil.error'),
              message: t('lang.taskOpenUtil.operateOnPhone'),
            });
          }else{
            reject({
              title: t('lang.taskOpenUtil.error'),
              message: t('lang.taskOpenUtil.taskDefineError'),
            });
          }
        } else {
          if(taskInfo.formKey[0] === '/'){
            let formKeys = taskInfo.formKey.split('|');
            if(formKeys != null && formKeys.length > 1){
              taskInfo.formKey = formKeys[0];
            }
            if(taskInfo.formKey.indexOf('/static-eam-app/') > -1){
              reject({
                title: t('lang.taskOpenUtil.error'),
                message: t('lang.taskOpenUtil.operateOnPhone'),
              });
            }
            let newUrl = null;
            if(taskInfo.formKey.indexOf('?') > 0){
              newUrl = taskInfo.formKey + '&taskId=' + taskInfo.id + '&recordId=' + taskInfo.recordId
                + '&title=' + taskInfo.title
                + '&subTitle=' + taskInfo.description;
            }else{
              newUrl = taskInfo.formKey + '?taskId=' + taskInfo.id + '&recordId=' + taskInfo.recordId
                + '&title=' + taskInfo.title
                + '&subTitle=' + taskInfo.description;
            }
            resolve({
              type: 'newWindow',
              url: newUrl,
            });
          }else{
            resolve({
              type: 'customerTask',
              taskId: taskInfo.id,
            });
          }
        }
      }
    });
  },
  /**
     * 打开历史任务
     */
  openHistoryTask: function(historyTaskInfo){
    var _self = this;
    var windowNo = historyTaskInfo.windowNo;
    var tabIndex = historyTaskInfo.tabIndex;
    var recordId = historyTaskInfo.recordId;
    var type = 'view';
    // 1. 首先判断category, category 如果等于 'CurdWindow' 说明是 CURD 窗口，否则是自定义的窗口
    // 2. 判断formKey是否存在
    // 2.1 如果存在那么就直接打开
    // 2.2 如果不存在那么，判断formKeyApp是否存在，如果不存在，那么表示本任务存在异常，如果存在，表示该任务需要在APP上执行，那么进行正确的提示。
    
    return new Promise((resolve, reject) => {
      if(historyTaskInfo.category === 'CurdWindow'  || historyTaskInfo.category != 'MandatoryTask'){
        if(historyTaskInfo.formKey === null || historyTaskInfo.formKey === ''){
          let newUrl = '/#/desktop/window1/window-read/'
                    + type + '/'
                    + windowNo + '/'
                    + tabIndex + '/'
                    + recordId
                    + '?workflowType=approve&taskInfoId=' + historyTaskInfo.id
                    + '&currIndex=1&totalCount=1&canGoBack=false&uuid=' + Uuid.createUUID();
          resolve({
            type: 'newWindow',
            url: newUrl,
          });
        }else{
          let newUrl = null;
          if(historyTaskInfo.formKey.indexOf('?') > 0){
            newUrl = historyTaskInfo.formKey + '&taskId=' + historyTaskInfo.id + '&recordId=' + historyTaskInfo.recordId
                + '&title=' + historyTaskInfo.title
                + '&subTitle=' + historyTaskInfo.description;
          }else{
            newUrl = historyTaskInfo.formKey + '?taskId=' + historyTaskInfo.id + '&recordId=' + historyTaskInfo.recordId
                + '&title=' + historyTaskInfo.title
                + '&subTitle=' + historyTaskInfo.description;
          }
          resolve({
            type: 'newWindow',
            url: newUrl,
          });
        }
      }else{
        if(historyTaskInfo.formKey === null || historyTaskInfo.formKey === ''){
          if(historyTaskInfo.formKeyApp !== null && historyTaskInfo.formKeyApp !== ''){
            reject({
              title: t('lang.taskOpenUtil.error'),
              message: t('lang.taskOpenUtil.operateOnPhone'),
            });
          }else{
            reject({
              title: t('lang.taskOpenUtil.error'),
              message: t('lang.taskOpenUtil.taskDefineError'),
            });
          }
        } else {
          if(historyTaskInfo.formKey[0] === '/'){
            let formKeys = historyTaskInfo.formKey.split('|');
            if(formKeys != null && formKeys.length > 1){
              historyTaskInfo.formKey = formKeys[0];
            }
            if(historyTaskInfo.formKey.indexOf('/static-eam-app/') > -1){
              reject({
                title: t('lang.taskOpenUtil.error'),
                message: t('lang.taskOpenUtil.operateOnPhone'),
              });
            }
            let newUrl = null;
            if(historyTaskInfo.formKey.indexOf('?') > 0){
              newUrl = historyTaskInfo.formKey + '&taskId=' + historyTaskInfo.id + '&recordId=' + historyTaskInfo.recordId
                + '&title=' + historyTaskInfo.title
                + '&subTitle=' + historyTaskInfo.description;
            }else{
              newUrl = historyTaskInfo.formKey + '?taskId=' + historyTaskInfo.id + '&recordId=' + historyTaskInfo.recordId
                + '&title=' + historyTaskInfo.title
                + '&subTitle=' + historyTaskInfo.description;
            }
            resolve({
              type: 'newWindow',
              url: newUrl,
            });
          }else{
            resolve({
              type: 'customerTask',
              taskId: historyTaskInfo.id,
            });
          }
        }
      }
    });
  },



  
  /**
     * 打开抄送任务
     */
  openCopyTask: function(copyTask){
    var _self = this;
    var windowNo = copyTask.windowNo;
    var tabIndex = copyTask.tabIndex;
    var recordId = copyTask.recordId;
    var type = 'view';
    // 1. 首先判断category, category 如果等于 'CurdWindow' 说明是 CURD 窗口，否则是自定义的窗口
    // 2. 判断formKey是否存在
    // 2.1 如果存在那么就直接打开
    // 2.2 如果不存在那么，判断formKeyApp是否存在，如果不存在，那么表示本任务存在异常，如果存在，表示该任务需要在APP上执行，那么进行正确的提示。
    
    return new Promise((resolve, reject) => {
      if(copyTask.category === 'CurdWindow' || copyTask.category != 'MandatoryTask'){
        if(copyTask.formKey === null || copyTask.formKey === ''){
          let newUrl = '/#/desktop/window1/window-read/'
                    + type + '/'
                    + windowNo + '/'
                    + tabIndex + '/'
                    + recordId
                    + '?workflowType=approve&taskInfoId=' + copyTask.id
                    + '&currIndex=1&totalCount=1&canGoBack=false&uuid=' + Uuid.createUUID();
          resolve({
            type: 'newWindow',
            url: newUrl,
          });
        }else{
          let newUrl = null;
          if(copyTask.formKey.indexOf('?') > 0){
            newUrl = copyTask.formKey + '&taskId=' + copyTask.id + '&recordId=' + copyTask.recordId
                + '&title=' + copyTask.title
                + '&subTitle=' + copyTask.description;
          }else{
            newUrl = copyTask.formKey + '?taskId=' + copyTask.id + '&recordId=' + copyTask.recordId
                + '&title=' + copyTask.title
                + '&subTitle=' + copyTask.description;
          }
          resolve({
            type: 'newWindow',
            url: newUrl,
          });
        }
      }else{
        if(copyTask.formKey === null || copyTask.formKey === ''){
          if(copyTask.formKeyApp !== null && copyTask.formKeyApp !== ''){
            reject({
              title: t('lang.taskOpenUtil.error'),
              message: t('lang.taskOpenUtil.operateOnPhone'),
            });
          }else{
            reject({
              title: t('lang.taskOpenUtil.error'),
              message: t('lang.taskOpenUtil.taskDefineError'),
            });
          }
        } else {
          if(copyTask.formKey[0] === '/'){
            let formKeys = copyTask.formKey.split('|');
            if(formKeys != null && formKeys.length > 1){
              copyTask.formKey = formKeys[0];
            }
            if(copyTask.formKey.indexOf('/static-eam-app/') > -1){
              reject({
                title: t('lang.taskOpenUtil.error'),
                message: t('lang.taskOpenUtil.operateOnPhone'),
              });
            }
            let newUrl = null;
            if(copyTask.formKey.indexOf('?') > 0){
              newUrl = copyTask.formKey + '&taskId=' + copyTask.id + '&recordId=' + copyTask.recordId
                + '&title=' + copyTask.title
                + '&subTitle=' + copyTask.description;
            }else{
              newUrl = copyTask.formKey + '?taskId=' + copyTask.id + '&recordId=' + copyTask.recordId
                + '&title=' + copyTask.title
                + '&subTitle=' + copyTask.description;
            }
            resolve({
              type: 'newWindow',
              url: newUrl,
            });
          }else{
            resolve({
              type: 'customerTask',
              taskId: copyTask.id,
            });
          }
        }
      }
    });
  },


};