<script setup lang="ts">

import type {IAppBaseDb} from "@/model/table/IAppBaseDb.ts";
import {ref} from "vue";
import {useI18n} from "vue-i18n";
import MessageUtils from "@/utils/MessageUtils.ts";
import SiemensDbApi from "@/api/siemensDb.ts";
import type {AppServeResponse} from "@/interface/api/AppServeResponse.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";

const modelValue = defineModel('modelValue', {required: true, default: false});
const emit = defineEmits(['reLoadingTableData']);
const close = () => {
  modelValue.value = false;
};
const { t } = useI18n();
const formData = ref<IAppBaseDb>({plcId: 1});

// 添加
const add = () => {
  if (!formData.value.dbName) {
    MessageUtils.warningMessageBox('请填写DB名称', '警告');
    return;
  }
  if (!formData.value.dbAddress) {
    MessageUtils.warningMessageBox('请填写DB地址', '警告');
    return;
  }
  if (!formData.value.plcId) {
    MessageUtils.warningMessageBox('请填写PLC ID', '警告');
    return;
  }
  MessageUtils.confirmMessageBox('确定添加以上数据？', '添加确认').then(() => {
    const loadingInstance = MessageUtils.loading();
    SiemensDbApi.addSiemensDb(formData.value).then((res) => {
      const response = JSON.parse(JSON.stringify(res.data)) as AppServeResponse;
      if (response.code == AppServeResponseCodeEnum.SUCCESS) {
        MessageUtils.successMessage('添加成功');
        emit('reLoadingTableData');
        close();
        return;
      }
      MessageUtils.warningMessageBox(response.msg, '警告');
    }).catch(() => {}).finally(() => {
      loadingInstance.close();
    })
  }).catch(()=>{});
};
</script>

<template>
  <div>
    <el-dialog :model-value="modelValue" :title="t('tabPage.siemensDbManage.addSiemensDbBaseData')" @close="close" :close-on-click-modal="false">
      <el-scrollbar style="height:50vh">
        <el-form :model="formData" label-position="right" label-width="100px" require-asterisk-position="right">
          <el-form-item :label="t('tabPage.siemensDbManage.dbName') + '：'" required>
            <el-input v-model="formData.dbName" placeholder=""></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.siemensDbManage.dbAddress') + '：'" required>
            <el-input v-model="formData.dbAddress" placeholder=""></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.siemensDbManage.plcId') + '：'" required>
            <el-input-number v-model="formData.plcId" :min="1" :max="3000"/>
            <el-text type="warning" style="font-size: 12px">{{t('baseTip.plcIdMatchedTip')}}</el-text>
          </el-form-item>
          <el-form-item :label="t('baseColName.remark') + '：'">
            <el-input v-model="formData.remark"></el-input>
          </el-form-item>
        </el-form>
      </el-scrollbar>
      <template #footer>
        <el-button-group>
          <el-button type="primary" @click="add">{{t('baseButton.add')}}</el-button>
          <el-button type="danger" @click="close">{{t('baseButton.close')}}</el-button>
        </el-button-group>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>