<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: 'pkid',
    label: '序号',
    component: 'Input',
    hidden: true
  },{
    field: 'productCode',
    label: '产品号',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'productName',
    label: '产品名称',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'organCode',
    label: '所属机构号',
    component: 'Select',
    componentProps: {
      
    },
  },
  {
    field: 'organName',
    label: '所属机构名称',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'isman',
    label: '是否人工发起',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'manDate',
    label: '人工发起日期',
    component: 'DatePicker',
    componentProps: {
      type: 'daterange'
    },
  },
  {
    field: 'manRiskReason',
    label: '人工发起评估原因',
    component: 'Select',
    componentProps: {
      
    },
  },
  {
    field: 'gatherDate',
    label: '采集日期',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'gatherScore',
    label: '采集分数',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'lastRiskDate',
    label: '上次评级日期',
    component: 'DatePicker',
    componentProps: {
      type: 'daterange'
    },
  },
  {
    field: 'lastRiskScore',
    label: '上一轮评级分数',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'riskInitialDate',
    label: '初评日期',
    component: 'DatePicker',
    componentProps: {
      type: 'daterange'
    },
  },
  {
    field: 'riskInitialLevel',
    label: '初评结果',
    component: 'Select',
    componentProps: {
      
    },
  },
  {
    field: 'riskRepeatDate',
    label: '复评日期',
    component: 'Select',
    componentProps: {
      
    },
  },
  {
    field: 'riskRepeatLevel',
    label: '复评结果',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'riskRepeatUser',
    label: '复评人',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'riskInitialUser',
    label: '初评人',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'statusNo',
    label: '处理状态',
    component: 'Select',
    componentProps: {
      
    },
  },
  {
    field: 'nextDate',
    label: '下次评级日期',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'riskScore',
    label: '当前评级分数',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'riskLevel',
    label: '风险等级',
    component: 'Select',
    componentProps: {
      
    },
  },
  {
    field: 'lastRiskLevel',
    label: '上次评级结果',
    component: 'Select',
    componentProps: {
      
    },
  },
  {
    field: 'gatherLevel',
    label: '采集等级',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'riskInitialScore',
    label: '初评分数',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'riskRepeatScore',
    label: '复评分数',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'riskReason',
    label: '评估原因',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'repeatReason',
    label: '复评退回原因',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'launchDate',
    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>

