<script setup lang="ts">
import {useI18n} from "vue-i18n";
import {ref} from "vue";
import type {IAppEtagController} from "@/model/table/IAppEtagController.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import EtagBaseApi from "@/api/etagBase.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<IAppEtagController>({ controllerId: 1, controllerStatus: 1, controllerType: '上尚'}); // 要添加的资料

// 添加
const add = () => {
  MessageUtils.confirmMessageBox("确定添加此数据", "添加确认").then(() => {
    const loadingInstance = MessageUtils.loading('请稍后');
    EtagBaseApi.addEtagControllerInfo(formData.value).then(res => {
      const responseString = JSON.stringify(res.data);
      const response = JSON.parse(responseString) as AppServeResponse;
      if (response && 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.etagController.addEtagController')" @close="close" :close-on-click-modal="false">
      <el-scrollbar style="height:50vh">
        <el-form :model="formData" label-position="right" label-width="120px" require-asterisk-position="right">
          <el-form-item :label="t('tabPage.etagController.controllerId') + '：'" required>
            <el-input-number v-model="formData.controllerId" :min="1" :max="3000"/>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagController.controllerName') + '：'" required>
            <el-input v-model="formData.controllerName" placeholder=""></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagController.ip') + '：'" required>
            <el-input v-model="formData.ip" placeholder=""></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagController.port') + '：'" required>
            <el-input v-model="formData.port" placeholder=""></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagController.type') + '：'" required>
            <el-input v-model="formData.controllerType" placeholder="" disabled></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagController.status') + '：'" required>
            <el-switch v-model="formData.controllerStatus" inline-prompt active-text="启用" :active-value="1" inactive-text="停用" :inactive-value="0"></el-switch>
          </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>