<script setup lang="tsx">
import { PropType, ref } from 'vue'
import { TableData } from '@/api/param/RepDataSource/types'
import { Descriptions, DescriptionsSchema } from '@/components/Descriptions'
import {
  DSTYPE_DICT_LIST,
  REMOVEABANDONED_DICT_LIST,
  POOLPREPAREDSTATEMENTS_DICT_LIST
} from '../constants'

const detailSchema = ref<DescriptionsSchema[]>([
  {
    field: 'dscode',
    label: '编码'
  },
  {
    field: 'dsname',
    label: '名称'
  },
  {
    field: 'dstype',
    label: '类型',
    slots: {
      default(row) {
        const dstype = DSTYPE_DICT_LIST.find((item) => item.value === row.dstype)
        if (dstype) {
          return <span>{dstype.label}</span>
        }
        return <span>{row.dstype ?? '-'}</span>
      }
    }
  },
  {
    field: 'dsdriver',
    label: '驱动类'
  },
  {
    field: 'dsurl',
    label: 'jdbc链接'
  },
  {
    field: 'username',
    label: '用户名'
  },
  {
    field: 'userpwd',
    label: '密码'
  },
  {
    field: 'defaultschema',
    label: '默认schema'
  },
  {
    field: 'initialsize',
    label: '初始化链接数'
  },
  {
    field: 'minidle',
    label: '最小空闲链接数'
  },
  {
    field: 'maxactive',
    label: '最大空闲链接数'
  },
  {
    field: 'removeabandoned',
    label: '是否开启链接遗漏检测',
    slots: {
      default(row) {
        const removeabandoned = REMOVEABANDONED_DICT_LIST.find(
          (item) => item.value === row.removeabandoned
        )
        if (removeabandoned) {
          return <span>{removeabandoned.label}</span>
        }
        return <span>{row.removeabandoned ?? '-'}</span>
      }
    }
  },
  {
    field: 'removeabandonedtimeout',
    label: '链接活跃时间(秒)'
  },
  {
    field: 'maxwait',
    label: '链接超时时间(毫秒)'
  },
  {
    field: 'timebetweenevictionrunsmillis',
    label: '检测间隔时间(毫秒)'
  },
  {
    field: 'poolpreparedstatements',
    label: '是否开启缓存游标',
    slots: {
      default(row) {
        const poolpreparedstatements = POOLPREPAREDSTATEMENTS_DICT_LIST.find(
          (item) => item.value === row.poolpreparedstatements
        )
        if (poolpreparedstatements) {
          return <span>{poolpreparedstatements.label}</span>
        }
        return <span>{row.poolpreparedstatements ?? '-'}</span>
      }
    }
  }
])

defineProps({
  currentRow: {
    type: Object as PropType<TableData>,
    default: () => {}
  }
})
</script>

<template>
  <Descriptions :schema="detailSchema" :data="currentRow || {}" />
</template>
