<template>
  <a-modal
    v-model:visible="props.visible"
    :title="props.dataSourceId ? '编辑' : '新增'"
    width="600px"
    @cancel="dialogClose"
  >
    <a-form
      class="ls-form"
      ref="formRef"
      :model="formData"
      :label-col="{ style: { width: '110px' } }"
    >
      <a-form-item
        label="数据源名称"
        name="name"
        :rules="{ required: true, message: '请输入数据源名称', trigger: 'blur' }"
      >
        <a-input v-model:value="formData.name" placeholder="请输入数据源名称" allow-clear />
      </a-form-item>
      <a-form-item
        label="数据源编码"
        name="code"
        :rules="{ required: true, message: '请输入数据源编码', trigger: 'blur' }"
      >
        <a-input v-model:value="formData.code" placeholder="请输入数据源编码" allow-clear />
      </a-form-item>
      <a-form-item
        label="数据库类型"
        name="dbType"
        :rules="{ required: true, message: '请选择数据库类型', trigger: 'blur' }"
      >
        <a-select v-model:value="formData.dbType" placeholder="请选择数据库类型">
          <a-select-option :value="1">MySQL</a-select-option>
          <a-select-option :value="2">PostgreSQL</a-select-option>
          <a-select-option :value="3">SQLServer</a-select-option>
          <a-select-option :value="4">Oracle</a-select-option>
          <a-select-option :value="5">Sqlite</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item
        label="数据库驱动类"
        name="dbDriver"
        :rules="{ required: true, message: '请选择数据库驱动类', trigger: 'blur' }"
      >
        <a-input v-model:value="formData.dbDriver" placeholder="请选择数据库驱动类" allow-clear />
      </a-form-item>
      <a-form-item
        label="数据源地址"
        name="dbUrl"
        :rules="{ required: true, message: '请输入数据源地址', trigger: 'blur' }"
      >
        <a-input v-model:value="formData.dbUrl" placeholder="请输入数据源地址" allow-clear />
      </a-form-item>
      <a-form-item
        label="数据库名称"
        name="dbName"
        :rules="{ required: true, message: '请输入数据库名称', trigger: 'blur' }"
      >
        <a-input v-model:value="formData.dbName" placeholder="请输入数据库名称" allow-clear />
      </a-form-item>
      <a-form-item
        label="数据库用户名"
        name="dbUsername"
        :rules="{ required: true, message: '请输入数据库用户名', trigger: 'blur' }"
      >
        <a-input v-model:value="formData.dbUsername" placeholder="请输入数据库用户名" allow-clear />
      </a-form-item>
      <a-form-item
        label="数据库密码"
        name="dbPassword"
        :rules="{ required: true, message: '请输入数据库密码', trigger: 'blur' }"
      >
        <a-input-password
          v-model:value="formData.dbPassword"
          show-password
          placeholder="请输入数据库密码"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="备注" name="note">
        <a-input v-model:value="formData.note" type="textarea" />
      </a-form-item>
    </a-form>
    <template #footer>
      <span class="dialog-footer">
        <a-button @click="dialogClose">取消</a-button>
        <a-button :loading="subLoading" type="primary" @click="submit"> 确定 </a-button>
      </span>
    </template>
  </a-modal>
</template>
<script lang="ts" setup>
  import type { FormInstance } from 'ant-design-vue';
  import { getDataSourceDetail, dataSourceAdd, dataSourceUpdate } from '@/api/monitor/dataSource';
  import { onMounted, reactive, shallowRef } from 'vue';
  import { message } from 'ant-design-vue';
  import { useLockFn } from '@/utils/useLockFn';

  /**
   * 定义参数变量
   */
  const emit = defineEmits(['success', 'update:visible']);
  const formRef = shallowRef<FormInstance>();

  /**
   * 定义表单参数
   */
  const formData = reactive({
    id: '',
    name: '',
    code: '',
    dbType: '',
    dbDriver: '',
    dbUrl: '',
    dbName: '',
    dbUsername: '',
    dbPassword: '',
    note: '',
  });

  /**
   * 定义接收的参数
   */
  const props = defineProps({
    visible: {
      type: Boolean,
      required: true,
      default: false,
    },
    dataSourceId: {
      type: Number,
      required: true,
      default: 0,
    },
  });

  /**
   * 执行提交表单
   */
  const handleSubmit = async () => {
    await formRef.value?.validate();
    props.dataSourceId ? await dataSourceUpdate(formData) : await dataSourceAdd(formData);
    message.success('操作成功');
    emit('update:visible', false);
    emit('success');
  };

  /**
   * 关闭窗体
   */
  const dialogClose = () => {
    emit('update:visible', false);
  };

  const { isLock: subLoading, lockFn: submit } = useLockFn(handleSubmit);

  /**
   * 设置表单数据
   */
  const setFormData = async () => {
    const data = await getDataSourceDetail(props.dataSourceId);
    for (const key in formData) {
      if (data[key] != null && data[key] != undefined) {
        //@ts-ignore
        formData[key] = data[key];
      }
    }
  };

  /**
   * 钩子函数
   */
  onMounted(() => {
    if (props.dataSourceId) {
      setFormData();
    }
  });
</script>
