<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 { TAB_LEVEL_LIST,RULE_TYPE_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: 'ruleNo',
    label: '规则编码',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'ruleType',
    label: '规则类别',
    component: 'Select',
    componentProps: {
      options:RULE_TYPE_LIST
    },
  },
  {
    field: 'ruleOwner',
    label: '规则归属',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'tabLevel',
    label: '分类',
    component: 'Select',
    componentProps: {
      options:TAB_LEVEL_LIST
    },
  },
  {
    field: 'tabNameEn',
    label: '校验表英文名',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'tabName',
    label: '校验表中文名',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'tabGuid',
    label: '表唯一性编码',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'itemNo',
    label: '校验项编码',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'itemName',
    label: '校验项名称',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'mainCategory',
    label: '校验大类',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'subCategory',
    label: '校验小类',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'checkRange',
    label: '校验范围',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'formulaDesc',
    label: '校验公式说明',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'checkScript',
    label: '校验sql脚本',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'checkProperty',
    label: '校验性质',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'dayThresholdValue',
    label: '缺失率阈值',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'monthThresholdValue',
    label: '缺失率阈值',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'linkTable',
    label: '关联表信息',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'verifyMode',
    label: '校验方式',
    component: 'Select',
    componentProps: {
      
    },
  },
  {
    field: 'outTableName',
    label: '外部表名',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'dbType',
    label: '数据库类型',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'versionNo',
    label: '规则版本号',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'createTime',
    label: '创建时间',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'updateTime',
    label: '更新时间',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'createUserId',
    label: '创建人id',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'createUserName',
    label: '创建人名称',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'updateUserId',
    label: '修改人id',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'updateUserName',
    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>

