<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, reactive, watch } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'

const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<any>,
    default: () => null
  },
  actionType: {
    type: String,
    default: 'add'
  }
})

const { formRegister, formMethods } = useForm()
const { setValues, getFormData, getElFormExpose } = formMethods

const formSchema = reactive<FormSchema[]>([
  {
    field: 'pkid',
    label: '序列字段',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'pkgCode',
    label: '数据包编号',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'pkgType',
    label: '数据包类型',
    component: 'Select',
    componentProps: {
      
    },
  },
  {
    field: 'repType',
    label: '交易类型',
    component: 'Select',
    componentProps: {
      
    },
  },
  {
    field: 'sendSeq',
    label: '当日报送的批次',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'pkgName',
    label: '数据包文件名',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'status',
    label: '数据包状态',
    component: 'Select',
    componentProps: {
      
    },
  },
  {
    field: 'createDate',
    label: '生成时间',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'pkgContent',
    label: '数据包实体',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'organCode',
    label: '机构名称',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'isuploadf',
    label: '有无附件',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'trantdate',
    label: '数据交易日期',
    component: 'DatePicker',
    componentProps: {
      type: 'daterange'
    },
  }
])

const rules = reactive({
  
})

const submit = async () => {
  const elForm = await getElFormExpose()
  const valid = await elForm?.validate().catch((err) => {
    console.log(err)
  })
  if (valid) {
    const formData = await getFormData()
    return formData
  }
}

watch(
  () => props.currentRow,
  (currentRow) => {
    if (!currentRow) return
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  submit
})
</script>

<template>
  <Form :rules="rules" @register="formRegister" :schema="formSchema" />
</template>

