<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 { useI18n } from '@/hooks/web/useI18n'
import { getMenuListApi } from '@/api/menu'
import { ElTag, ElButton } from 'element-plus'

const { t } = useI18n()

const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<any>,
    default: () => null
  }
})

const formSchema = reactive<FormSchema[]>([
  {
    field: 'name',
    label: '指标名称长度',
    component: 'Input'
  },
  {
    field: 'id',
    label: '指标编码',
    component: 'Input'
  },
  {
    field: 'id1',
    label: '规则强度',
    component: 'Input'
  },
  {
    field: 'id1',
    label: '规则状态',
    component: 'Input'
  },
  {
    field: 'id1',
    label: '业务类型',
    component: 'Input'
  },
  {
    field: 'id1',
    label: '对照编码',
    component: 'Input'
  },
  {
    field: 'id1',
    label: '保存前校验',
    component: 'Input'
  },
  {
    field: 'id1',
    label: '导入前校验',
    component: 'Input'
  },
  {
    field: 'id1',
    label: '入库前校验',
    component: 'Input'
  },
  {
    field: 'id1',
    label: '非空函数',
    component: 'Input'
  },
  {
    field: 'id1',
    label: '过滤条件',
    component: 'Input'
  },
  {
    field: 'id1',
    label: '规则说明',
    component: 'Input'
  }
])

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

const { formRegister, formMethods } = useForm()
const { setValues, getFormData, getElFormExpose, setSchema } = formMethods

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>
