<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 { REQ_STATUS_LIST,RECORD_TYPE_LIST } from "../constants"
import dayjs from 'dayjs'

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: 'dataDate',
    label: '批次日期(接口)',
    component: 'DatePicker',
    componentProps: {
      
    },
  },
  {
    field: 'workflowDataDate',
    label: '数据日期(工作流)',
    component: 'DatePicker',
    componentProps: {
      
    },
  },
  {
    field: 'workflowId',
    label: '工作流编码',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'dispatchTime',
    label: '下发时间戳',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'reqStatus',
    label: '请求状态',
    component: 'Select',
    componentProps: {
      options:REQ_STATUS_LIST
    },
  },
  {
    field: 'reqMessage',
    label: '请求报文',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'msg',
    label: '失败原因',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'createTime',
    label: '创建时间',
    component: 'DatePicker',
    componentProps: {
      
    },
  },
  {
    field: 'updateTime',
    label: '更新时间',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'timeConsume',
    label: '耗时',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'recordType',
    label: '记录类型',
    component: 'Select',
    componentProps: {
      options:RECORD_TYPE_LIST
    },
  }
])

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 { dataDate, workflowDataDate, createTime } = formData;
    Object.assign(formData, { dataDate: dayjs(dataDate).format('YYYY-MM-DD') })
    Object.assign(formData, { workflowDataDate: dayjs(workflowDataDate).format('YYYY-MM-DD') })
    Object.assign(formData, { createTime: dayjs(createTime).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>

