<script lang="ts">
// 声明额外的选项
export default {
  name: "Add"
};
</script>
<script setup lang="ts">
import { inject, onMounted, reactive, ref, Uploadfile, UploadImg, Editor, useRenderIcon, ElMessage, ElMessageBox, Upload, Close, http, getGroupUrl, RegularVerification, verification } from "@/utils/importUsed"
// 接口列表实例
let UrlList = reactive(null)
// 获取当前板块接口列表
onMounted(async () => {
  UrlList = await getGroupUrl(["prizeSet"]);
  projectIdQuery();

})
// 组件传参对象
const props = defineProps<{
  submit: Function;
  addVisible: {
    type: Boolean;
    default: false;
  };
  width: {
    type: Number;
    default: 50;
  };
  projectId: {
    type: Number;
    default: 0;
  },

}>();
// 表单数据
let UpdateForm = ref({
  title: "", //数据库名称
  ipAddress: "", //IP地址
  port: null, //端口
  userId: "", //用户名
  password: "", //密码
  databaseName: "", //数据库名
  projectId: props.projectId, //项目


});
// 表单实例
const ruleFormRef = ref()

// 传参选项数据
// 项目选项数据
const projectIdOptionList = ref([]);

// 选项卡参数(默认值为列表某项的id)
const activeId = ref('1')
// 提交函数
const submit = async (formEl) => {
  // 表单校验拦截
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      //表单校验成功回调
      console.log('submit!')


      // 需动态生成接口
      const { status, msg }: any = await http.Request({
        method: UrlList.prizeSet.priaddPriDatabaseSet.method,
        url: UrlList.prizeSet.priaddPriDatabaseSet.url,
        params: UpdateForm.value
      });
      if (status === 1) {
        //业务成功回调
        ElMessage({
          message: "新增成功",
          type: "success"
        });
        UpdateForm.value = {
          title: "", //数据库名称
          ipAddress: "", //IP地址
          port: null, //端口
          userId: "", //用户名
          password: "", //密码
          databaseName: "", //数据库名
          projectId: props.projectId, //项目

        };
        // 关闭新增弹窗;
        closeVisible()
      } else {
        //业务失败回调
        ElMessageBox.alert(msg, "提示", {
          confirmButtonText: "关闭",
          type: "warning"
        });
      }
    } else {
      //表单校验失败回调
      ElMessage({
        message: "请输入完整信息",
        type: "error"
      });
    }
  })
};

//获取项目数据
const projectIdQuery = async () => {
  const { status, data }: any = await http.Request({ method: UrlList.prizeSet.prigetPriProjectDic.method, url: UrlList.prizeSet.prigetPriProjectDic.url, params: {} });
  if (status === 1) {
    projectIdOptionList.value = data.records;
  }
};



// 表单校验规则
const rules = reactive({
  ipAddress: [
    { required: true, message: '请输入IP地址', trigger: 'blur' },
  ],
  port: [
    { required: true, message: '请输入端口', trigger: 'blur' },
  ],
  userId: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ],
  projectId: [
    { required: true, message: '请输入项目', trigger: 'blur' },
  ],

})
// 关闭弹窗回调函数
const closeFn: any = inject("closeAddVisible");
const closeVisible = () => {
  // 清空表单项;
  UpdateForm.value = {
    title: "", //数据库名称
    ipAddress: "", //IP地址
    port: null, //端口
    userId: "", //用户名
    password: "", //密码
    databaseName: "", //数据库名
    projectId: props.projectId, //项目

  };
  closeFn();
};

// 弹窗是否全屏
const isFullscreen = ref(false)
</script>

<template lang="pug">
.main
  el-dialog(v-model='props.addVisible' width="50%" :fullscreen="isFullscreen" title="新增" draggable @close="closeVisible")
    el-form(:model='UpdateForm' label-position="right" ref="ruleFormRef" :rules="rules" label-width="100px")
      el-form-item(label='数据库名称' prop="title")
        el-input(v-model='UpdateForm.title' autocomplete='off'
          placeholder="请输入数据库名称")
      el-form-item(label='IP地址' prop="ipAddress")
        el-input(v-model='UpdateForm.ipAddress' autocomplete='off'
          placeholder="请输入IP地址")
      el-form-item(label='端口' prop="port")
        el-input-number(v-model='UpdateForm.port' :min="0" :max="100000"
          placeholder="请输入端口")
      el-form-item(label='用户名' prop="userId")
        el-input(v-model='UpdateForm.userId' autocomplete='off'
          placeholder="请输入用户名")
      el-form-item(label='密码' prop="password")
        el-input(v-model='UpdateForm.password' autocomplete='off'
          placeholder="请输入密码")
      el-form-item(label='数据库名' prop="databaseName")
        el-input(v-model='UpdateForm.databaseName' autocomplete='off'
          placeholder="请输入数据库名")
      el-form-item(label="项目", prop="projectId")
        el-select(
          v-model="UpdateForm.projectId",
          placeholder="请选择项目",
          clearable,
        )
          el-option(:label="item.projectName", :value="item.id" v-for="(item,index) in projectIdOptionList")

    .flex.justify-end
      el-button(
        :icon="useRenderIcon(Close)",
        @click="closeVisible"
      ) 关闭
      el-button(
        type="primary",
        :icon="useRenderIcon(Upload)",
        @click="submit(ruleFormRef)"
      ) 确认提交
</template>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}

:deep(.el-form-item__label) {
  font-weight: 700;
}

:deep(.el-pagination) {
  flex-flow: wrap;
}

:deep(.is-draggable) {
  max-height: 80vh;
  overflow: auto;
}

:deep(.el-dialog__header) {
  position: sticky;
  top: 0;
  z-index: 2;
  background: #fff;
}

.collapsedom {
  padding: 0 20px;
  background-color: #fff;
}

.ovh-x {
  height: 40vh;
  overflow-y: auto;
}

:deep(.el-descriptions__header) {
  margin: 16px 0 !important;
}

.el-select {
  width: 100%;
}
</style>
