<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';
import {
  getRepDataModelListApi,
} from '@/api/dataset/RepDataModel'

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: {
      disabled:props.actionType == 'edit'
    },
  },
  {
    field: 'indexsetName',
    label: '指标集名称',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'beginDate',
    label: '开始日期',
    component: 'DatePicker',
    componentProps: {
      valueFormat: 'YYYYMMDD',
      style:{width:'100%'}
    },
    
  },
  {
    field: 'endDate',
    label: '结束日期',
    component: 'DatePicker',
    componentProps: {
      valueFormat: 'YYYYMMDD',
      style:{width:'100%'}
    },
  },
 
  {
    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',
    componentProps:{
      style:{width:'100%'}
    }
  },
  {
    field: 'indexsetType',
    label: '指标集类型',
    componentProps: {
      // checkStrictly: true,
      nodeKey: 'id',
      props: { children: 'childs', label: 'nodeName' },
      filterable: true,
    //  multiple: true,
      collapseTags: true,
      // showCheckbox: true,
      style:{width:'100%'},
      // checkOnClickNode: true,
    },
    component: 'TreeSelect',
    optionApi: async () => {
      return props.indexTypeParam;
    }
  },
  {
    field: 'modelCode',
    label: '数据模型编码',
    component: 'Select',
    componentProps: {
      
    },
    optionApi:async ()=>{
      let res = await getRepDataModelListApi({}).then(item=>{
        return item.body.list;
      });
      res = res.map(item=>{
        return {
          label:item.modelCode,
          value:item.modelCode
        }
      })
      return res;
    }
  },
  {
    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()],
  indexsetName: [required()],
  beginDate: [required()],
  endDate: [required()],
  frequency: [required()],
  indexsetType: [required()],
  modelCode: [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>

