<template>
  <div>
    <el-tabs v-model="activeName" type="border-card" @tab-click="handleTabsClick">
      <el-tab-pane label="业务详情" name="business">
        <component :is="currentComponent"></component>

      </el-tab-pane>
      <el-tab-pane label="审批流程" name="process">
        <h3>审批历史</h3>
        <el-card :body-style="cardBodyStyle">
          <el-table :data="tableDataHistory" border highlight-current-row>
            <el-table-column prop="actionname" label="操作名称" width="200" header-align="center" align="center"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="actiontime" label="操作时间" width="200" header-align="center" align="center"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="comments" label="审批意见" width="200" header-align="center" align="center"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="nodename" label="节点名称" width="200" header-align="center" align="center"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="username" label="用户名" width="200" header-align="center" align="center"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="memo" label="备注" header-align="center" align="center"
                             show-overflow-tooltip>
            </el-table-column>
          </el-table>
        </el-card>
        <h3 v-if="type === '1' || type === '3'">审批意见</h3>
        <el-card :body-style="cardBodyStyle" v-if="type === '1' || type === '3'">
          <el-input type="textarea" :rows="4" placeholder="请输入审批意见" v-model="comment"></el-input>
        </el-card>
        <h3 v-if="type !== '0' && flowInfo.flowButtons !== ''">操作</h3>
        <el-card :body-style="cardBodyStyle" v-if="type !== '0' && flowInfo.flowButtons !== ''">
          <el-button v-for="item in flowInfo.flowButtons"
                     :key="item.buttonId"
                     :type="buttonType(item.buttonId)"
                     v-show="shouldShowButton(item.buttonId)"
                     @click="handleClick(item)">
            {{buttonName(item.buttonId)}}
          </el-button>
        </el-card>

        <el-dialog :close-on-click-modal="false" draggable title="请选择人员" v-model="dialogVisibleUser" width="1200px"
                   @open="openDialog">

          <el-row :gutter="20" style="min-height: 400px; margin-top: -50px">
            <el-col :span="8">
              <H4>组织结构</H4>
              <el-card :body-style="cardBodyStyle">
                <H4 style="margin-top: -4px;">部门</H4>
                <el-tree :data="dataTree" :props="defaultProps" node-key="id" @node-click="handleNodeClick"
                         class="tree-container" :expand-on-click-node="false" :default-expanded-keys="expandedKeys"  style="margin-top: -8px; ">
                  <template #default="{ node, data }">
                    <span class="custom-tree-node"> {{ node.label }} </span>
                  </template>
                </el-tree>
                <H4>岗位</H4>
                <el-tree :data="dataPost" :props="defaultPropsPost" node-key="id" @node-click="handleNodeClickPost"
                         class="tree-container" :expand-on-click-node="false" :default-expanded-keys="expandedKeysPost"  style="margin-top: -8px; ">
                  <template #default="{ node, data }">
                    <span class="custom-tree-node"> {{ node.label }} </span>
                  </template>
                </el-tree>

              </el-card>
            </el-col>

            <el-col :span="8">
              <H4>请选择</H4>
              <el-card :body-style="cardBodyStyle">
                <el-table :data="tableDataUserDept" border @select="handleSelectionChange">
                  <el-table-column type="selection" fixed></el-table-column>
                  <el-table-column prop="id" label="id" show-overflow-tooltip v-if="isShow">
                  </el-table-column>
                  <el-table-column prop="user.userName" sortable label="用户名" show-overflow-tooltip
                                   header-align="center" align="center">
                  </el-table-column>
                  <el-table-column prop="user.userDesc" sortable label="姓名" show-overflow-tooltip
                                   header-align="center" align="center">
                  </el-table-column>
                </el-table>
              </el-card>
            </el-col>

            <el-col :span="8">
              <H4>已选择</H4>
              <el-card :body-style="cardBodyStyle">
                <el-table :data="tableDataUser" border>
                  <el-table-column prop="id" label="id" show-overflow-tooltip v-if="isShow">
                  </el-table-column>
                  <el-table-column prop="user.userName" sortable label="用户名" show-overflow-tooltip
                                   header-align="center" align="center">
                  </el-table-column>
                  <el-table-column prop="user.userDesc" sortable label="姓名" show-overflow-tooltip
                                   header-align="center" align="center">
                  </el-table-column>

                  <el-table-column header-align="center" align="center" width="60px">
                    <template #default="scope">
                      <el-button type="danger" size="small" :icon="Delete" circle @click="handleDeleteUserData(scope.row )"/>
                    </template>
                  </el-table-column>

                </el-table>
              </el-card>
            </el-col>
          </el-row>

          <template #footer>
            <div class="dialog-footer">
              <el-button type="success" @click="handleSave()">确认</el-button>
              <el-button type="danger" @click="handleCancelSave">取消</el-button>
            </div>
          </template>
        </el-dialog>

        <el-dialog :close-on-click-modal="false" draggable title="请选择节点" v-model="dialogVisibleNode" width="800px"
                   @open="openDialog">

          <div>
            <el-radio-group v-model="radioNode" size="large" @change="onRadioChange">
              <el-radio-button v-for="item in radioNodes" :label="item.nodeId" :value="item.nodeId" >{{item.nodeName}}</el-radio-button>
            </el-radio-group>
          </div>

          <template #footer>
            <div class="dialog-footer">
              <el-button type="success" @click="handleSaveNode()">确认</el-button>
              <el-button type="danger" @click="handleCancelSaveNode">取消</el-button>
            </div>
          </template>
        </el-dialog>


      </el-tab-pane>
      <el-tab-pane label="流程图" name="image">
        <iframe :src="urlFrame" width="100%" height="600px" ref="iframe"></iframe>
      </el-tab-pane>
    </el-tabs>

  </div>
</template>

<script setup>
import {
  ref,
  reactive,
  onMounted,
  onActivated,
  getCurrentInstance,
  nextTick,
  defineAsyncComponent
} from 'vue'
const {
  proxy
} = getCurrentInstance()
import {
  useRoute
} from 'vue-router';
const route = useRoute();

const multipleSelection = ref([])
const handleSelectionChange = (val) => {
  console.log(val)
  let repeat = false

  for(let i = 0; i < val.length; i++) {
    console.log(val[i].user.id)
    repeat = false
    for(let j = 0; j < multipleSelection.value.length; j++) {
      if(multipleSelection.value[j].user.id === val[i].user.id) {
        repeat = true
        break
      }
    }
    if(!repeat){
      multipleSelection.value.push(val[i])
    }
  }

  for(let i = 0; i < val.length; i++) {
    repeat = false
    for(let j = 0; j < tableDataUser.value.length; j++) {
      if(tableDataUser.value[j].user.id === val[i].user.id) {
        repeat = true
        break
      }
    }
    if(!repeat){
      tableDataUser.value.push(val[i])
    }
  }

}

const buttonName = (buttonId) => {
  switch (buttonId) {
    case 'submit':
      return '同意';
    case 'reject':
      return '退回';
    case 'jumpreject':
      return 'warning';
    case 'jumprejectstart':
      return '驳回';
    case 'stop':
      return '终止';
    case 'move':
      return '转办';
    case 'getback':
      return '拿回';
    default:
      return 'default'; // 或者返回null/undefined，但Element UI的el-button有默认样式
  }
};

// 一个用于根据name返回button type的函数
const buttonType = (buttonId) => {
  switch (buttonId) {
    case 'submit':
      return 'success';
    case 'reject':
      return 'default';
    case 'jumpreject':
      return 'warning';
    case 'jumprejectstart':
      return 'warning';
    case 'stop':
      return 'danger';
    case 'move':
      return 'primary';
    case 'getback':
      return 'primary';
    default:
      return 'default'; // 或者返回null/undefined，但Element UI的el-button有默认样式
  }
};
const shouldShowButton = (buttonId) => {
  return buttonId === 'submit'
      || buttonId === 'reject'
      // || buttonId === 'jumpreject'
      || buttonId === 'jumprejectstart'
      || buttonId === 'stop'
      || buttonId === 'getback'
      || buttonId === 'move';
};

import style from '@/tools/style'
const cardBodyStyle = ref(style.card)

const dialogVisibleUser = ref(false)
const dialogVisibleNode = ref(false)
const radioNode = ref('')
const radioNodeValue = ref('')

const radioNodes = ref([])
const radioNodeSelect = ref([])

const openDialog = () => {
}

const defaultProps = ref({
  children: 'children',
  label: 'deptName'
})

const expandedKeys = ref([])
const dataTree = ref([])
const selectedDept = ref({
  id: '',
  deptCode: ''
})

const onRadioChange =  (data) => {
  console.log(data)
  radioNodeValue.value = data

}

const handleNodeClick = (data) => {
  selectedDept.value = data
  getTableDataUserDept();
}

const getDataDeptTree = () => {
  if(deptIds.value === ''){
    var url = 'framework/SysDepartment/queryTree'
    var param = {}
    proxy.$axios.get(url, param).then(response => {
      dataTree.value = response.data.data
      if (dataTree.value.length === 1) {
        for (var node of dataTree.value) {
          expandedKeys.value.push(node.id)
        }
        console.log(dataTree.value)
      }
    });
  }else{
    var url = 'framework/SysDepartment/queryByIds'
    var param = {
      ids: deptIds.value
    }
    proxy.$axios.get(url, param).then(response => {
      if (response.data.code == '0') {
        // var children = []
        // children.push(response.data.data)
        dataTree.value = [{
          id: '10010',
          deptCode: 'root',
          deptName: '部门',
          children: response.data.data
        }]
        for (var node of dataTree.value) {
          expandedKeys.value.push(node.id)
        }
      }
    });
  }
}

const defaultPropsPost = ref({
  children: 'children',
  label: 'postName'
})

const expandedKeysPost = ref([])
const dataPost = ref([])
const selectedPost = ref({
  id: '',
  postCode: ''
})

const getDataPost = () => {
  var url = 'framework/SysPost/queryByIds'
  var param = {
    page: 1,
    rows: 40,
    ids: postIds.value
  }
  proxy.$axios.get(url, param).then(response => {
    if (response.data.code == '0') {

      dataPost.value = [{
        id: '10086',
        postCode: 'root',
        postName: '岗位',
        children: response.data.data
      }]
      for (var node of dataPost.value) {
        expandedKeysPost.value.push(node.id)
      }
    }
  });
}

const handleNodeClickPost = (data) => {
  selectedPost.value = data
  getTableDataUserPost();
}

const getTableDataUserPost = () => {
  var url = 'framework/SysUserPost/queryByPostId'
  var param = {
    postId: selectedPost.value.id
  }
  proxy.$axios.get(url, param).then(response => {
    tableDataUserDept.value = response.data.data
  });
}


const tableDataUserDept = ref([])

const getTableDataUserDept = () => {
  var url = 'framework/SysUserDept/queryByDeptId'
  var param = {
    deptId: selectedDept.value.id
  }
  proxy.$axios.get(url, param).then(response => {
    tableDataUserDept.value = response.data.data
  })
}

const tableDataUser = ref([])

const handleDeleteUserData = (row) => {

  for (let i=0; i < multipleSelection.value.length; i++){
    if (multipleSelection.value[i].id === row.id){
      multipleSelection.value.splice(i, 1)
    }
  }

  for (let i=0; i < tableDataUser.value.length; i++){
    if (tableDataUser.value[i].id === row.id){
      tableDataUser.value.splice(i, 1)
    }
  }
}


const handleSave = () => {
  handleConfirmSubmit(nextNodeName.value)
  dialogVisibleUser.value = false
}

const handleCancelSave = () => {
  dialogVisibleUser.value = false
}

const handleSaveNode = () => {
  // handleConfirmSubmit()
  radioNodeSelect.value = []
  console.log(radioNodeValue.value)
  for(let i=0; i < radioNodes.value.length; i++){
    if(radioNodes.value[i].nodeId === radioNodeValue.value){
      radioNodes.value[i].isSelect = "true"
      radioNodeSelect.value.push(radioNodes.value[i])
    }
  }

  nextNodeList.value = radioNodeSelect.value
  console.log(nextNodeList.value)
  handlePreSubmit()
  dialogVisibleNode.value = false
}

const handleCancelSaveNode = () => {
  dialogVisibleNode.value = false
}

onMounted(() => {
  // flowId.value = route.query.flowId
  // workId.value = route.query.workId
  // trackId.value = route.query.trackId
  // subjectionId.value = route.query.subjectionId
  // type.value = route.query.type
  const workflowQuery = JSON.parse(sessionStorage.getItem('workflowQuery'))
  flowId.value = workflowQuery.flowId
  workId.value = workflowQuery.workId
  trackId.value = workflowQuery.trackId
  subjectionId.value = workflowQuery.subjectionId
  type.value = workflowQuery.type
  if(type.value === undefined || type.value === null){
    type.value = '0'
  }
  openWorkflow()
})

onActivated(() => {
  // flowId.value = route.query.flowId
  // workId.value = route.query.workId
  // trackId.value = route.query.trackId
  // subjectionId.value = route.query.subjectionId
  // type.value = route.query.type
  const workflowQuery = JSON.parse(sessionStorage.getItem('workflowQuery'))
  flowId.value = workflowQuery.flowId
  workId.value = workflowQuery.workId
  trackId.value = workflowQuery.trackId
  subjectionId.value = workflowQuery.subjectionId
  type.value = workflowQuery.type
  if (type.value === undefined || type.value === null) {
    type.value = '0'
  }
  openWorkflow()
  // getBusinessPage()

})

// const loginName = ref(localStorage.getItem('userName'))
const handleTabsClick = (tab, event, item) => {
  console.log(tab.props.name)
  if (tab.props.name == 'process') {
    getHistory()
  } else if (tab.props.name == 'image') {
    urlFrame.value =
        `${import.meta.env.VITE_WORKFLOW_ADDR}` + '/horizon-workflow-boot/workflow/module/designer/index.html?workid=' + workId
            .value + '&showtype=2&dataid=' + workId.value
  }
}

const activeName = ref('business')
const flowId = ref('')
const workId = ref('')
const trackId = ref('')
const subjectionId = ref('')
const type = ref('')

const nextNodeList = ref([])

const urlFrame = ref('')

const currentComponent = ref('')
const tableDataHistory = ref([])

const getHistory = () => {
  var url = 'workflow/process/history'
  var param = {
    workId: workId.value
  }
  proxy.$axios.post(url, param).then(response => {
    if (response.data.code == '0') {
      tableDataHistory.value = response.data.data.records
    } else {
      ElMessage.error('操作失败！' + response.data.msg)
    }
  })
}

const getBusinessPage = () => {
  var url = 'framework/BusinessProcess/queryBusinessPageByFlowId'
  var param = {
    // processDefinitionKey: flowId.value,
    // tableName: tableName.value
    flowId: flowId.value
  }
  console.log('flowId:::' + JSON.stringify(flowId.value))
  proxy.$axios.post(url, param).then(response => {
    if (response.data.code == '0') {
      console.log('getBusinessPage:::' + JSON.stringify(response.data.data))

      const modules = import.meta.glob('../**/*.vue')
      currentComponent.value = defineAsyncComponent(modules[`..${response.data.data.pathWeb}.vue`])

      // currentComponent.value = defineAsyncComponent(() =>
      // 	import(`..${response.data.data.pathWeb}.vue`)
      // );


    } else {
      ElMessage.error('操作失败！' + response.data.msg)
    }
  })
}

const flowInfo = ref({})
const businessId = ref('')
const tableName = ref('')
const nextNodeName = ref('')

const postIds = ref('')

const deptIds = ref('')

const userIds = ref('')

import {
  useWorkflowStore
} from "@/stores/workflow"
import {ElMessage, ElMessageBox} from "element-plus";
import {Delete} from "@element-plus/icons-vue";

const workflow = useWorkflowStore()

const openWorkflow = () => {
  var url = 'workflow/task/open'
  var param = {
    workId: workId.value,
    trackId: trackId.value,
    subjectionId: subjectionId.value
  }
  proxy.$axios.post(url, param).then(response => {
    if (response.data.code == '0') {
      flowInfo.value = response.data.data
      //businessId.value = flowInfo.value.flowVars['businessId']
      //tableName.value = flowInfo.value.flowVars['tableName']
      // if(!businessId.value||businessId.value == null||businessId.value == ''){
      // 	ElMessage.error('未找到相关业务数据ID，请联系管理检查流程！' )
      // 	return
      // }
      // if(!tableName.value||tableName.value == null||tableName.value == ''){
      // 	ElMessage.error('未找到相关业务数据表名，请联系管理检查流程！' )
      // 	return
      // }
      // console.log('businessId：：：' + businessId.value)
      // console.log('tableName：：：' + tableName.value)

      // workflow.setBusinessId(businessId.value)
      // getBusinessPage()
    } else {
      // ElMessage.error('操作失败！' + response.data.msg)
    }
    workflow.setWorkId(workId.value)
    getBusinessPage()
  })
}

const comment = ref('')

const handleClick = (item) => {
  console.log(item['buttonId'])
  console.log(item['buttonName'])
  if (item['buttonId'] === 'submit') {
    handlePreSubmit()
    // handleSubmit()
  } else if (item['buttonId'] === 'reject') {
    handleReject()
  } else if (item['buttonId'] === 'jumprejectstart') {
    handleJumpRejectStart()
  } else if (item['buttonId'] === 'getback') {
    handleGetBack()
  }

}

const handleSubmit = () => {
  if (comment.value === undefined || comment.value === '') {
    ElMessage({
      type: 'error',
      message: '请填写审批意见'
    });
    return
  }
  ElMessageBox.confirm(
      '确定通过?',
      '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
        draggable: true,
      }
  ).then(() => {
    var url = 'workflow/task/confirmSubmit'
    var param = {
      workId: workId.value,
      trackId: trackId.value,
      subjectionId: subjectionId.value,
      comment: comment.value
    }
    proxy.$axios.post(url, param).then(response => {

      if (response.data.code == 0) {
        var status = response.data.data.status

        // 选人
        if (status === 4) {
          var nextNodes = response.data.data.nextNodes;
          // 后续就只有一个节点，不需要选择节点
          if (nextNodes && nextNodes.length === 1) {

            // 选人
            if ((nextNodes[0].authors.Author && nextNodes[0].authors.Author.initAuthor)
                || nextNodes[0].authors.Author.selectAuthor === undefined) {

              var positions = undefined
              var depts = undefined
              if (nextNodes[0].authors.Author.initAuthor) {
                positions = nextNodes[0].authors.Author.initAuthor.positions
                depts = nextNodes[0].authors.Author.initAuthor.depts
                // 岗位
                if (positions) {
                  postName.value = positions[0].name
                  console.log('positions:' + postName.value)
                  getDataPost();
                }

                // 部门
                if (depts) {
                  deptIds.value = depts[0].id
                  console.log('depts:' + deptIds.value)
                  getDataDeptTree();
                }
              }

              if (nextNodes[0].authors.Author.initAuthor === undefined
                  && nextNodes[0].authors.Author.selectAuthor === undefined) {
                getDataPost();
                getDataDeptTree();
              }

              // 打开弹出框
              dialogVisibleUser.value = true
              nextNodeList.value = nextNodes;

            }
            // 不选人
            else {
              handleConfirmSubmit();
            }

          }
          // 后续多个节点，需要选择节点
          else if (nextNodes && nextNodes.length > 1) {
            console.log(nextNodes.length)
          }
          // 没有后续节点
          else {
            console.log(nextNodes.length)
          }

        }
        // 选节点
        else if (status === 2) {
          console.log('请选择节点')
          console.log(status)
          console.log(response.data.data)
          radioNodes.value = response.data.data.nextNodes
          console.log(radioNodes.value)
          if (radioNodes.value.length > 0) {
            radioNode.value = radioNodes.value[0].nodeName;
            console.log(radioNode.value)
          }
          dialogVisibleNode.value = true

        }
        // 执行成功
        else if (status === 0) {
          ElMessage({
            type: 'success',
            message: '操作成功，流程已通过',
          })
          handleTodo()
        }
      } else {
        ElMessage({
          type: 'error',
          message: '操作失败' + response.data.msg
        });
      }
    });
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '已取消',
    })
  })
}

const handlePreSubmit = () => {
  if (comment.value === undefined || comment.value === '') {
    ElMessage({
      type: 'error',
      message: '请填写审批意见'
    });
    return
  }
  var url = 'workflow/task/submit'
  var param = {
    workId: workId.value,
    trackId: trackId.value,
    subjectionId: subjectionId.value,
    comment: comment.value,
    nextNodes: JSON.stringify(nextNodeList.value),
  }

  proxy.$axios.post(url, param).then(response => {
    if (response.data.code == 0) {

      console.log(response.data.data)
      var status = response.data.data.status

      // 选人
      if (status === 4) {
        var nextNodes = response.data.data.nextNodes;
        // 后续就只有一个节点，不需要选择节点
        if (nextNodes && nextNodes.length === 1) {
          if ((nextNodes[0].authors.Author && nextNodes[0].authors.Author.initAuthor)
              || (nextNodes[0].authors.Author && nextNodes[0].authors.Author.selectAuthor === undefined)) {

            var positions = undefined
            var depts = undefined
            var others = undefined
            if (nextNodes[0].authors.Author.initAuthor) {
              positions = nextNodes[0].authors.Author.initAuthor.positions
              depts = nextNodes[0].authors.Author.initAuthor.depts
              others = nextNodes[0].authors.Author.initAuthor.others
              // 岗位
              if (positions) {
                postIds.value = ''
                for (let i = 0; i < positions.length; i++) {
                  postIds.value += positions[i].id + ','
                }
                console.log('positions:' + postIds.value)
                getDataPost();
              }

              // 部门
              if (depts) {
                deptIds.value = ''
                for (let i = 0; i < depts.length; i++) {
                  deptIds.value += depts[i].id + ','
                }
                console.log('depts:' + deptIds.value)
                getDataDeptTree();
              }

              // 单人
              // if (others) {
              //   userIds.value = ''
              //   for(let i = 0; i < others.length; i++) {
              //     userIds.value += others[i].id + ','
              //   }
              //   console.log('users:' + userIds.value)
              //   getUserPost();
              // }
            }

            if (nextNodes[0].authors.Author.initAuthor === undefined
                && nextNodes[0].authors.Author.selectAuthor === undefined) {
              getDataPost();
              getDataDeptTree();
            }

            // 打开弹出框
            dialogVisibleUser.value = true
            nextNodeList.value = nextNodes;
            nextNodeName.value = nextNodes[0].nodeName
          }
          // 不选人
          else {
            handleConfirmSubmit(nextNodes[0].nodeName);
          }
        }
        // 后续多个节点，需要选择节点、给多节点赋值用户
        else if (nextNodes && nextNodes.length > 1) {
          console.log('后续多个节点，需要选择节点、给多节点赋值用户')
        }
        // 没有后续节点
        else {
          console.log('没有后续节点')
        }
      }
      // 选节点
      else if (status === 2) {
        radioNodes.value = response.data.data.nextNodes
        console.log(radioNodes.value)
        if (radioNodes.value.length > 0) {
          radioNode.value = radioNodes.value[0].nodeName;
          console.log(radioNode.value)
        }
        dialogVisibleNode.value = true
      }
      // 执行成功
      else if (status === 0) {
        ElMessage({
          type: 'success',
          message: '操作成功，流程已通过',
        })
      }
    } else {
      ElMessage({
        type: 'error',
        message: '操作失败',
      })
    }
  });
}

const handleConfirmSubmit = (nodeName) => {
  ElMessageBox.confirm(
      '确定通过?下一节点:' + nodeName,
      '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
        draggable: true,
      }
  ).then(() => {
    var url = 'workflow/task/confirmSubmit'

    if (tableDataUser.value.length > 0) {
      var selectAuthor = ''
      var selectAuthorName = ''
      for (let i = 0; i < tableDataUser.value.length; i++) {
        selectAuthor = selectAuthor + tableDataUser.value[i].user.id + ';'
        selectAuthorName = selectAuthorName + tableDataUser.value[i].user.userDesc + ';'
      }
      var author = {
        "authorId": "Author",
        "authorName": "主办",
        "selectType": "radio",
        "isFree": "true",
        "isUser": "false",
        "isDefUser": "false",
        "isSelect": "true",
        "selectAuthor": selectAuthor,
        "selectAuthorName": selectAuthorName,
        "required": null,
        "initAuthor": null
      }
      nextNodeList.value[0].authors.Author = author;
    }

    var param = {
      workId: workId.value,
      trackId: trackId.value,
      subjectionId: subjectionId.value,
      comment: comment.value,
      nextNodes: JSON.stringify(nextNodeList.value),
    }

    console.log('param' + JSON.stringify(param))

    proxy.$axios.post(url, param).then(response => {
      if (response.data.code == 0) {
        console.log(response.data)
        ElMessage({
          type: 'success',
          message: '操作成功，流程已通过',
        })
        handleTodo()
      } else {
        ElMessage({
          type: 'error',
          message: '操作失败' + response.data.msg
        });
      }
    });
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '已取消',
    })
  })
}

const handleReject = () => {
  ElMessageBox.confirm(
      '确定退回?',
      '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
        draggable: true,
      }
  ).then(() => {
    var url = 'workflow/task/reject'
    var param = {
      workId: workId.value,
      trackId: trackId.value,
      subjectionId: subjectionId.value,
      comment: comment.value
    }
    proxy.$axios.post(url, param).then(response => {
      if (response.data.code == 0) {
        ElMessage({
          type: 'success',
          message: '操作成功，流程已退回',
        })
        handleTodo()
      } else {
        ElMessage({
          type: 'error',
          message: '操作失败' + response.data.msg
        });
      }
    });
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '已取消',
    })
  })
}

const handleGetBack = () => {
  ElMessageBox.confirm(
      '确定拿回?',
      '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
        draggable: true,
      }
  ).then(() => {
    var url = 'workflow/task/getBack'
    var param = {
      workId: workId.value,
      trackId: trackId.value,
      subjectionId: subjectionId.value
    }
    proxy.$axios.post(url, param).then(response => {
      if (response.data.code == 0) {
        ElMessage({
          type: 'success',
          message: '操作成功，流程已拿回',
        })
        handleTodo()
      } else {
        ElMessage({
          type: 'error',
          message: '操作失败，流程已审批，不允许拿回'
        });
      }
    });
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '已取消',
    })
  })
}

const handleJumpRejectStart = () => {
  ElMessageBox.confirm(
      '确定退回?',
      '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
        draggable: true,
      }
  ).then(() => {
    var url = 'workflow/task/jumpRejectStart'
    var param = {
      workId: workId.value,
      trackId: trackId.value,
      subjectionId: subjectionId.value,
      comment: comment.value
    }
    proxy.$axios.post(url, param).then(response => {
      if (response.data.code == 0) {
        ElMessage({
          type: 'success',
          message: '操作成功，流程已退回到提交人',
        })
        handleTodo()
      } else {
        ElMessage({
          type: 'error',
          message: '操作失败' + response.data.msg
        });
      }
    });
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '已取消',
    })
  })
}

const handleTodo = () => {

  if (type.value === '2') {
    proxy.$router.push({
      path: '/workflow/haddone',
      query: {}
    })
  } else if (type.value === '3') {
    proxy.$router.push({
      path: '/schedule'
    })
  } else {
    proxy.$router.push({
      path: '/workflow/todo',
      query: {}
    })
  }
}

</script>

<style>
</style>
