<script setup lang="ts">
import {useI18n} from "vue-i18n";
import type {IAppEtagController} from "@/model/table/IAppEtagController.ts";
import {computed} from "vue";
import MessageUtils from "@/utils/MessageUtils.ts";
import EtagBaseApi from "@/api/etagBase.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";
import type {AppServeResponse} from "@/interface/api/AppServeResponse.ts";

const modelValue = defineModel('modelValue', {required: true, default: false});
const emit = defineEmits(['reLoadingTableData']);
const close = () => {modelValue.value = false;};
const props = defineProps<{
  formData: IAppEtagController;
}>();
const { t } = useI18n();
const formData = computed(() => props.formData);

const save = () => {
  MessageUtils.confirmMessageBox('确定保存新数据？', '保存').then(() => {
    const loading = MessageUtils.loading("正在保存");
    EtagBaseApi.updateEtagControllerInfo(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(() => {
      loading.close();
    })
  });
};

const deleteData = () => {
  MessageUtils.confirmMessageBox('确定删除该数据？', '删除确认').then(() => {
    const loading = MessageUtils.loading("正在删除");
    EtagBaseApi.deleteEtagControllerInfo(formData.value.controllerId).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(() => {
      loading.close();
    })
  });
};

</script>

<template>
  <div>
    <el-dialog :model-value="modelValue" :title="t('tabPage.etagController.editEtagController')" @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" disabled/>
          </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=""></el-input>
          </el-form-item>
          <el-form-item :label="t('baseColName.createTime') + '：'" required>
            <el-input v-model="formData.createTime" placeholder="" disabled></el-input>
          </el-form-item>
          <el-form-item :label="t('baseColName.updateTime') + '：'" required>
            <el-input v-model="formData.updateTime" 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="save">{{t('baseButton.save')}}</el-button>
          <el-button type="warning" @click="deleteData">{{t('baseButton.delete')}}</el-button>
          <el-button type="danger" @click="close">{{t('baseButton.close')}}</el-button>
        </el-button-group>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>