<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'
import dayjs from 'dayjs'

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: 'id',
    label: '主键',
    component: 'Input',
    hidden: true
  },{
    field: 'filePath',
    label: '文件绝对路径,包含文件名',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'orgNo',
    label: '机构编号',
    component: 'Select',
    componentProps: {
      
    },
  },
  {
    field: 'filePwd',
    label: '文件压缩密码',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'fileMd5',
    label: '文件MD5校验码',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'notifyTime',
    label: '指标通知下发时间',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'createTime',
    label: '数据库记录创建的时间',
    component: 'DatePicker',
    componentProps: {
      
    },
  }
])

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()
    const { createTime } = formData;
    Object.assign(formData, { createTime: dayjs(createTime).format('YYYY-MM-DD') })
    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>

