<template>
  <div class="aos-flow">
    <div class="aos-flow__body">
      <div v-if="['countersign', 'return-countersign', 'hangup', 'turnto'].indexOf(actionType) < 0" class="aos-flow__type">
        <div class="aos-flow__part" v-if="actionType === 'reject' && showReject && rejectActivitys.length > 0">
          <span class="aos-flow__message-title">退回至</span>
        </div>
        <div class="aos-flow__part aos-flow__part-message" v-if="actionType === 'reject' && showReject && rejectActivitys.length > 0">
          <div>
            <el-select v-model="rejectNode" class="aos-flow-reject-group" clearable>
              <el-option class="aos-flow-reject-radio" v-for="item in rejectActivitys" :key="item.activityId" :value="item.activityId" :label="item.activityName"></el-option>
            </el-select>
          </div>
        </div>
        <div class="aos-flow__part">
          <span class="aos-flow__flow-message-title">审批意见</span>
        </div>
        <div class="aos-flow__part aos-flow__part-message">
          <el-input type="textarea" :autosize="{minRows:2}" v-model="outMessage" placeholder="请在此输入审批意见"></el-input>
        </div>
        <div class="aos-flow__part" v-if="showInnerMessage">
          <span class="aos-flow__flow-message-title">内部意见</span>
        </div>
        <div class="aos-flow__part aos-flow__part-message" v-if="showInnerMessage">
          <el-input type="textarea" :autosize="{minRows:2}" v-model="message"  placeholder="请在此输入内部意见"></el-input>
        </div>
        <div class="aos-flow__part" v-if="showConditionMessage && actionType !== 'reject'">
          <span class="aos-flow__flow-message-title">有条件通过意见</span>
        </div>
        <div class="aos-flow__part aos-flow__part-message" v-if="showConditionMessage && actionType !== 'reject'">
          <el-input type="textarea" :autosize="{minRows:2}" v-model="conditionMessage"  placeholder="请在此输入有条件通过意见"></el-input>
        </div>
        <div class="aos-flow__images aos-flow__part-message">
          <!-- <div class="aos-flow__image-box" v-for="(item,index) in filesList" :key="index" @mouseout="handleImageMouseOut(index)" @mouseover="handleImageMouseOver(index)">
            <img :src="'/bpm/api/task/file-download/'+ taskId + '/'+ item.id" alt="流程附件图片">
            <span v-show="activeItemIndex === index">
              <i class="el-icon-search aos-flow__icon" title="查看" @click="handleViewImage(item)"></i>
              <a class="aos-flow__icon" title="下载" :href="'/bpm/api/task/file-download/'+ taskId + '/'+ item.id"  @click="handleDownload(item)">
                <i class="el-icon-download"></i>
              </a>
              <i class="el-icon-delete aos-flow__icon" title="删除" @click="handleDeleteImage(item)"></i>
            </span>
          </div> -->
          <el-upload
            drag
            multiple
            class="aos-flow__upload"
            :file-list="filesList"
            :action='"/bpm/api/task/upload/"+taskId'
            :on-success="handleUploadSuccess"
            :before-remove="handleBeforeRemove">
            <i class="el-icon-upload" style="font-size: 40px;margin:0;"></i>
            <div class="el-upload__text" style="font-size: 12px;margin:0 8px;">将文件拖到此处或<em>点击上传</em></div>
          </el-upload>
        </div>
      </div>
      <div v-if="actionType === 'countersign'" class="aos-flow__type aos-flow__countersign">
        <div class="aos-flow__part">
          <span class="aos-flow__message-title">加签说明</span>
        </div>
        <div class="aos-flow__part aos-flow__part-message">
          <el-input type="textarea" :autosize="{minRows:2}" v-model="countersignMessage" placeholder="请在此输入加签说明"></el-input>
        </div>
        <div class="aos-flow__part">
          <span class="aos-flow__message-title">加签人</span>
        </div>
        <div class="aos-flow__part aos-flow__part-message">
          <el-select></el-select>
        </div>
      </div>
      <div v-if="actionType === 'return-countersign'" class="aos-flow__type aos-flow__return-countersign">
        <div class="aos-flow__part">
          <span class="aos-flow__message-title">回复加签说明</span>
        </div>
        <div class="aos-flow__part aos-flow__part-message">
          <el-input type="textarea" :autosize="{minRows:2}" v-model="returnCountersignMessage" placeholder="请在此输入回复加签说明"></el-input>
        </div>
      </div>
      <div v-if="actionType === 'hangup'" class="aos-flow__type aos-flow__hangup">
        <div class="aos-flow__part">
          <span class="aos-flow__message-title">挂起原因</span>
        </div>
        <div class="aos-flow__part aos-flow__part-message">
          <el-input type="textarea" :autosize="{minRows:2}" v-model="hangUpMessage" placeholder="请在此输入挂起原因"></el-input>
        </div>
      </div>
      <div v-if="actionType === 'turnto'" class="aos-flow__type aos-flow__turnto">
        <div class="aos-flow__part">
          <span class="aos-flow__message-title">转办说明</span>
        </div>
        <div class="aos-flow__part aos-flow__part-message">
          <el-input type="textarea" :autosize="{minRows:2}" v-model="turntoMessage" placeholder="请在此输入转办说明"></el-input>
        </div>
        <div class="aos-flow__part">
          <span class="aos-flow__message-title">转办人</span>
        </div>
        <div class="aos-flow__part aos-flow__part-message">
          <el-select v-model="turnToUser" placeholder="请选择">
            <el-option
              v-for="item in turnTowardUsers"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </div>
      </div>
    </div>
    <div class="aos-flow__footer">
      <div v-if="['countersign', 'return-countersign', 'hangup', 'turnto', 'reject'].indexOf(actionType) < 0">
        <el-button type="success" @click="agreeEvent" :loading="loading">通过</el-button>
        <!-- <el-button type="success" @click="handleBack">返回</el-button> -->
        <el-button type="warning" class="aos-flow__footer-button" :loading="loading" v-if="showReject" @click="rejectEvent">退回</el-button>
        <el-button class="aos-flow__footer-button" :loading="loading" v-if="!showReject && showTurnto" @click="handleTurnTo">转办</el-button>
        <el-dropdown class="aos-flow__footer-button" type="primary" trigger="click" @command="handleMoreCommand">
          <el-button>
            更多<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item v-if="showReject && showTurnto" command="turnto">转办</el-dropdown-item>
            <el-dropdown-item command="refuse" v-if="showRefuse" :disabled="loading">拒绝</el-dropdown-item>
            <!-- <el-dropdown-item command="countersign">加签</el-dropdown-item> -->
            <el-dropdown-item command="hangup">挂起</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
      <div class="aos-flow__footer-inline" v-else>
        <el-button @click="handleInlineBack">返回</el-button>
        <el-button class="aos-flow__footer-button" type="primary" :loading="loading" @click="handleInlineSubmit">确定</el-button>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'AosFlow',
  props: {
    // 表单状态判断,true为表单已处理好,false为表单未处理好
    operation: {
      type: Boolean
    },
    // 表单未处理好的提示信息
    operationMessage: {
      type: String,
      default: '当前表单未处理'
    },
    // 是否显示操作按钮
    disabled: {
      type: Boolean,
      default: false
    },
    // 是否显示意见框
    showMessage: {
      type: Boolean,
      default: false
    },
    // 点击操作之前
    beforeAction: {
      type: Function,
      default: function() {return true}
    },
    // 点击同意操作之前
    agreeBeforeAction: {
      type: Function,
      default: function() {return true}
    },
    rejectBeforeAction: {
      type: Function,
      default: function(){return true}
    },
    refuseBeforeAction: {
      type: Function,
      default: function(){return true}
    },
    // 点击提交按钮之前触发事件
    submitBeforeAction: {
      type: Function,
      default: function(){return true}
    },
    // 同意时是否走agreeBeforeAction方法
    agreeBefore: {
      type: Boolean,
      default: false
    },
    // // 驳回是否走rejectBeforeAction方法
    // rejectBefore: {
    //   type: Boolean,
    //   default: false
    // },
    // // 拒绝是否走refuseBeforeAction方法
    // refuseBefore: {
    //   type: Boolean,
    //   default: false
    // },
    // 提交时是否走submitBeforeAgree方法
    submitBefore: {
      type: Boolean,
      default: false
    },
    // 返回按钮返回的URL,默认返回任务中心
    backRouter: {
      type: String,
      default: '/bpm/task'
    },
    // 提交确认成功之后跳转的页面
    confirmBackRouter: {
      type: String,
      default: '/bpm/task'
    },
    // 是否显示返回按钮
    showBackBtn: {
      type: Boolean,
      default: true
    },
    // 显示的风格,horizontal:水平 vertical: 垂直
    showType:{
      type: String,
      default: 'horizontal'
    },
    // agreeBeforeAction方法:false同步请求,true有异步请求
    agreeAsycHttp: {
      type: Boolean,
      default: false
    },
    // 控制refuseBeforeAction方法为同步还是异步： 
    refuseAsyncHttp: {
      type: Boolean,
      default: false
    },
    rejectAsyncHttp: {
      type: Boolean,
      default: false
    },
    // 是否自动触发提交,true为自动提交
    autoOperate:{
      type: Boolean,
      default: true
    },
    // 是否自动触发驳回
    autoReject: {
      type: Boolean,
      default: true
    },
    // 是否自动触发拒绝
    autoRefuse: {
      type: Boolean,
      default: true
    },
    // 是否水平版 自动拒绝
    autoHorizontalRefuse: {
      type: Boolean,
      default: true
    },
    // 水平版拒绝前的校验函数
    beforeHorizantalRefuseAction: {
      type: Function,
      default: function(){
        return true
      }
    },
    // 是否水平版 自动驳回
    autoHorizontalReject: {
      type: Boolean,
      default: true
    },
    // 水平版驳回前的校验函数
    beforeHorizantalRejectAction: {
      type: Function,
      default: function(){
        return true
      }
    },
    autoSubmit: {
      type: Boolean,
      default: true
    },
    // 弹框 点击确认前事件
    beforeSubmitAction: {
      type: Function,
      default: function(){
        return true
      }
    },
   
  },
  data(){
    return {
      showFlow: false, // 是否显示流程审批按钮
      active: 0,
      showReject: false, //是否显示驳回按钮
      showRefuse: false, // 是否显示拒绝按钮
      rejectActivitys: '', // 驳回信息(包含环节等)
      refuseReasons: [], // 拒绝原因
      rejectReasons: [], // 驳回原因
      completeReasons: [], // 通过原因
      completeGroupReasons: {}, // 通过的组原因
      refuseGroupReasons: {}, // 通过的组原因
      rejectGroupReasons: {}, // 通过的组原因
      reasonList: [], //原因
      reasonGroupObject: {}, //组原因
      taskId: '', // 当前任务id
      processInstanceId: '', //流程id
      startEndMessage: '', // 审批开始结束信息
      showHistory: false, // 显示历史信息
      taskDetail: '', // 任务详细
      historyDetailList: '', // 历史审批意见,
      translateHistoryDetailList: '', // 转换过类型的历史审批意见
      messageTitle: '', //意见dailog名称
      operationType: true, // 操作的类型,true为通过,false为驳回
      // showMessage: false, // 是否显示意见dialog 改为双向数据绑定
      message: '', // 内部意见
      conditionMessage: '', //有条件通过意见
      outMessage: '', //外部意见
      reason:'', // 选择的原因
      rejectNode: '', //驳回节点
      auditRecord: '', // 审批意见
      messageSuccess: false, // 通过或拒绝是否成功
      approvalCheckedType: '',
      rejectType:'',
      showApprovalMessage: false, // 切换审批和审批意见,默认为审批
      approvalType: '', // 当前审批类型
      reverse: false,
      dialogImageUrl: '', // 图片的url
      dialogVisible: false, // 图片预览
      filesList: [],
      activeItemIndex: '',
      moreActionList: [
        {
          label: '转办',
          key: 'turnto',
        },
        {
          label: '驳回',
          key: 'reject',
        },
        {
          lable: '加签',
          key: 'countersign'
        },
        {
          label: '挂起',
          key: 'hangup'
        }
      ],
      actionType: '',
      hangUpMessage: '',
      turntoMessage: '',
      returnCountersignMessage: '',
      countersignMessage: '',
      showTurnto: false,
      turnTowardUsers: [],
      turnToUser: '',
      // 是否显示内部意见
      showInnerMessage: false,
      // 是否显示有条件通过意见
      showConditionMessage: false,
      loading: false
    }
  },
  methods: {
    handleInlineBack () {
      this.actionType = ''
      this.hangUpMessage = ''
      this.turntoMessage = ''
      this.returnCountersignMessage = ''
      this.countersignMessage = ''
    },
    handleMoreCommand (commond) {
      this.actionType = commond
      if (commond === 'refuse') {
        this.refuseEvent()
      }
    },
    handleTurnTo () {
      this.actionType = 'turnto'
    },
    handleInlineSubmit () {
      if (this.actionType === 'hangup') {
        this.handleHangUp()
      } else if (this.actionType ==='reject') {
        this.handleRejectEvent()
      } else if (this.actionType ==='turnto') {
        this.turnToEvent()
      }
    },
    handleRejectEvent() {
      this.handleApprovalCheckChange(1)
      if(!this.rejectNode){
        this.$message({
          type:'error',
          message: '请选择退回环节'
        })
        return;
      }
      this.handleVerticalSubmitMessage()
    },
    handleHangUp () {
      this.$confirm('确认挂起？', '提示').then(async () => {
        try {
          const submitData = {
            reason: this.hangUpMessage,
            taskId: this.taskId,
            userId: '',
            auditRecord: '',
            innerRemind: '',
            remind: ''
          }
          this.loading = true
          await this.hangup(submitData)
          this.loading = false
          this.$message({
            type: 'success',
            message: '挂起成功'
          })
          this.$emit('hangup')
          this.$utils.removeCurrentTab(this.$route)
          this.handleEmitRefresh()
          this.$router.push(this.backRouter)
        } catch (error) {
          console.error(error)
          this.loading = false
        }
      })
    },
    hangup (data) {
      return this.$http({
        url: '/bpm/api/task-suspendeda-ctivate',
        method: 'post',
        data
      }).then(res => {
        return res
      })
    },
    turnToEvent () {
      this.$confirm('确认转办？', '提示').then(async () => {
        try {
          const submitData = {
            reason: this.turntoMessage,
            taskId: this.taskId,
            auditRecord: '',
            innerRemind: '',
            remind: '',
            userId: '',
            turnTowardUserId: this.turnToUser
          }
          this.loading = true
          await this.turnto(submitData)
          this.$message({
            type: 'success',
            message: '转办成功'
          })
          this.loading = false
          this.$emit('turnto')
          this.$utils.removeCurrentTab(this.$route)
          this.handleEmitRefresh()
          this.$router.push(this.backRouter)
        } catch (error) {
          console.error(error)
          this.loading = false
        }
      })
    },
    turnto (data) {
      return this.$http({
        url: '/bpm/api/task-turn-toward',
        method: 'post',
        data
      }).then(res => {
        return res
      })
    },
    getImageList(){
      this.$http({
        url: '/bpm/api/task/files/' +this.taskId,
        method: 'get'
      }).then((res) => {
        this.filesList = res.data;
        if (this.filesList && this.filesList.length > 0) {
          this.filesList.forEach(item => {
            const message = JSON.parse(item.message)
            const url = '/bpm/api/task/file-download/'+ this.taskId + '/'+ item.id
            item.name = message.fileName + message.fileSuffix
            item.url = url
          })
        }
      }).catch(() => {
        this.$message({
          type: 'error',
          message: '获取流程附件出错'
        })
      });
    },
    // 图片删除
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    // 图片预览
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleUploadSuccess(response, file, fileLis){
      this.getImageList()
    },
    handleOnRemove (file) {
      return this.$http({
        url: '/bpm/api/task/file-delete/' + this.taskId + '/' + file.id,
        method: 'post'
      }).then((res) => {
        return res
      })
    },
    handleBeforeRemove (file) {
      return new Promise((resolve,reject) => {
        this.$confirm('确认删除当前附件？','删除提示',{
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(async () => {
          try {
            await this.handleOnRemove(file)
            this.getImageList();
            this.$message({
              type: 'success',
              message: '删除流程附件成功'
            })
            resolve(true)
          } catch (error) {
            reject(false)
            this.$message({
              type: 'error',
              message: '删除流程附件出错'
            })
          }
        }).catch(() => {
          reject(false)
        })
      })
    },
    handleImageMouseOut(index){
      this.activeItemIndex = '';
    },
    handleImageMouseOver(index){
      this.activeItemIndex = index;
    },
    handleViewImage(item){
      this.dialogVisible = true;
      this.dialogImageUrl = '/bpm/api/task/file-download/' + this.taskId + '/' + item.id;
    },
    handleViewTimeLineImage(url){
      this.dialogVisible = true;
      this.dialogImageUrl = url;
    },
    handleDownload(item){
      let a = document.createElement('a');
      if(typeof a.download === 'undefined'){
        let newUrl = window.location.protocol + "//" + window.location.host + '/bpm/api/task/file-download/'+ this.taskId + '/'+ item.id;
        window.open(newUrl);
      }
    },
    handleDeleteImage(item){
      this.$confirm('确认删除当前附件？','删除提示',{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.$http({
          url: '/bpm/api/task/file-delete/' + this.taskId + '/' + item.id,
          method: 'delete'
        }).then((res) => {
          this.getImageList();
        }).catch(() => {
          this.$message({
            type: 'error',
            message: '删除流程附件出错'
          })
        });
      })
    },
    // 切换到审批
    changeType(){
      this.showApprovalMessage = false;
    },
    // 原因选择 
    handleReasonChange(val){
      this.reason = val;
    },
    // 获取驳回是否显示按钮,以及驳回的id
    getConfig(){
      this.$http({
        method: 'get',
        url: '/bpm/api/task-opconfig/' + this.taskId
      }).then((res) => {
        if(res.status === 200){
          const resData = res.data
          if(resData.refuseAble){
            this.showRefuse = true;
          }
          if(resData.reasonAble){
            this.showConditionMessage = true;
          }
          if(resData.innerRemindAble){
            this.showInnerMessage = true;
          }
          this.rejectReasons = resData.rejectReasons;
          if(resData.completeReasons){
            this.completeReasons = resData.completeReasons;
          }
          if(resData.completeGroupReasons){
            this.completeGroupReasons = resData.completeGroupReasons;
          }
          if(resData.refuseReasons){
            this.refuseReasons = resData.refuseReasons;
          }
          if(resData.refuseGroupReasons){
            this.refuseGroupReasons = resData.refuseGroupReasons;
          }
          if(resData.canRejectNodes){
            this.rejectActivitys = resData.canRejectNodes;
            if (this.rejectActivitys && this.rejectActivitys.length > 0) {
              this.rejectNode = this.rejectActivitys[0].activityId
            }
            this.showReject = true
          } else {
            this.showReject = false;
          }
          if(resData.rejectGroupReasons){
            this.rejectGroupReasons = resData.rejectGroupReasons;
          }
          if (resData.canTurnTowardUsers) {
            this.showTurnto = true
            this.turnTowardUsers = resData.canTurnTowardUsers
          } else {
            this.showTurnto = false
          }
        }
      }).catch(() => {
        this.$message({
          type: 'error',
          message: '请求流程按钮配置出错'
        })
      });
    },
   
    // 获取任务详细
    getTaskDetail(){
      this.$http({
        method: 'get',
        url: '/bpm/api/task/info/' + this.taskId
      }).then((res) => {
        if(res.data){
          this.taskDetail = res.data
        }
      }).catch(() => {
        this.$message({
          type: 'error',
          message: '请求任务详细出错'
        })
      });
    },
    getTaskId(){
      if(this.$route.query && this.$route.query.taskId){
        this.taskId = this.$route.query.taskId;
        this.showFlow = true;
        this.getConfig();
        // this.getTaskDetail();
      }
      if(this.$route.query && this.$route.query.processInstanceId){
        this.processInstanceId = this.$route.query.processInstanceId
      }
    },
    handleAgreeBeforeAction() {
      if (this.operation) {
        if (this.showType === 'horizontal') {
          this.$emit('update:showMessage', true);
        }
      } else {
        this.$message({
          type: 'info',
          message: this.operationMessage
        })
      }
      if (!!this.agreeBeforeAction && this.agreeAsycHttp) {
        return this.agreeBeforeAction;
      } else {
        if (!this.agreeBeforeAction()) {
          return false;
        }
        return true;
      }
    },
    handleBeforeAction(){
      if(!this.beforeAction()){
        return false;
      }
      if(this.operation){
        if(this.showType === 'horizontal'){
          this.$emit('update:showMessage',true);
        }
      } else {
        this.$message({
          type: 'info',
          message: this.operationMessage
        })
      }
      return true;
    },
    agreeEvent () {
      this.handleApprovalCheckChange(0)
      this.handleVerticalSubmitMessage()
    },
    refuseEvent () {
      this.handleApprovalCheckChange(2)
      this.handleVerticalSubmitMessage()
    },
    rejectEvent () {
      this.actionType = 'reject'
      // this.handleApprovalCheckChange(1)
      // if(!this.rejectNode){
      //   this.$message({
      //     type:'error',
      //     message: '请选择退回环节'
      //   })
      //   return;
      // }
      // this.handleVerticalSubmitMessage()
    },
    // 选择的审批类型: 0:通过,1:驳回,2:拒绝
    handleApprovalCheckChange(label){
      this.approvalCheckedType = label;
      if(label === 0){
        this.handleAgree();
      } else if(label === 1){
        this.handleReject();
      } else if(label === 2){
        this.handleRefuse();
      }
      // this.reason = '';
      // this.auditRecord = '';
      // this.rejectNode = '';
      this.$emit('approval-change',label);
    },
    // 点击通过按钮
    handleAgree(){
      this.approvalType = 'agree';
      if(this.showType === 'horizontal'){
        this.$emit("change",'submit');
       if(this.submitBefore){
          this.submitBeforeAction();
        } else {
          this.handleBeforeAction()
        }
      } else {
        this.$emit("change",'agree');
        this.reasonList = this.completeReasons;
        this.reasonGroupObject = this.completeGroupReasons;
        this.operationType = true;
      }
    },
    // 点击退回驳回按钮
    handleReject(){
       this.approvalType = 'reject';
      if(this.showType === 'horizontal'){
        this.$emit("change",'back');
        // if(this.handleBeforeAction()){
          this.reasonList = this.rejectReasons;
          this.reasonGroupObject = this.rejectGroupReasons
          this.messageTitle = "退回";
          this.operationType = false;
          this.$emit('update:showMessage',true);
          // this.handleSubmitMessage();
        // }
      } else {
        this.$emit("change",'reject');
        this.reasonList = this.rejectReasons;
        this.reasonGroupObject = this.rejectGroupReasons
        this.messageTitle = "驳回";
        this.operationType = false;
      }
    },
    // 点击拒绝按钮
    handleRefuse(){
      this.approvalType = 'refuse';
      this.$emit("change",'refuse');
      if(this.showType === 'horizontal'){
        if(this.handleBeforeAction()){
          this.reasonList = this.refuseReasons;
          this.reasonGroupObject = this.refuseGroupReasons
          this.messageTitle = "拒绝";
          // this.$emit('update:showMessage',true);
        }
    } else {
      this.reasonList = this.refuseReasons;
      this.reasonGroupObject = this.refuseGroupReasons
    }
    },
    handleBack(){
      this.$utils.removeCurrentTab(this.$route)
      this.handleEmitRefresh()
      this.$router.push(this.confirmBackRouter);
    },
    handleEmitRefresh() {
      this.$eventBus.emit('refresh')
    },
    // 关闭意见
    handleCloseMessage(){
      // this.$confirm('确认关闭?','关闭',{
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning',
      // }).then(() => {
        this.$emit('update:showMessage',false);
      // });
    },
    handleVerticalSubmitMessage(){
      if((this.reasonGroupObject && Object.keys(this.reasonGroupObject).length > 0) || (this.reasonList && this.reasonList.length > 0)){
        let message;
        if(this.approvalType === 'agree'){
          message = '请选择通过原因';
        } else if(this.approvalType === 'reject'){
          message = '请选择驳回原因';
        }else if(this.approvalType === 'refuse'){
          message = '请选择拒绝原因';
        }
        if(!this.reason){
          this.$message({
            type: 'error',
            message: message
          });
          return;
        }
      }
      // if(!this.outMessage){
      //     this.$message({
      //       type: 'error',
      //       message: '请输入审批意见'
      //     });
      //     return;
      //   }
      // if(!this.message){
      //   this.$message({
      //     type: 'error',
      //     message: '请输入内部意见'
      //   });
      //   return;
      // }
      if(this.agreeBefore && this.approvalType === 'agree'){
        if(this.autoOperate){
          if(this.agreeAsycHttp){
            this.agreeBeforeAction().then((valid) => {
              if(valid){
                this.handleSubmitMessage();
              }
            })
          }else {
            if(this.handleAgreeBeforeAction()){
              this.handleSubmitMessage();
            }
          }
        } else {
          this.handleAgreeBeforeAction();
        }
      }
      if(this.approvalType === 'reject'){
        if(this.autoReject){
          if (this.rejectAsyncHttp) {
            this.rejectBeforeAction().then(valid => {
              if (valid) {
                this.handleSubmitMessage();
              }
            })
          } else {
            if(this.rejectBeforeAction()) {
              this.handleSubmitMessage();
            }
          }
       } else {
         this.rejectBeforeAction()
       }
      }
      if(this.approvalType === 'refuse'){
       if(this.autoRefuse){
        if (this.refuseAsyncHttp) {
          this.refuseBeforeAction().then(valid => {
            if (valid) {
              this.handleSubmitMessage();
            }
          })
        } else {
          if(this.refuseBeforeAction()){
            this.handleSubmitMessage();
          }
        }
        } else {
          this.refuseBeforeAction()
        }
      }
    },
    handleHorizontalSubmitMessage(){
      if(this.approvalType === 'agree'){
        if(!this.autoSubmit){
          this.beforeSubmitAction()
        } else {
          this.handleSubmitMessage();
        }
      }
      if(this.approvalType === 'refuse'){
        if(!this.autoHorizontalRefuse){
          this.beforeHorizantalRefuseAction()
        } else {
          this.handleSubmitMessage();
        }
      }
      if(this.approvalType === 'reject'){
        if(!this.autoHorizontalReject){
          this.beforeHorizantalRejectAction()
        } else {
          this.handleSubmitMessage();
        }
      } 
    },
    handleSubmitMessage(){
      this.loading = true
      let submit = this.submitMessage();
      window.Promise.all([submit]).then(() => {
        this.loading = false
        this.$emit('after-action', this.messageSuccess);
        if (this.messageSuccess) {
          this.$utils.removeCurrentTab(this.$route)
          this.handleEmitRefresh()
          this.$router.push(this.backRouter);
        }
      }).catch((error) => {
        console.log(error);
        this.loading = false
      })
    },
    // 意见提交,operationType: true 通过
    submitMessage(){
      let url;
      let submitData = {};
      // let success = false;
      if (this.showType === 'horizontal') {
        if (this.approvalType === 'agree') {
          url = '/bpm/api/task-complete';
          submitData = {
            auditRecord: this.auditRecord,
            innerRemind: this.message,
            remind: this.outMessage,
            reason: this.conditionMessage,
            taskId: this.taskId
          }
        }else if(this.approvalType === 'reject'){
          url = '/bpm/api/task-reject';
          submitData = {
            targetActivityId: this.rejectActivitys[0].activityId,
            remind: this.outMessage,
            taskId: this.taskId
          }
        } else if(this.approvalType === 'refuse'){
          url = '/bpm/api/task-refuse';
          submitData = {
            remind: this.outMessage,
            taskId: this.taskId
          }
        }
      }else {
        if((this.reasonGroupObject && Object.keys(this.reasonGroupObject).length > 0) || (this.reasonList && this.reasonList.length > 0)){
          let message;
          if(this.approvalType === 'agree'){
            message = '请选择通过原因';
          } else if(this.approvalType === 'reject'){
            message = '请选择驳回原因';
          }else if(this.approvalType === 'refuse'){
            message = '请选择拒绝原因';
          }
          if(!this.reason){
            this.$message({
              type: 'error',
              message: message
            });
            return;
          }
        }
        // if(!this.message){
        //   this.$message({
        //     type: 'error',
        //     message: '请输入内部意见'
        //   });
        //   return;
        // }
        // if(!this.outMessage){
        //   this.$message({
        //     type: 'error',
        //     message: '请输入外部意见'
        //   });
        //   return;
        // }
        if(this.approvalType === 'agree'){
          url = '/bpm/api/task-complete';
          submitData = {
            auditRecord: this.auditRecord,
            innerRemind: this.message,
            remind: this.outMessage,
            reason: this.conditionMessage,
            taskId: this.taskId
          }
        }else if(this.approvalType === 'reject'){
          url = '/bpm/api/task-reject';
          submitData = {
            auditRecord: this.auditRecord,
            innerRemind: this.message,
            remind: this.outMessage,
            targetActivityId: this.rejectNode,
            reason: this.reason,
            taskId: this.taskId,
          }
        } else if(this.approvalType === 'refuse'){
          url = '/bpm/api/task-refuse';
          submitData = {
            auditRecord: this.auditRecord,
            innerRemind: this.message,
            remind: this.outMessage,
            reason: this.reason,
            taskId: this.taskId,
          }
        }
      }
        
      return this.$http({
        method: 'post',
        url: url,
        data: submitData
      }).then((res) => {
        if(res.status === 200){
          let successMessage = '';
          if(this.approvalType === 'agree'){
            if(this.showType === 'horizontal'){
              successMessage = "提交成功";
            } else {
              successMessage = "通过成功";
            }
          } else if(this.approvalType === 'reject') {
            if(this.showType === 'horizontal'){
              successMessage = "退回成功";
              this.$emit('update:showMessage',false);
            } else {
              successMessage = "驳回成功";
            }
          } else {
            successMessage = "拒绝成功";
          }
           this.$message({
            type: 'success',
            message: successMessage
          })
          this.messageSuccess = true;
        }
      }).catch(() => {
        let errorMessage = '';
        if(this.approvalType === 'agree'){
          if(this.showType === 'horizontal'){
            errorMessage = "提交失败";
          } else {
            errorMessage = "通过失败";
          }
        } else if(this.approvalType === 'reject'){
          if(this.showType === 'horizontal'){
            errorMessage = "退回失败";
          } else {
            errorMessage = "驳回失败";
          }
        } else {
          errorMessage = "拒绝失败";
        }
        this.$message({
          type: 'error',
          message: errorMessage
        })
        this.messageSuccess = false;
      });
      
    },
  },
  created() {
    this.getTaskId();
    this.getImageList();
  }
}
</script>