<template>
  <!-- <a-row :gutter="24">
  <a-col :md="24" :sm="24">-->
  <span>
    <span
      v-if="Boolean(processData.isSuspended)"
      style="cursor: no-drop;color: #999999;"
      title="流程已被挂起，无法操作！"
    >
      通过
      <a-divider type="vertical" />驳回
      <a-divider type="vertical" />委托
      <a-divider type="vertical" />
    </span>
    <span v-else>
      <a-button style="margin-left: 8px;" type="primary" @click="passTask(processData)">通过</a-button>
      <a-button style="margin-left: 8px;" type="primary" @click="backTask(processData)">驳回</a-button>
      <a-button style="margin-left: 8px;" type="primary" @click="delegateTask(processData)">委托</a-button>
      <!-- <a href="javascript:void(0);" @click="passTask(processData)" style="color: green">通过</a>
      <a-divider type="vertical" />
      <a href="javascript:void(0);" @click="backTask(processData)" style="color: orange">驳回</a>
      <a-divider type="vertical" />
      <a href="javascript:void(0);" @click="delegateTask(processData)" style="color: #00A0E9">委托</a>
      <a-divider type="vertical" />-->
    </span>
    <!-- 审批操作 -->
    <a-modal :title="modalTaskTitle" v-model="modalTaskVisible" :mask-closable="false" :width="500">
      <div v-if="modalTaskVisible">
        <a-form ref="form" :model="form" :label-width="85" :rules="formValidate">
          <a-form-item label="审批意见" prop="reason">
            <a-input type="textarea" v-model="form.comment" :rows="4" />
          </a-form-item>
          <a-form-item label="下一审批人" prop="assignees" v-show="showAssign" :error="error">
            <a-select
              v-model="form.assignees"
              placeholder="请选择"
              allowClear
              mode="multiple"
              :loading="userLoading"
            >
              <a-select-option
                v-for="(item, i) in assigneeList"
                :key="i"
                :value="item.username"
              >{{item.realname}}</a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item label="下一审批人" v-show="isGateway">
            <span>分支网关处暂不支持自定义选择下一审批人，将发送给下一节点所有人</span>
          </a-form-item>
          <div v-show="form.type==1">
            <a-form-item label="驳回至">
              <a-select v-model="form.backTaskKey" :loading="backLoading" @change="changeBackTask">
                <a-select-option
                  v-for="(item, i) in backList"
                  :key="i"
                  :value="item.key"
                >{{item.name}}</a-select-option>
              </a-select>
            </a-form-item>
            <a-form-item
              label="指定原节点审批人"
              prop="assignees"
              v-show="form.backTaskKey!=-1"
              :error="error"
            >
              <a-select
                v-model="form.assignees"
                placeholder="请选择"
                allowClear
                mode="multiple"
                :loading="userLoading"
              >
                <a-select-option
                  v-for="(item, i) in assigneeList"
                  :key="i"
                  :value="item.id"
                >{{item.username}}</a-select-option>
              </a-select>
            </a-form-item>
          </div>
          <a-form-item label="选择委托人" prop="userId" :error="error" v-show="form.type==2">
            <JSelectUserByDep v-model="form.userId" :multi="false"></JSelectUserByDep>
          </a-form-item>
          <a-form-item label="消息通知">
            <a-checkbox v-model="form.sendMessage">站内消息通知</a-checkbox>
            <a-checkbox v-model="form.sendSms" disabled>短信通知</a-checkbox>
            <a-checkbox v-model="form.sendEmail" disabled>邮件通知</a-checkbox>
          </a-form-item>
        </a-form>
      </div>
      <div slot="footer">
        <a-button type="text" @click="modalTaskVisible=false">取消</a-button>
        <a-button type="primary" :loading="submitLoading" @click="handelSubmit">提交</a-button>
      </div>
    </a-modal>
  </span>
  <!-- </a-col> -->

  <!-- </a-row> -->
</template>

<script>
import { getAction, postFormAction } from '@/api/manage'
import JSelectUserByDep from '@/components/jeecgbiz/JSelectUserByDep'
import todoManage from '../todoManage'
export default {
  name: 'todoManageOperation',
  components: { JSelectUserByDep, todoManage },
  props: {
    /*流程数据*/
    processData: {
      type: Object,
      default: () => {
        return {}
      },
      required: false
    },
    todoManageOperationObject: {
      type: Object,
      default: () => {
        return {}
      }
    },
    disabled: false,
    btndisabled: false
  },
  data() {
    return {
      url: {
        todoList: '/actTask/todoList',
        pass: '/actTask/pass',
        back: '/actTask/back',
        backToTask: '/actTask/backToTask',
        delegate: '/actTask/delegate',
        getNextNode: '/activiti_process/getNextNode',
        getNode: '/activiti_process/getNode/',
        getBackList: '/actTask/getBackList/',
        passAll: '/actTask/passAll/',
        backAll: '/actTask/backAll/'
      },
      form: {
        id: '',
        userId: '',
        procInstId: '',
        comment: '',
        type: 0,
        assignees: [],
        backTaskKey: '-1',
        sendMessage: true,
        sendSms: false,
        sendEmail: false
      },
      modalTaskVisible: false,
      modalTaskTitle: '',
      formValidate: {
        // 表单验证规则
      },
      showAssign: false,
      userLoading: false,
      assigneeList: [],
      isGateway: false,
      backLoading: false,
      backList: [
        {
          key: '-1',
          name: '发起人'
        }
      ],
      error: '',
      submitLoading: false // 添加或编辑提交状态
    }
  },
  methods: {
    passTask(v) {
      //调用表单提交表单的保存方法
      //  console.log(this.todoManageOperationObject.isSave)
      //  console.log(this.disabled)
      //  console.log(this.btndisabled)
      //  let isPassTask=false;//是否可以通过
      //  //表单是否需要保存
      //  if(this.disabled==false&&this.btndisabled==false){
      //    //需要保存 那么是否已经点击保存
      //    if(this.todoManageOperationObject.isSave){
      //      isPassTask=true;
      //    }
      //  }else{//不需要保存直接可以通过
      //    isPassTask=true;
      //  }
      // if(isPassTask==false){
      //   this.$message.error("请先保存表单数据！")
      //   return;
      // }

      this.modalTaskTitle = '审批通过'
      this.form.id = v.id
      this.form.procInstId = v.procInstId
      this.form.priority = v.priority
      this.form.type = 0
      this.modalTaskVisible = true
      this.userLoading = true
      getAction(this.url.getNextNode, { procDefId: v.procDefId, currActId: v.key }).then(res => {
        this.userLoading = false
        if (res.success) {
          if (res.result.type == 3 || res.result.type == 4) {
            this.isGateway = true
            this.showAssign = false
            this.error = ''
            return
          }
          this.isGateway = false
          if (res.result.users && res.result.users.length > 0) {
            this.error = ''
            this.assigneeList = res.result.users
            // 默认勾选
            let ids = []
            res.result.users.forEach(e => {
              ids.push(e.username)
            })
            this.form.assignees = ids
            this.showAssign = true
          } else {
            this.form.assignees = []
            this.showAssign = false
          }
        }
      })
    },
    changeBackTask(v) {
      if (v == '-1') {
        return
      }
      this.userLoading = true
      getAction(this.url.getNode + v).then(res => {
        this.userLoading = false
        if (res.success) {
          if (res.result.users && res.result.users.length > 0) {
            this.assigneeList = res.result.users
            // 默认勾选
            let ids = []
            res.result.users.forEach(e => {
              ids.push(e.username)
            })
            this.form.assignees = ids
          }
        }
      })
    },
    backTask(v) {
      this.modalTaskTitle = '审批驳回'
      this.form.id = v.id
      this.form.procInstId = v.procInstId
      this.form.procDefId = v.procDefId
      this.form.priority = v.priority
      this.form.type = 1
      this.showAssign = false
      this.modalTaskVisible = true
      // 获取可驳回节点
      this.backList = [
        {
          key: '-1',
          name: '发起人'
        }
      ]
      this.form.backTaskKey = '-1'
      this.backLoading = true
      getAction(this.url.getBackList + v.procInstId).then(res => {
        this.backLoading = false
        if (res.success) {
          res.result.forEach(e => {
            this.backList.push(e)
          })
        }
      })
    },
    delegateTask(v) {
      this.modalTaskTitle = '委托他人代办'
      this.form.id = v.id
      this.form.procInstId = v.procInstId
      this.form.type = 2
      this.showAssign = false
      this.modalTaskVisible = true
    },
    async handelSubmit() {
      console.log('提交')
      this.submitLoading = true
      var formData = Object.assign({}, this.form)
      formData.assignees = formData.assignees.join(',')
      if (formData.type == 0) {
        // 通过
        if (this.showAssign && formData.assignees.length < 1) {
          this.$message.error('请至少选择一个审批人')
          this.submitLoading = false
          return
        } else {
          this.error = ''
        }
        //是否需要保存表单
        if (this.disabled == false && this.btndisabled == false) {
          //调用保存按钮
          await this.$emit('handleSubmit2');
          //保存
          this.postFormAction(this.todoManageOperationObject.url, this.todoManageOperationObject.formData).then(res => {
            if (res.success) {
              this.$message.success('保存成功！')
              //通过
              postFormAction(this.url.pass, formData).then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success('操作成功')
                  this.modalTaskVisible = false
                  //刷新父组件数据
                  this.$emit('refreshToDo')
                }
              })
            } else {
              this.$message.error(res.message)
              // resolve(false);
              this.modalTaskVisible = false
            }
          })
        } else {//不需要保存直接通过
          //通过
          postFormAction(this.url.pass, formData).then(res => {
            this.submitLoading = false
            if (res.success) {
              this.$message.success('操作成功')
              this.modalTaskVisible = false
              //刷新父组件数据
              this.$emit('refreshToDo')
            }
          })
        }
      } else if (formData.type == 1) {
        // 驳回
        if (formData.backTaskKey == '-1') {
          // 驳回至发起人
          postFormAction(this.url.back, formData).then(res => {
            this.submitLoading = false
            if (res.success) {
              this.$message.success('操作成功')
              this.modalTaskVisible = false
              //刷新父组件数据
              this.$emit('refreshToDo')
            }
          })
        } else {
          // 自定义驳回
          if (formData.backTaskKey != '-1' && formData.assignees.length < 1) {
            this.$message.error('请至少选择一个审批人')
            this.submitLoading = false
            return
          } else {
            this.error = ''
          }
          postFormAction(this.url.backToTask, formData).then(res => {
            this.submitLoading = false
            if (res.success) {
              this.$message.success('操作成功')
              this.modalTaskVisible = false
              //刷新父组件数据
              this.$emit('refreshToDo')
            }
          })
        }
      } else if (formData.type == 2) {
        // 委托
        if (!formData.userId) {
          this.$message.error('请选择一委托人')
          this.submitLoading = false
          return
        } else {
          this.error = ''
        }
        postFormAction(this.url.delegate, formData).then(res => {
          this.submitLoading = false
          if (res.success) {
            this.$message.success('操作成功')
            this.modalTaskVisible = false
            //刷新父组件数据
            this.$emit('refreshToDo')
          }
        })
      }
    }
  }
}
</script>
