<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 dayjs from 'dayjs'
import { OPERATION_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: '主键ID',
    component: 'Input',
    hidden: true
  },{
    field: 'orgNo',
    label: '机构金融许可证号',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'orgName',
    label: '机构编号',
    component: 'Select',
    componentProps: {
      
    },
  },
  {
    field: 'logRecordTime',
    label: '日志记录时间',
    component: 'DatePicker',
    componentProps: {
      
    },
  },
  {
    field: 'operationDate',
    label: '操作日期',
    component: 'DatePicker',
    componentProps: {
      
    },
  },
  {
    field: 'operationTime',
    label: '操作时间',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'userName',
    label: '操作账号',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'userIp',
    label: '操作IP',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'operationType',
    label: '操作类型',
    component: 'Select',
    componentProps: {
      options:OPERATION_TYPE_LIST
    },
  },
  {
    field: 'logDetail',
    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()
    const { logRecordTime, operationDate } = formData;
    Object.assign(formData, { logRecordTime: dayjs(logRecordTime).format('YYYY-MM-DD') })
    Object.assign(formData, { operationDate: dayjs(operationDate).format('YYYY-MM-DD') })
    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>

