<template>
  <w-dialog
    ref="dialogRef"
    :title="$t('system.datasource.grid.toolbar.connectionProperties')"
    width="900px"
    :can-maximize="false"
    :buttons="[
      {
        label: $t('submit'),
        noCaps: true,
        click: () => {
          axios.post(Environment.apiContextPath('/api/system/datasource/connectionProperties/' + id), formRef.getData()).then((response) => {
            close();
            NotifyManager.success();
          });
        },
      },
    ]"
  >
    <w-form
      ref="formRef"
      :cols-num="2"
      :fields="[
        {
          name: 'name',
          label: 'name',
          hidden: true,
        },
        {
          name: 'autoCommit',
          label: $t('system.datasource.hikari.autoCommit'),
          type: 'select',
          options: ['', 'true', 'false'],
        },
        {
          name: 'connectionTimeout',
          label: $t('system.datasource.hikari.connectionTimeout'),
          type: 'text',
        },
        {
          name: 'idleTimeout',
          label: $t('system.datasource.hikari.idleTimeout'),
          type: 'text',
        },
        {
          name: 'maxLifetime',
          label: $t('system.datasource.hikari.maxLifetime'),
          type: 'text',
        },
        {
          name: 'connectionTestQuery',
          label: $t('system.datasource.hikari.connectionTestQuery'),
          type: 'text',
        },
        {
          name: 'minimumIdle',
          label: $t('system.datasource.hikari.minimumIdle'),
          type: 'text',
        },
        {
          name: 'maximumPoolSize',
          label: $t('system.datasource.hikari.maximumPoolSize'),
          type: 'text',
        },
        {
          name: 'metricRegistry',
          label: $t('system.datasource.hikari.metricRegistry'),
          type: 'text',
        },
        {
          name: 'healthCheckRegistry',
          label: $t('system.datasource.hikari.healthCheckRegistry'),
          type: 'text',
        },
        {
          name: 'poolName',
          label: $t('system.datasource.hikari.poolName'),
          type: 'text',
        },
        {
          name: 'initializationFailTimeout',
          label: $t('system.datasource.hikari.initializationFailTimeout'),
          type: 'text',
        },
        {
          name: 'isolateInternalQueries',
          label: $t('system.datasource.hikari.isolateInternalQueries'),
          type: 'select',
          options: ['', 'true', 'false'],
        },
        {
          name: 'allowPoolSuspension',
          label: $t('system.datasource.hikari.allowPoolSuspension'),
          type: 'select',
          options: ['', 'true', 'false'],
        },
        {
          name: 'readOnly',
          label: $t('system.datasource.hikari.readOnly'),
          type: 'select',
          options: ['', 'true', 'false'],
        },
        {
          name: 'registerMbeans',
          label: $t('system.datasource.hikari.registerMbeans'),
          type: 'select',
          options: ['', 'true', 'false'],
        },
        {
          name: 'catalog',
          label: $t('system.datasource.hikari.catalog'),
          type: 'text',
        },
        {
          name: 'connectionInitSql',
          label: $t('system.datasource.hikari.connectionInitSql'),
          type: 'text',
        },
        {
          name: 'transactionIsolation',
          label: $t('system.datasource.hikari.connectionInitSql'),
          type: 'select',
          options: ['', 'TRANSACTION_READ_COMMITTED', 'TRANSACTION_REPEATABLE_READ', ''],
        },
        {
          name: 'validationTimeout',
          label: $t('system.datasource.hikari.validationTimeout'),
          type: 'text',
        },
        {
          name: 'leakDetectionThreshold',
          label: $t('system.datasource.hikari.leakDetectionThreshold'),
          type: 'text',
        },
        {
          name: 'schema',
          label: $t('system.datasource.hikari.schema'),
          type: 'text',
        },
      ]"
      class="p-2"
    ></w-form>
  </w-dialog>
</template>
<script setup lang="ts">
import { nextTick, ref } from 'vue';
import { axios, Environment, NotifyManager } from 'platform-core';

const dialogRef = ref();
const formRef = ref();
let id = '';

const open = (datasourceId: string) => {
  id = datasourceId;
  dialogRef.value.show();
  nextTick(() => {
    axios.get(Environment.apiContextPath('/api/system/datasource/connectionProperties/' + datasourceId)).then((response) => {
      formRef.value.setData(response.data);
    });
  });
};

const close = () => {
  dialogRef.value.hide();
};

defineExpose({
  open,
  close,
});
</script>
