<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, reactive, watch } from 'vue'
import { WHETHER_UUID_LIST,WHETHER_UNITEDKEY_LIST } from '../constants'
import { useValidator } from '@/hooks/web/useValidator'
import { ElButton, ElMessage, ElMessageBox } from 'element-plus';


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: 'modelCode',
    label: '数据模型编码',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'ruleId',
    label: '规则id',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'srcCol',
    label: '源列名',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'srcColType',
    label: '源列类型',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'mappingName',
    label: '映射目标列名称',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'mappingCol',
    label: '映射目标列',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'mappingColType',
    label: '映射目标列类型',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'mappingColNum',
    label: '映射目标列列号',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'remarks',
    label: '注释字段',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'primarykey',
    label: '组成联合主键',
    component: 'Select',
    componentProps: {
       options:WHETHER_UNITEDKEY_LIST
    },
       value:'0',
       formItemProps: {
       rules: [required()],
    }
  },
  {
    field: 'primaryKeyNum',
    label: '主键排序',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'uuid',
    label: '标识是否为uuid',
    component: 'Select',
    componentProps: {
       options:WHETHER_UUID_LIST
    },
       value:'0',
       formItemProps: {
       rules: [required()],
    }
  }
])

const rules = reactive({
  modelCode: [required()],
  ruleId: [required()],
  srcCol: [required()],
})

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>

