<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 { getDictOneApi } from '@/api/common'
import useSystemVersion from '@/hooks/web/useSystemVersion';

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 { formItem } = useSystemVersion(formMethods, { disabled: props.actionType === 'edit' }, props);

const formSchema = reactive<FormSchema[]>([
  ...formItem,

  {
    field: 'reportCode',
    label: '报表编码',
    component: 'Input',
    componentProps: {
      disabled: props.actionType === 'edit'
    },
  },
  {
    field: 'reportName',
    label: '报表名称',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'reportVersion',
    label: '报表版本',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'reportUnit',
    label: '报表单位',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'reportFrequency',
    label: '报表频度',
    component: 'Select',
    optionApi: async () => {
      const res = await getDictOneApi({
        paramName: 'DataStoreFrequency_param',
        systemCode: 'system'
      })
      return res
    },
    componentProps: {
      
    },
  },
  {
    field: 'itemName',
    label: '指标名称',
    component: 'Input',
    componentProps: {
      
    },
  },
  {
    field: 'itemType',
    label: '指标类型',
    component: 'Select',
    optionApi: async () => {
      const res = await getDictOneApi({
        paramName: 'IndexItemType_param',
        systemCode: 'system'
      })
      return res
    },
    componentProps: {
      
    },
  },
  {
    field: 'itemRow',
    label: '指标行坐标',
    component: 'Input',
    componentProps: {
      disabled: props.actionType === 'edit'
    },
  },
  {
    field: 'itemColumn',
    label: '指标列坐标',
    component: 'Input',
    componentProps: {
      disabled: props.actionType === 'edit'
    },
  }
])

const rules = reactive({
  reportCode: [required()],
  reportName: [required()],
  reportVersion: [required()],
  reportFrequency: [required()],
  itemName: [required()],
  itemType: [required()],
  itemRow: [required()],
  reportUnit: [required()],
  itemColumn: [required()],
  system_version:[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>

