<template>
  <basicModal
    @register="modalRegister"
    ref="modalRef"
    class="basicModal basicFormModal"
    @on-ok="handleSubmit"
    @on-close="handleClose"
  >
    <template #default>
      <n-form
        class="ls-form"
        ref="formRef"
        :model="formData"
        label-placement="left"
        label-width="110px"
      >
        <n-form-item
          label="数据源名称"
          path="name"
          :rule="{ required: true, message: '请输入数据源名称', trigger: 'blur' }"
        >
          <n-input v-model:value="formData.name" placeholder="请输入数据源名称" clearable />
        </n-form-item>
        <n-form-item
          label="数据源编码"
          path="code"
          :rule="{ required: true, message: '请输入数据源编码', trigger: 'blur' }"
        >
          <n-input v-model:value="formData.code" placeholder="请输入数据源编码" clearable />
        </n-form-item>
        <n-form-item
          label="数据库类型"
          path="dbType"
          :rule="{ type: 'number', required: true, message: '请选择数据库类型', trigger: 'change' }"
        >
          <n-select
            v-model:value="formData.dbType"
            placeholder="请选择数据库类型"
            :options="dbTypeList"
          />
        </n-form-item>
        <n-form-item
          label="数据库驱动类"
          path="dbDriver"
          :rule="{ required: true, message: '请输入数据库驱动类', trigger: 'blur' }"
        >
          <n-input v-model:value="formData.dbDriver" placeholder="请输入数据库驱动类" clearable />
        </n-form-item>
        <n-form-item
          label="数据源地址"
          path="dbUrl"
          :rule="{ required: true, message: '请输入数据源地址', trigger: 'blur' }"
        >
          <n-input v-model:value="formData.dbUrl" placeholder="请输入数据源地址" clearable />
        </n-form-item>
        <n-form-item
          label="数据库名称"
          path="dbName"
          :rule="{ required: true, message: '请输入数据库名称', trigger: 'blur' }"
        >
          <n-input v-model:value="formData.dbName" placeholder="请输入数据库名称" clearable />
        </n-form-item>
        <n-form-item
          label="数据库用户名"
          path="dbUsername"
          :rule="{ required: true, message: '请输入数据库用户名', trigger: 'blur' }"
        >
          <n-input v-model:value="formData.dbUsername" placeholder="请输入数据库用户名" clearable />
        </n-form-item>
        <n-form-item
          label="数据库密码"
          path="dbPassword"
          :rule="{ required: true, message: '请输入数据库密码', trigger: 'blur' }"
        >
          <n-input
            type="password"
            v-model:value="formData.dbPassword"
            show-password-on="mousedown"
            placeholder="请输入数据库密码"
            clearable
          />
        </n-form-item>
        <n-form-item label="备注" path="note">
          <n-input v-model:value="formData.note" type="textarea" />
        </n-form-item>
      </n-form>
    </template>
  </basicModal>
</template>
<script lang="ts" setup>
  import { getDataSourceDetail, dataSourceAdd, dataSourceUpdate } from '@/api/monitor/dataSource';
  import { onMounted, reactive, ref } from 'vue';
  import { useMessage } from 'naive-ui';
  import { useModal } from '@/components/Modal';

  const emit = defineEmits(['success', 'update:visible']);
  const formRef = ref();

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

  /**
   * 定义接收的参数
   */
  const props = defineProps({
    visible: {
      type: Boolean,
      required: true,
      default: false,
    },
    dataSourceId: {
      type: Number,
      required: true,
      default: 0,
    },
  });
  const [modalRegister, { openModal, setSubLoading }] = useModal({
    title: props.dataSourceId ? '编辑数据源' : '添加数据源',
    subBtuText: '确定',
    width: 600,
  });
  const dbTypeList = [
    { label: 'MySQL', value: 1 },
    { label: 'PostgreSQL', value: 2 },
    { label: 'SQLServer', value: 3 },
    { label: 'Oracle', value: 4 },
    { label: 'Sqlite', value: 5 },
  ];
  /**
   * 执行提交
   */
  const handleSubmit = async () => {
    formRef.value
      .validate()
      .then(async () => {
        props.dataSourceId ? await dataSourceUpdate(formData) : await dataSourceAdd(formData);
        message.success('操作成功');
        setSubLoading(false);
        emit('update:visible', false);
        emit('success');
      })
      .catch((error) => {
        setSubLoading(false);
      });
  };

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

  /**
   * 设置表单数据
   */
  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();
    }
  });
  //导出方法
  defineExpose({
    openModal,
  });
</script>
