<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 { getIndexType } from '@/api/dataset/RepIndexSet';
import { transfDictList } from '@/utils';

const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<any>,
    default: () => null
  },
  actionType: {
    type: String,
    default: 'add'
  },
  indexTypeParam:{
    type:Object,
    default:()=>[]
  }
})

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

const formSchema = reactive<FormSchema[]>([
  {
    field: 'indexsetCode',
    label: '指标集编码',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'beginDate',
    label: '开始日期',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'endDate',
    label: '结束日期',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'indexsetName',
    label: '指标集名称',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'organCode',
    label: '机构编码',
    component: 'Input',
    hidden:true,
    componentProps: {
      
    },
  },
  {
    field: 'frequency',
    label: '频度',
        optionApi: async () => {
               const res = await getIndexType({ paramName: 'frequency_param', systemCode: 'ordb' });
               return transfDictList(res.body.result);
        },
        value: '1',
        component: 'Select'
  },
  {
    field: 'indexsetType',
    label: '指标集类型',
    componentProps: {
      // checkStrictly: true,
      nodeKey: 'id',
      props: { children: 'childs', label: 'nodeName' },
      filterable: true,
    //  multiple: true,
      collapseTags: true,
      // showCheckbox: true,
      style: { minWidth: '200px' },
      checkOnClickNode: true,
    },
    component: 'TreeSelect',
    optionApi: async () => {

      return props.indexTypeParam;
    }
  },
  {
    field: 'modelCode',
    label: '数据模型编码',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'createOrgan',
    label: '创建机构',
    component: 'Input',
    componentProps: {
      
    },
    hidden:true,
  },
  {
    field: 'createUser',
    label: '创建用户',
    component: 'Input',
    componentProps: {
      
    },
    hidden:true,
  },
   {
    field: 'description',
    label: '描述',
    component: 'Input',
    componentProps: {
      
    },
  },
])

const rules = reactive({
  indexsetCode: [required()],
  beginDate: [required()],
  endDate: [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>

