<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 { ENABLE_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',
    hidden: true
  },{
    field: 'regulationId',
    label: '制度编号',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'regulationName',
    label: '制度名称',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'regulationVersion',
    label: '制度版本',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'filePwd',
    label: '制度文件压缩密码',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'collectingCode',
    label: '采集编号',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'filePath',
    label: '制度文件绝对路径',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'fileMd5',
    label: '制度包MD5',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'deliveryTime',
    label: '制度下发时间',
    component: 'DatePicker',
    componentProps: {
      
    },
  },
  {
    field: 'enable',
    label: '是否启用',
    component: 'Select',
    componentProps: {
      options:ENABLE_LIST
    },
  },
  {
    field: 'createTime',
    label: '数据库记录创建的时间',
    component: 'Input',
    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 { deliveryTime } = formData;
    Object.assign(formData, { deliveryTime: dayjs(deliveryTime).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>

