<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: 'modelCode',
    label: '模型编码',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'modelName',
    label: '模型名称',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'description',
    label: '描述',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'organCode',
    label: '用户所属机构',
    component: 'Select',
    componentProps: {
      
    },
  },
  {
    field: 'createUser',
    label: '登录用户',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'createDate',
    label: '创建日期',
    component: 'DatePicker',
    componentProps: {
      
    },
  }
])

const rules = reactive({
  modelCode: [required()],
})

const submit = async () => {
  const elForm = await getElFormExpose()
  const valid = await elForm?.validate().catch((err) => {
    console.log(err)
  })
  if (valid) {
    const formData = await getFormData()
    const { createDate } = formData;
    Object.assign(formData, { createDate: dayjs(createDate).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>

