<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'

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: 'subNo',
    label: '风险要素',
    component: 'Select',
    componentProps: {
      
    },
  },
  {
    field: 'subCode',
    label: '子项代码',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'subName',
    label: '子项',
    component: 'Select',
    componentProps: {
      
    },
  },
  {
    field: 'custType',
    label: '客户类型',
    component: 'Select',
    componentProps: {
      
    },
  },
  {
    field: 'upSubNo',
    label: '上级代码',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'subWight',
    label: '权重',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'subDesc',
    label: '说明',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'treelaye',
    label: '层级',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'grade',
    label: '分数',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'isLast',
    label: '是否子节点',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'isEdit',
    label: '是否可编辑',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'orderby',
    label: '排序',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'lawerMan',
    label: '法人代码',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'lawerOrg',
    label: '法人机构',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'gathertype',
    label: '采集类型',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'flag',
    label: '启用标识',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'topName',
    label: '最上级节点名称',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'viewOrderby',
    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>

