<template>
  <!-- 自定义表单 -->
  <SplitProjectDetail :row="splitDetailList" :form="fromData" v-if="fromData"/>
  <ApprovalDetails :processInstanceId="processInstanceId" />
  <ApprovalFromPage :showApprovalForm="showApprovalForm" :buttons="resButton.buttons" ref="ApprovalFromPageRef"
    @submit="handleSubmit" @exit="exit" />
</template>
<script lang="ts" name="fieldChangeApproval" setup>
import { onMounted, ref, reactive } from 'vue'
import { approvalSplitProject, getActionParam } from '../myWork/inComplete/inComplete.api';
import { ElMessage } from 'element-plus'
import {  approvalFieldchange ,getSplitDetailList,getFromDataFromSplit} from '../projectLib/projectInfo/projectInfo.api'

import SplitProjectDetail from "../ProcessApprovalSubPage/component/SplitProjectDetail.vue"
import ApprovalDetails from "../ProcessApprovalSubPage/component/ApprovalDetails.vue";
import ApprovalFromPage from "../ProcessApprovalSubPage/component/ApprovalFromPage.vue"
let dataTo = defineProps(["record"])
const emit = defineEmits(['close']);
let splitDetailList = ref()
let projectId = dataTo.record.projectId as string
let processInstanceId = dataTo.record.processInstanceId
let taskid = dataTo.record.taskId as string
let fromData = ref()
let showApprovalForm = ref()
let ApprovalFromPageRef = ref()
let resButton = reactive({
  showApprovalForm: false,
  isEdit: false,
  buttons: []
})
let fileList = reactive<Array<any>>([]);
onMounted(async () => {
  resButton = await getActionParam({ processInstanceId: processInstanceId, taskId: taskid, procesType: dataTo.record.procesType })
  showApprovalForm.value = resButton.showApprovalForm
  splitDetailList.value=await getSplitDetailList({ projectid: projectId})
  let template=await getFromDataFromSplit({ projectid: projectId})
  fromData.value=template
})
function removeFile(option) {
  for (let i = 0; i < fileList.length; i++) {
    if (fileList[i].file.name == option.name) {
      fileList.splice(i, 1)
    }
  }
}
function httpRequest(option) {
  fileList.push(option)

}

// 上传前处理
async function beforeUpload(file) {
  let fileSize = file.size
  const FIVE_M = 500 * 1024 * 1024;
  //大于5M，不允许上传
  if (fileSize > FIVE_M) {
    ElMessage.error("最大上传500M")
    return false
  }
  return true
}
// 文件数量过多时提醒
function handleExceed() {
  ElMessage.warning("最多只能上传一个文件")
}




async function handleSubmit() {
  let approvalform = await ApprovalFromPageRef.value.getFieldsValueApprovalForm();
  const params = new FormData()
  params.append("flag", approvalform.flag)
  params.append("projectid", projectId)
  params.append("comment", approvalform.comment)
  params.append("taskId", taskid)
  if (fileList.length > 0) {
    fileList.forEach((x) => {
      params.append("file", x.file)
    });
  }
  console.log("param", params, "approvalform", approvalform)
  if (await ApprovalFromPageRef.value.validateApprovalForm() ) {

    let res = await approvalSplitProject(params)
    console.log("提交成功！", res)
    emit("close")


  }
}
async function exit() {
  emit("close")

}
</script>
<style></style>