<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 {IS_PROMARY_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: 'tableCode',
    label: '表编码',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'tableNameEn',
    label: '表英文名',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'tableNameCn',
    label: '表中文名',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'fieldCode',
    label: '数据项代码',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'fieldNameEn',
    label: '数据项英文名称',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'fieldNameCn',
    label: '数据项代码中文名称',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'isPrimery',
    label: '是否主键字段',
    component: 'Input',
    componentProps: {
      options:IS_PROMARY_LIST
    },
  },
  {
    field: 'collectingCode',
    label: '采集编号',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'regulationId',
    label: '制度编号',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'regulationName',
    label: '制度名称',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'regulationVersion',
    label: '制度版本',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'fieldType',
    label: '数据类型',
    component: 'Select',
    componentProps: {
      
    },
  },
  {
    field: 'fieldLength',
    label: '数据长度',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'metaNo',
    label: '元数据编号',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'metaName',
    label: '元数据名称',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'preMetaList',
    label: '关联元数据编号',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'isPartition',
    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()
    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>

