<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, reactive, watch, ref } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'

const { required } = useValidator()

const actionType = ref('add')

const props = defineProps({
  currentRow: {
    type: Object as PropType<any>,
    default: () => null
  }
})

const formSchema = reactive<FormSchema[]>([
  {
    field: 'orgNo',
    label: '金融许可证号',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    }
  },
  {
    field: 'orgName',
    label: '机构名称',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    }
  },
  {
    field: 'collectingCode',
    label: '采集编号',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    }
  },
  {
    field: 'regulationId',
    label: '制度编号',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    }
  },
  {
    field: 'regulationName',
    label: '制度名称',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    }
  },
  {
    field: 'regulationVersion',
    label: '制度版本号',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    }
  },
  {
    field: 'dataDate',
    label: '数据日期',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    }
  },
  {
    field: 'rereportNum',
    label: '重报次数',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    }
  },
  {
    field: 'reportDt',
    label: '报送日期',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    }
  },
  {
    field: 'reportStatus',
    label: '报送状态',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    }
  },
  {
    field: 'tabLevel',
    label: '二级分类',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    }
  },
  {
    field: 'tabNameEn',
    label: '表英文名',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    }
  },
  {
    field: 'tabName',
    label: '表中文名',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    }
  },
  {
    field: 'tabGuid',
    label: '表唯一性编号',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    }
  },
  {
    field: 'tabType',
    label: '表三级分类',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    }
  },
  {
    field: 'volume',
    label: '表数据总量',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    }
  },
  {
    field: 'filePathList',
    label: '上报文件存放全路径',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    }
  },
  {
    field: 'updateTime',
    label: '本条数据创建或更新时间',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    }
  },
  {
    field: 'recordType',
    label: '本条数据的记录类型',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    }
  },
  {
    field: 'workflowId',
    label: '任务ID',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    }
  }
])

const rules = reactive({
  component: [required()],
  path: [required()],
  orgNo: [required()],
  orgName: [required()],
  collectingCode: [required()],
  regulationId: [required()],
  regulationName: [required()],
  regulationVersion: [required()],
  dataDate: [required()],
  rereportNum: [required()],
  reportDt: [required()],
  reportStatus: [required()],
  tabLevel: [required()],
  tabNameEn: [required()],
  tabName: [required()],
  tabGuid: [required()],
  tabType: [required()],
  volume: [required()],
  filePathList: [required()],
  updateTime: [required()],
  recordType: [required()],
  workflowId: [required()]
})

const { formRegister, formMethods } = useForm()
const { setValues, getFormData, getElFormExpose } = formMethods

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
    actionType.value = 'update'
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  submit
})
</script>

<template>
  <Form :rules="rules" @register="formRegister" :schema="formSchema" />
</template>
