<!--
 * @Author: nxy
 * @Date: 2024-08-07 14:31:29
-->
<template>
  <a-modal
    :width="750"
    :maskClosable="false"
    :visible="visible"
    :confirm-loading="loading"
    :forceRender="true"
    title="流程预测"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    @ok="save"
    class="fix-modal"
    @close="updateVisible(false)"
  >
    <FlowDrawerItem name="流程预测">
      <NextNodeSelect v-model:form="form" ref="nextNodeSelectRef" />
    </FlowDrawerItem>
  </a-modal>
</template>

<script setup name="FlowPrediction">
import { onMounted, ref } from 'vue';
import NextNodeSelect from './next-node-select.vue';
import { FlowTaskApi } from '../../list/api/FlowTaskApi';
import { deepClone } from '@/utils/common/util';
import { message } from 'ant-design-vue/es';
import FlowDrawerItem from '../../../../../flow-design/layout/right/panel/common/FlowDrawerItem.vue'

const props = defineProps({
  visible: Boolean,
  // 查询参数
  params: Object
});

const emits = defineEmits(['done', 'update:visible']);

const loading = ref(false);

// 表单数据
const form = ref({
  // 下一节点列表
  nextNodeList: []
});

// ref
const nextNodeSelectRef = ref();

onMounted(() => {
  getRuntimeInstancePredict();
});

// 获取下一节点列表
const getRuntimeInstancePredict = () => {
  FlowTaskApi.runtimeInstancePredict(props.params).then(res => {
    form.value.nextNodeList = res.data ?? [];
  });
};

// 更改弹框或抽屉状态
const updateVisible = value => {
  emits('update:visible', value);
};

// 保存
const save = async () => {
  let flagArr = [];
  form.value.nextNodeList.forEach(item => {
    if (item.approverNodeType == 10 && item.approverPersons && item.approverPersons.length > 1) {
      flagArr.push(true);
    } else {
      flagArr.push(false);
    }
  });
  if (flagArr.includes(true)) {
    return message.warning('单人活动只能有一个审批人');
  }
  await nextNodeSelectRef.value.$refs.formRef.validate();
  let pointNodeApproverUserId = {};
  Object.keys(form.value.nextNodeList).forEach(key => {
    pointNodeApproverUserId[form.value.nextNodeList[key].nextNodeId] = form.value.nextNodeList[key].approverPersons.map(item => item.id);
  });
  let params = deepClone(props.params);
  params.instanceVariable.pointNodeApproverUserId = pointNodeApproverUserId;
  emits('done', params);
  updateVisible(false);
};
</script>

<style></style>
