<script setup lang="ts">

import StringUtils from "@/utils/StringUtils.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import StackerApi from "@/api/stacker.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 props = defineProps(['formData']);
const emit = defineEmits(['reLoadingTableData']);
const close = () => { modelValue.value = false; };
// 保存更改
const save = () => {
  if(StringUtils.isNullOrEmpty(props.formData.stackerName) || StringUtils.isNullOrEmpty(props.formData.readStatusAddress)) {
    MessageUtils.warningMessageBox('存在必填的项未填');
    return;
  }
  MessageUtils.confirmMessageBox('确定变更上述数据？', '二次确认').then(() => {
    const loadingInstance = MessageUtils.loading();
    StackerApi.updateStackerInfo(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(()=>{})
}



</script>

<template>
  <div>
    <el-dialog :model-value="modelValue" title="查看/编辑 堆垛机基础资料" @close="close" :close-on-click-modal="false">
      <el-scrollbar style="height:50vh">
        <el-form :model="formData" label-position="right" label-width="180px" require-asterisk-position="right">
          <el-form-item label="堆垛机ID:" required>
            <el-text type="primary">【{{formData.stackerId}}】</el-text>
          </el-form-item>
          <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">该PLCID必须在PLC配置内，表示该堆垛机归属哪个PLC控制</el-text>
          </el-form-item>
          <el-form-item label="堆垛机名称:" required>
            <el-input v-model="formData.stackerName" placeholder="请填写堆垛机名称"></el-input>
          </el-form-item>
          <el-form-item label="堆垛机状态:" required>
            <el-switch v-model="formData.stackerStatus" inline-prompt active-text="启用" :active-value="1" inactive-text="停用" :inactive-value="0"></el-switch>
          </el-form-item>
          <el-form-item label="货叉状态:" required>
            <el-input v-model="formData.forkStatus" placeholder="请填写货叉状态"></el-input>
            <el-text type="warning" style="font-size: 12px">每个字符仅能是1和0，1表示开始，0表示关闭；有多少个货叉就填多少字符，如 “10” 表示有两个货叉，1号货叉开启，2号货叉关闭</el-text>
          </el-form-item>
          <el-form-item label="是否允许入库:" required>
            <el-switch v-model="formData.allowIn" inline-prompt active-text="是" :active-value="1" inactive-text="否" :inactive-value="0"></el-switch>
          </el-form-item>
          <el-form-item label="是否允许出库:" required>
            <el-switch v-model="formData.allowOut" inline-prompt active-text="是" :active-value="1" inactive-text="否" :inactive-value="0"></el-switch>
          </el-form-item>
          <el-form-item label="是否允许移库:" required>
            <el-switch v-model="formData.allowMove" inline-prompt active-text="是" :active-value="1" inactive-text="否" :inactive-value="0"></el-switch>
          </el-form-item>
          <el-form-item label="动作节拍:" required>
            <el-input v-model="formData.actionBeat" placeholder="请填写动作节拍"></el-input>
            <el-text type="warning" style="font-size: 12px">1表示入库，2表示出库，9表示移库；堆垛机将按照配置的节拍循环执行任务，每种任务类型可以存在多次</el-text>
          </el-form-item>
          <el-form-item label="读取堆垛机状态地址:" required>
            <el-input v-model="formData.readStatusAddress" placeholder="请填写读取堆垛机状态地址"></el-input>
          </el-form-item>
          <el-form-item label="写入堆垛机任务地址:" required>
            <el-input v-model="formData.writeTaskAddress" placeholder="请填写写入堆垛机任务地址"></el-input>
            <el-text type="warning" style="font-size: 12px">多个货叉时用英文分号（;）隔开，数量必须和配置货叉状态一致，否则系统将按照少的一个判定货叉数量</el-text>
          </el-form-item>
          <el-form-item label="写入任务确认地址:" required>
            <el-input v-model="formData.taskConfirmAddress" placeholder="请填写写入任务确认地址"></el-input>
          </el-form-item>
          <el-form-item label="过账地址:" required>
            <el-input v-model="formData.taskStatusAddress" placeholder="请填写过账地址"></el-input>
          </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="danger" @click="close">关闭窗口</el-button>
        </el-button-group>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>