<script setup lang="ts">

import {ref} from "vue";
import type {IPlcTableVo} from "@/model/table/IPlcTableVo.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import StringUtils from "@/utils/StringUtils.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 formData = ref<IPlcTableVo>({plcId: 1, plcType: 1, plcStatus: 1, port: 102, slot: 1, rack: 0, plcVersion: ''});
const close = () => {
  modelValue.value = false;
};
// 添加
const add = () => {
  if(StringUtils.isNullOrEmpty(formData.value.plcName)) {
    MessageUtils.warningMessageBox('存在必填的数据未填');
    return;
  }
  MessageUtils.confirmMessageBox('确定添加以上数据？', '添加确认').then(() => {
    const loadingInstance = MessageUtils.loading();
    PlcApi.addPlc(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="添加 PLC 基础资料" @close="close" :close-on-click-modal="false">
      <el-text type="primary" style="margin-bottom: 10px">注意：可能存在代理</el-text>
      <el-scrollbar style="height:50vh">
        <el-form :model="formData" label-position="right" label-width="100px" require-asterisk-position="right">
          <el-form-item label="PLC ID:" required>
            <el-input-number v-model="formData.plcId" :min="1" :max="99"/>
            <el-text type="warning" style="font-size: 12px">该 PLC ID 是唯一的，请勿填写重复的 ID</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-text type="warning" style="font-size: 12px">必须是正确的 IP 格式</el-text>
          </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="备注:">
            <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">确定添加</el-button>
          <el-button type="danger" @click="close">关闭窗口</el-button>
        </el-button-group>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>