<script setup lang="ts">

import MessageUtils from "@/utils/MessageUtils.ts";
import {PlcApi} from "@/api/plc.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 props = defineProps(['formData'])
const close = () => {
  modelValue.value = false;
};
// 保存
const save = () => {
  MessageUtils.confirmMessageBox('确定修改以上数据？', '操作询问').then(() => {
    const loadingInstance = MessageUtils.loading();
    PlcApi.updatePlc(props.formData).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(()=>{})
};
// 删除数据
const deleteData = () => {
  MessageUtils.confirmMessageBox('确定删除数据？高风险操作，执行后不可恢复', '操作询问').then(() => {
    const loadingInstance = MessageUtils.loading();
    PlcApi.deletePlc(props.formData.plcId).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="查看/编辑 PLC 基础资料" @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="PLC ID:" required>
            <el-text type="primary">【{{formData.plcId}}】</el-text>
          </el-form-item>
          <el-form-item label="PLC 名称:" required>
            <el-input v-model="formData.plcName" placeholder="请填写PLC 名称"></el-input>
          </el-form-item>
          <el-form-item label="PLC 型号:" required>
            <el-select v-model="formData.plcType" placeholder="请选择PLC 型号" clearable>
              <el-option :key="1" label="西门子" :value="1"/>
            </el-select>
          </el-form-item>
          <el-form-item label="PLC 版本:" required>
            <el-input v-model="formData.plcVersion" placeholder="请填写PLC版本"></el-input>
          </el-form-item>
          <el-form-item label="PLC 状态:" required>
            <el-switch v-model="formData.plcStatus" inline-prompt active-text="启用" :active-value="1" inactive-text="停用" :inactive-value="0"></el-switch>
          </el-form-item>
          <el-form-item label="IP 地址:" required>
            <el-input v-model="formData.ip" placeholder="请填写PLC 的 IP 地址"></el-input>
          </el-form-item>
          <el-form-item label="端口号:" required>
            <el-input-number v-model="formData.port" :min="1" :max="65535"/>
          </el-form-item>
          <el-form-item label="插槽:" required>
            <el-input-number v-model="formData.slot" :min="0" :max="99"/>
          </el-form-item>
          <el-form-item label="机架:" required>
            <el-input-number v-model="formData.rack" :min="0" :max="99"/>
          </el-form-item>
          <el-form-item label="数据创建时间:" required>
            <el-text type="primary">{{formData.createTime}}</el-text>
          </el-form-item>
          <el-form-item label="上次更新时间:" required>
            <el-text type="primary">{{formData.updateTime}}</el-text>
          </el-form-item>
          <el-form-item label="备注:">
            <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">保存数据</el-button>
          <el-button type="warning" @click="deleteData">删除数据</el-button>
          <el-button type="danger" @click="close">关闭窗口</el-button>
        </el-button-group>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>