<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    :centered="true"
    title="驳回"
    destroyOnClose 
    width="50%"
    @cancel="handleCancel"
    :helpMessage="['帮助']"
    @ok="saveReject"
  >
    <BasicForm @register="registerForm" style="margin: 10px" />
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref, reactive,nextTick } from 'vue'
  import { useForm, BasicForm, FormSchema } from '/@/components/Form'
  import { BasicModal, useModalInner } from '/@/components/Modal'
  import { rejectTask, getNextFlowNode} from "/@/components/Process/api/todo"

  const checkSendUser = ref()
  const checkType = ref()
  const checkSendRole = ref()
  const multiInstanceVars = ref()

  const flowTodoTask = ref({})
  const [register, { closeModal, setModalProps }] = useModalInner(async (data) => {
    const params = { taskId: data.data.taskId }
    flowTodoTask.value = data.data
    //alert(JSON.stringify(flowTodoTask.value))
    //alert(JSON.stringify(data))
    await setNextNode(params)
  });


  const formSchemas: FormSchema[] = [
    {
      label: '驳回原因',
      field: 'comment',
      component:'InputTextArea',
      required:true,
      componentProps:{
        allowClear: true,
        showCount: true,
        autoSize:{
          minRows: 6,
          maxRows: 10
        },
      }
    }
  ];

  const [registerForm, { setProps, validate }] = useForm({
    schemas: formSchemas,
    showActionButtonGroup: false,
    
  });

  const handleCancel = ()=> {
    closeModal();
  }

  const handleSetModalProps = ()=> {
    setModalProps({
      title:"流程审批"
    })
  }

  const setNextNode = async (params) => {
    
    getNextFlowNode(params).then((res) => {
      if(res==null) {
        closeModal();
      }
      const data = res.data
      if (data) {
        if (data.dataType === 'dynamic') {
          if (data.type === 'assignee') {
            // 指定人员
            checkSendUser.value = true
            checkType.value = 'single'
          } else if (data.type === 'candidateUsers') {
            // 候选人员(多个)
            checkSendUser.value = true
            checkType.value = 'multiple'
          } else if (data.type === 'candidateGroups') {
            // 指定组(所属角色接收任务)
            checkSendRole.value = true
          } else {
            // 会签
            // 流程设计指定的 elementVariable 作为会签人员列表
            multiInstanceVars.value = data.vars
            checkSendUser.value = true
            checkType.value = 'multiple'
          }
        }
      }
    })
  }


  const saveReject = async () => {

      const formDataTmp = await validate();
     // alert(flowTodoTask.value.procInsId)
      formDataTmp["procInsId"] = flowTodoTask.value.procInsId
      formDataTmp["deployId"] = flowTodoTask.value.deployId
      formDataTmp["taskId"] = flowTodoTask.value.taskId

      await rejectTask(formDataTmp).then(res => {
        closeModal()
      });
      
  }

</script>

<style scoped>
</style>
