<!--
  @description: 流程下一节点
  @Modifier: haoFx
  @routerPath:
  @date: 2020/3/27-12:53
-->
<template>
  <div>
    <el-form
      ref="form"
      :model="processNode"
      :rules="rules"
      label-width="140px"
    >
      <el-form-item
        v-if="isShowProcessInfo"
        ref="nodeName"
        label="请选择流程路径"
        prop="nodeName"
      >
        <el-select
          v-model="processNode.nodeName"
          style="width: 230px;"
          placeholder="请选择流程路径"
          @change="nodeNameChange"
        >
          <el-option
            v-for="(item,index) in list"
            :key="index"
            :label="item.nodeName"
            :value="index"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { nextTasks, getActModeUserByNodeIdProcessDefinitionKey } from '@/api/work/work'

export default {
  name: 'ProcessNodeDialog',
  data() {
    return {
      isShowProcessInfo: false, // 是否显示流程节点信息 当只有一个节点时不显示
      list: [], // 网关节点数据
      // 校验输入内容
      rules: {
        nodeName: [{ required: true, message: '请选择', trigger: 'change' }]
      },
      // 节点信息
      processNode: {
        nodeId: '', // 节点id主键
        nodeName: '', // 节点名称
        processDefinitionId: '',
        processDefinitionKey: '',
        type: '01' // 01：初次提交立项 02：审核驳回再次提交
      },
      passNodeId: {
        nodeId: '', // 节点id主键
        nodeName: '', // 节点名称s
        isCreateUser: '',
        isArtificial: '',
        processDefinitionId: '',
        processDefinitionKey: '',
        gatewayType: '',
        nodeKey: '',
        nodeValue: ''
      },
      noPassNodeId: {
        nodeId: '', // 节点id主键
        nodeName: '', // 节点名称s
        isCreateUser: '',
        isArtificial: '',
        processDefinitionId: '',
        processDefinitionKey: '',
        gatewayType: '',
        nodeKey: '',
        nodeValue: ''
      },
      gatewayType: '', // 是否遇到网关 01是  00 否
      isCreateUser: '', // 是否指定人员01指定  00不指定
      isArtificial: '', // 是否人工 智能
      isCheckMutil: '', // 是否指定审核人（审核人是否弹框）00不弹框
      isBl: 0,
      isResult: 0,
      arrayPush: []
    }
  },
  methods: {
    // 父组件触发初始化下一节点信息
    initNextTasks(taskId, processNodeCode, gatewayValue, processDefinitionId, processDefinitionKey) {
      this.processNode.processDefinitionId = processDefinitionId
      this.passNodeId.processDefinitionId = processDefinitionId
      this.noPassNodeId.processDefinitionId = processDefinitionId
      this.processNode.processDefinitionKey = processDefinitionKey
      this.passNodeId.processDefinitionKey = processDefinitionKey
      this.noPassNodeId.processDefinitionKey = processDefinitionKey
      this.getNodeNameNextTasks(taskId, processNodeCode, gatewayValue)
    },
    // 工作流流程节点/获取下个节点数据
    getNodeNameNextTasks(taskId, processNodeCode, gatewayValue) {
      let paramsNodeId = '' // 正常节点网关节点取nodeId
      let intelligenceItem = {} // 网关节点数据
      nextTasks(taskId).then(res => {
        const { data } = res.data
        this.isResult = data.length
        /* data.map((item, i) => {
            // 判断网关节点 节点id取值不同
            if (item.hasOwnProperty('extended') === false && item.outgoingFlows.length > 1) {
              this.isResult = this.isResult - 1
            }
          })*/
        // data.reverse()
        const thin = this
        data.map((item, i) => {
          // 判断网关节点 节点id取值不同
          let gatewayTyper = ''
          // const arrayPush = []
          if (item.hasOwnProperty('extended') === false && item.outgoingFlows.length > 1) {
            intelligenceItem = item
            gatewayTyper = '00' // 是否遇到网关 01是  00 否
            const gatewayval = parseInt(gatewayValue)
            item.outgoingFlows.map((outItem, t) => {
              thin.arrayPush.push(item.outgoingFlows[t])
            })
            thin.arrayPush.map((outItem, t) => {
              const val = parseInt(outItem.conditionExpression.replace('${passvalue', '').replace('}', '').replace('==', ''))
              if (val !== gatewayval) {
                item.outgoingFlows.splice(t, 1)
              } else {
                paramsNodeId = outItem.targetRef
              }
            })
          } else {
            // 正常节点
            paramsNodeId = data[i].id
            gatewayTyper = '00' // 是否遇到网关 01是  00 否
            // 初始化节点没有网关
            this.handleListData(data, processNodeCode, gatewayValue)
            // 传给父组件
            this.$emit('isGateway', this.gatewayType)
            intelligenceItem = item
          }
          // 查询当前配置
          this.getActModeUser(paramsNodeId, intelligenceItem, gatewayTyper, gatewayValue)
        })
      }).catch(() => {

      })
    },
    // 工作流流程节点/根据processDefinitionId和节点id 查询当前配置
    getActModeUser(paramsNodeId, intelligenceItem, gatewayTyper, gatewayValue) {
      const thin = this
      getActModeUserByNodeIdProcessDefinitionKey(paramsNodeId, thin.processNode.processDefinitionKey)
        .then(res => {
          const { data } = res.data
          thin.isCreateUser = data.isCreateUser // 是否指定人员01指定  00不指定
          thin.isArtificial = data.isArtificial // 是否人工 智能
          thin.isCheckMutil = data.isCheckMutil// 是否指定审核人（审核人是否弹框）00不弹框
          // 人工 && 网关节点
          if (this.isArtificial === '00' && gatewayTyper === '01') {
            thin.flowGateway(intelligenceItem, thin.isCreateUser, thin.isArtificial, thin.isCheckMutil)
            thin.isShowProcessInfo = true
          }
          // 智能 && 网关节点
          if (thin.isArtificial === '01' && gatewayTyper === '01') {
            // 流程网关处理方法
            thin.flowGateway(intelligenceItem, thin.isCreateUser, thin.isArtificial, thin.isCheckMutil, gatewayValue)
          }
          if (thin.passNodeId.nodeId === paramsNodeId) {
            thin.passNodeId.isArtificial = thin.isArtificial
            thin.passNodeId.isCreateUser = thin.isCreateUser
            thin.passNodeId.isCheckMutil = thin.isCheckMutil
          } else if (thin.noPassNodeId.nodeId === paramsNodeId) {
            thin.noPassNodeId.isArtificial = thin.isArtificial
            thin.noPassNodeId.isCreateUser = thin.isCreateUser
            thin.noPassNodeId.isCheckMutil = thin.isCheckMutil
          }
          thin.isBl = thin.isBl + 1
          // 传给父组件通过节点和驳回节点的节点信息
          if (thin.isBl === thin.isResult) {
            thin.$emit('conditionExpression', thin.passNodeId, thin.noPassNodeId)
          }

          // 传给父组件 是否指定创建人 是否人工智能配置
          thin.$emit('getActModeUser', data)
        }).catch(() => {

        })
    },
    /* ===========================正常节点不走网关========================================*/
    // 初始化节点没有网关
    handleListData(data, processNodeCode, gatewayValue) {
      let str = ''
      let conditionExpression = ''
      data.map((item) => {
        if (item.outgoingFlows.length === 1) {
          item.outgoingFlows = this.arrayPush
        }

        // 处理同意节点驳回节点信息
        item.incomingFlows.forEach((item) => {
          if (item.sourceRef === processNodeCode) {
            str = item.conditionExpression
          }
        })

        conditionExpression = str.slice(2, str.length - 1)// 截取字符串
        if (conditionExpression === 'pass') {
          if (item.hasOwnProperty('extended') === false && item.outgoingFlows.length > 1) {
            item.outgoingFlows.forEach((itemGate) => {
              if (itemGate.conditionExpression.indexOf(gatewayValue) > -1) {
                this.passNodeId.nodeId = itemGate.targetRef
                /*  nodeIdAll = itemGate.conditionExpression // nodeKey nodeValue值
                    index = nodeIdAll.indexOf('==')// ==出现位置
                    this.passNodeId.nodeKey = nodeIdAll.slice(2, index)// 截取字符串
                    this.passNodeId.nodeValue = nodeIdAll.slice(index + 2, nodeIdAll.length - 1)// 截取字符串*/
              }
            })
            // this.passNodeId.nodeId = item.outgoingFlows[0].targetRef
            this.passNodeId.processDefinitionId = this.processNode.processDefinitionId
            this.passNodeId.gatewayType = '01'
          } else {
            // 通过节点
            this.passNodeId.nodeId = item.id
            this.passNodeId.nodeName = item.name
            this.passNodeId.processDefinitionId = this.processNode.processDefinitionId
            this.passNodeId.gatewayType = '00'
          }
        } else if (conditionExpression === '!pass') {
          if (item.hasOwnProperty('extended') === false && item.outgoingFlows.length > 1) {
            item.outgoingFlows.forEach((itemGate) => {
              if (itemGate.conditionExpression.indexOf(gatewayValue) > -1) {
                this.noPassNodeId.nodeId = itemGate.targetRef
                /* nodeIdAll = itemGate.conditionExpression // nodeKey nodeValue值
                  index = nodeIdAll.indexOf('==')// ==出现位置
                  this.noPassNodeId.nodeKey = nodeIdAll.slice(2, index)// 截取字符串
                  this.noPassNodeId.nodeValue = nodeIdAll.slice(index + 2, nodeIdAll.length - 1)// 截取字符串*/
              }
            })
            // this.noPassNodeId.nodeId = item.outgoingFlows[0].targetRef
            this.noPassNodeId.processDefinitionId = this.processNode.processDefinitionId
            this.noPassNodeId.gatewayType = '01'
          } else {
            // 驳回节点
            this.noPassNodeId.nodeId = item.id
            this.noPassNodeId.nodeName = item.name
            this.noPassNodeId.processDefinitionId = this.processNode.processDefinitionId
            this.noPassNodeId.gatewayType = '00'
          }
        } else {
          const outArr = item.outgoingFlows
          outArr.map((outitem) => {
            if (outitem.conditionExpression === '${passvalue == ' + gatewayValue + '}') {
              this.passNodeId.nodeId = outitem.targetRef
              // this.passNodeId.nodeName = outitem.name
              this.passNodeId.processDefinitionId = this.processNode.processDefinitionId
              this.passNodeId.gatewayType = '00'
            }
          })
        }
      })
    },
    /* =============================下个节点是网关流程网关处理======================================*/
    // 流程网关
    flowGateway(data, isCreateUser, isArtificial, gatewayValue, isCheckMutil) {
      let str = ''
      let index = ''
      let nodeIdAll = ''
      // 下个节点是网关  且 isArtificial==00  需要选择节点  nodeKey=node  nodeValue=1  根据线${node==1}  拆分

      const list = data.outgoingFlows
      const thin = this
      list.map((item) => {
        item.nodeName = item.name + '到' + item.documentation
        item.nodeId = item.id
        item.processDefinitionId = thin.processNode.processDefinitionId
        item.isCreateUser = isCreateUser
        item.isArtificial = isArtificial
        str = item.conditionExpression // nodeKey nodeValue值
        index = str.indexOf('==')// ==出现位置
        item.nodeKey = str.slice(2, index)// 截取字符串
        item.nodeValue = str.slice(index + 2, str.length - 1)// 截取字符串
        if (item.conditionExpression.indexOf(gatewayValue) > -1) {
          this.passNodeId.nodeId = item.targetRef
          nodeIdAll = item.conditionExpression // nodeKey nodeValue值
          index = nodeIdAll.indexOf('==')// ==出现位置
          this.passNodeId.nodeKey = nodeIdAll.slice(2, index)// 截取字符串
          this.passNodeId.nodeValue = nodeIdAll.slice(index + 2, nodeIdAll.length - 1)// 截取字符串
          this.processNode.nodeKey = nodeIdAll.slice(2, index)// 截取字符串
          this.processNode.nodeValue = nodeIdAll.slice(index + 2, nodeIdAll.length - 1).replace(/'/g, '')// 截取字符串
        }
      })
      /* =====智能网关（取第一条数据传给父组件）============*/
      if (thin.isArtificial === '01') {
        /*  thin.processNode.nodeKey = list[0].nodeKey
            thin.processNode.nodeValue = list[0].nodeValue
            thin.processNode.nodeName = list[0].name + '到' + list[0].documentation
            thin.processNode.nodeId = list[0].targetRef*/
        thin.passNodeId.isCreateUser = isCreateUser
        thin.passNodeId.isArtificial = isArtificial
        thin.processNode.isCreateUser = isCreateUser
        thin.processNode.isArtificial = isArtificial
        thin.processNode.isCheckMutil = isCheckMutil // 是否指定审核人（审核人是否弹框）00不弹框
        // 传给父组件保存节点的信息
        this.$emit('processNodeData', this.processNode)
      }
      this.list = list
      this.$emit('isGateway', '01') // 是否遇到网关 01是  00 否
    },
    // 网关（人工）节点===触发选中节点名称
    nodeNameChange(val) {
      this.processNode.nodeKey = this.list[val].nodeKey
      this.processNode.nodeValue = this.list[val].nodeValue
      this.processNode.nodeName = this.list[val].name + '到' + this.list[val].documentation
      this.processNode.nodeId = this.list[val].targetRef
      this.processNode.isCreateUser = this.list[val].isCreateUser
      this.processNode.isArtificial = this.list[val].isArtificial
      this.processNode.isCheckMutil = this.list[val].isCheckMutil // 是否指定审核人（审核人是否弹框）00不弹框
      // 传给父组件
      this.$emit('processNodeData', this.processNode)
    },
    // 校验是否选择流程路径
    validate() {
      if (!this.processNode.nodeName) {
        this.$message({
          message: '请选择流程路径',
          type: 'warning'
        })
        return false
      } else {
        return true
      }
    }
  }
}
</script>

<style scoped>

</style>
