<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'
import { PROCESS_FLAG_LIST } from "../constants"
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',
    componentProps: {
      
    },
  },
  {
    field: 'orgNo',
    label: '机构编号',
    component: 'Select',
    componentProps: {
      
    },
  },
  {
    field: 'subOrgNo',
    label: '子机构编号',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'content',
    label: '待处理通知内容',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'createTime',
    label: '数据库记录创建的时间',
    component: 'DatePicker',
    componentProps: {
      
    },
  },
  {
    field: 'processFlag',
    label: '是否处理',
    component: 'Select',
    componentProps: {
      options:PROCESS_FLAG_LIST
    },
  }
])

const rules = reactive({
  orgNo: [required()],
  subOrgNo: [required()],
  content: [required()],
  createTime: [required()],
  processFlag: [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 { 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>

