<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 { DB, DSTYPE_DICT_LIST } from '../constants'

const { required } = useValidator()

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

const formSchema = reactive<FormSchema[]>([
  {
    field: 'dscode',
    label: '编码',
    component: 'Input',
    componentProps: {}
  },
  {
    field: 'dsname',
    label: '名称',
    component: 'Input',
    componentProps: {}
  },
  {
    field: 'dstype',
    label: '类型',
    component: 'Select',
    componentProps: {
      options: DSTYPE_DICT_LIST,
      on: {
        change: (dstype: number) => {
          if (DB[dstype + '']) {
            setValues(DB[dstype + ''])
          }
        }
      }
    }
  },
  {
    field: 'dsdriver',
    label: '驱动类',
    component: 'Input',
    componentProps: {}
  },
  {
    field: 'dsurl',
    label: 'jdbc链接',
    component: 'Input',
    componentProps: {}
  },
  {
    field: 'username',
    label: '用户名',
    component: 'Input',
    componentProps: {}
  },
  {
    field: 'userpwd',
    label: '密码',
    component: 'Input',
    componentProps: {
      type: 'password'
    }
  },
  {
    field: 'defaultschema',
    label: '默认schema',
    component: 'Input',
    componentProps: {}
  },
  {
    field: 'initialsize',
    label: '初始化链接数',
    component: 'InputNumber',
    componentProps: {}
  },
  {
    field: 'minidle',
    label: '最小空闲链接数',
    component: 'InputNumber',
    componentProps: {}
  },
  {
    field: 'maxactive',
    label: '最大空闲链接数',
    component: 'InputNumber',
    componentProps: {}
  },
  {
    field: 'removeabandoned',
    label: '是否开启链接遗漏检测',
    component: 'Switch',
    componentProps: {
      inlinePrompt: true,
      activeText: '是',
      activeValue: 1,
      inactiveText: '否',
      inactiveValue: 0
    }
  },
  {
    field: 'removeabandonedtimeout',
    label: '链接活跃时间(秒)',
    component: 'InputNumber',
    componentProps: {}
  },
  {
    field: 'maxwait',
    label: '链接超时时间(毫秒)',
    component: 'InputNumber',
    componentProps: {}
  },
  {
    field: 'timebetweenevictionrunsmillis',
    label: '检测间隔时间(毫秒)',
    component: 'InputNumber',
    componentProps: {}
  },
  {
    field: 'poolpreparedstatements',
    label: '是否开启缓存游标',
    component: 'Switch',
    componentProps: {
      inlinePrompt: true,
      activeText: '是',
      activeValue: 1,
      inactiveText: '否',
      inactiveValue: 0
    }
  }
])

const rules = reactive({
  dscode: [required()],
  dsname: [required()],
  dstype: [required()],
  dsdriver: [required()],
  dsurl: [required()],
  username: [required()],
  userpwd: [required()],
  defaultschema: [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
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  submit
})
</script>

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