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