<script setup name="hiddenAppForm">
import { nextTick, ref, watch } from 'vue'
// import appBasicInfo from './appBasicInfo.vue'
// import appSafeInfo from './appSafeInfo.vue'
// import appResourceInfo from './appResourceInfo.vue'
import ApplicationInfo from '@/pages/declareManage/projectDeclare/declarePage/components/applicationInfo.vue'
const props = defineProps({
    applicationList: Array,
    isFirst: {
      type: Number,
      default: undefined
    }
  }),
  // appBasicInfoRef = ref(),
  // appSafeInfoRef = ref(),
  // appResourceInfoRef = ref(),
  applicationInfoRef = ref(),
  applicationData = ref([]),
  validCount = ref(0),
  drawerVisible = ref(),
  validAllAppForm = async (callback) => {
    drawerVisible.value = true
    validCount.value = 0
    for (const item of props.applicationList) { // 不能用forEach,因为不会等异步的执行完成才执行下一条
      await doAsyncOperation(item)
    }
    setTimeout(async () => {
      drawerVisible.value = false
      callback(validCount.value === props.applicationList.length)
    })
  },
  doAsyncOperation = async (data) => {
    applicationData.value = JSON.parse(JSON.stringify(data))
    await nextTick() // 必须nextTick,不然赋值视图上不会更新
    return new Promise((resolve, reject) => {
      const form = []
      // form.push(new Promise((resolve, reject) => {
      //   appBasicInfoRef.value.validForm((valid) => {
      //     if (valid) {
      //       resolve()
      //     } else {
      //       reject(false)
      //     }
      //   })
      // }))
      // form.push(new Promise((resolve, reject) => {
      //   appSafeInfoRef.value.validForm((valid) => {
      //     if (valid) {
      //       resolve()
      //     } else {
      //       reject(false)
      //     }
      //   })
      // }))
      // form.push(new Promise((resolve, reject) => {
      //   appResourceInfoRef.value.validForm((valid) => {
      //     if (valid) {
      //       resolve()
      //     } else {
      //       reject(false)
      //     }
      //   })
      // }))
      form.push(new Promise((resolve, reject) => {
        applicationInfoRef.value.validForm((valid) => {
          if (valid) {
            resolve()
          } else {
            reject(false)
          }
        })
      }))
      Promise.all([...form]).then(() => {
        validCount.value++
      }).finally(() => {
        resolve()
      })
    })
  }
watch(
  () => props.applicationList,
  () => {
  },
  { immediate: true, deep: true }
)
defineExpose({ validAllAppForm })

</script>

<template>
  <div style="display: none">
    <!--    <app-basic-info-->
    <!--      ref="appBasicInfoRef"-->
    <!--      :data="applicationData"-->
    <!--      :drawer-visible="drawerVisible"-->
    <!--      :is-innovate-whole-province-share="isInnovateWholeProvinceShare"-->
    <!--    />-->
    <!--    <app-safe-info ref="appSafeInfoRef" :data="applicationData" />-->
    <!--    <app-resource-info ref="appResourceInfoRef" :data="applicationData" />-->
    <application-info
      ref="applicationInfoRef"
      :data="applicationData"
      :is-first="isFirst"
    />
  </div>
</template>
