<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="title" @ok="handleSubmit" width="60%">
    <div class="pt-4" style="height: 220px" v-for="(item, index) in czWc" :key="index +'czwc'"> 
      <!-- <BasicForm @register="registerForm" /> -->
      <fillingendForm :ref="(el) => childRef(el, index)" upload=" " :data="item" :isRead="true" :disabled="objFlag"></fillingendForm>
    </div>
  </BasicModal>
</template>

<script lang="ts" setup>
  import { ref, nextTick } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { updateCzwc } from '../api';
  import { useMessage } from '/@/hooks/web/useMessage';
  import fillingendForm from './fillingendForm.vue';
  const { createMessage } = useMessage();
  // Emits声明
  const emit = defineEmits(['register', 'success']);
  const czWc = ref<any>([]);
  const czwcBzd = ref<any>([]);
  const objFlag = ref<any>(false);
    

  const values = ref<any>({});
  //设置标题
  const title = ref('接收完成填报');
  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
   
    //表单赋值
    // await setFieldsValue({
    //   ...data.record,
    // });
    values.value = data.record

    setModalProps({showCancelBtn:data?.showFooter,showOkBtn:data?.showFooter});
    
    if (data.record?.flag && data.record?.flag == 2) {
      czWc.value = data.record?.czwcList
      objFlag.value = true
    } else {
      czWc.value = []
    }
    // czWc.value = []
    czwcBzd.value = []

    let arr = data.record.jsdRealname.split(",")
    if (values.value.flag != 2) {
      for (let i = 0; i < arr.length; i++) {
        const element = arr[i];
        addDw(element, data.record, i)
      }
    }
    
  });

  const childRef = (el, index) => {
    if (!el) return
    czwcBzd.value[index] = el;
  };


  function addDw(element, data, i) {
    czWc.value.push({ jsdRealname: element, jsdzgbm: data.jsdzgbm.split(",")[i], szdRealname: data.szdRealname, szdzgbm: data.szdzgbm, });
    nextTick(() => {
      callChildMethod(czwcBzd.value, czwcBzd.value.length - 1);
    });
  }

  const callChildMethod = (item, index) => {
    item[index].getFormValueBz();
  };

  
  const czwcList = ref<any>([])
  //表单提交事件
  async function handleSubmit() {
    for (let i = 0; i < czwcBzd.value.length; i++) {
      const element = czwcBzd.value[i];
      let obj = await element.getFormValueBz()
      obj.wrdkid = values.value.wrdkid
      czwcList.value.push(obj)
    }
    // return
    try {
      values.value.czwcList = czwcList.value
      setModalProps({ confirmLoading: true });
      //提交表单
      await updateCzwc(values.value);
      //关闭弹窗
      closeModal();
      emit('success');
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>

<style lang="less" scoped>
  :deep(.ant-input-number) {
    width: 100%;
  }
</style>
