<script setup lang="ts">
import TrueFalseTagStyleFormatter from "@/plugin/formatter/TrueFalseTagStyleFormatter.ts";
import {ConveyPickStandTypeFormatter} from "@/plugin/formatter/ConveyPickStandTypeFormatter.ts";
import {ref} from "vue";
import type {IAppConveyPickStand} from "@/model/table/IAppConveyPickStand.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import ConveyStandApi from "@/api/conveyStand.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 trueFalseTagStyleFormatter = new TrueFalseTagStyleFormatter();
const conveyPickStandTypeFormatter = new ConveyPickStandTypeFormatter();
const close = () => { modelValue.value = false; };
const formData = ref<IAppConveyPickStand>({
  standId: '',
  standName: '',
  plcId: 1,
  areaId: '',
  standStatus: 1,
  standType: 0,
  router: 1
});
// 添加站台
const add = () => {
  if(!formData.value.standId || !formData.value.standName || formData.value.standId == '' || formData.value.standName == '') {
    MessageUtils.warningMessageBox('站台号和站台名称不能为空', '警告');
    return;
  }
  if(!formData.value.areaId || formData.value.areaId == '') {
    MessageUtils.warningMessageBox('请填写区域号', '警告');
    return;
  }
  MessageUtils.confirmMessageBox('确定要保存吗？', '请确认').then(() => {
    const loadingInstance = MessageUtils.loading('正在添加站台信息');
    ConveyStandApi.addPickStand(formData.value).then((res) => {
      const response = JSON.parse(JSON.stringify(res.data)) as AppServeResponse;
      if(response.code == AppServeResponseCodeEnum.SUCCESS) {
        MessageUtils.successMessage('添加成功');
        emit('reLoadingTableData');
        close();
      } else {
        MessageUtils.warningMessageBox(response.msg);
      }
    }).catch(() => {}).finally(() => {
      loadingInstance.close();
    });
  }).catch(() => {});
}

</script>

<template>
  <el-dialog v-model="modelValue" title="新增捡选站台信息" @close="close" :close-on-click-modal="false">
    <el-scrollbar style="height:50vh;">
      <el-form :model="formData" label-position="right" label-width="100px" require-asterisk-position="right" style="width: calc(100% - 10px)">
        <el-form-item label="站台号:" required>
          <el-input v-model="formData.standId" placeholder="请填写站台号" ></el-input>
        </el-form-item>
        <el-form-item label="站台名称:" required>
          <el-input v-model="formData.standName" placeholder="请填写站台名称"></el-input>
        </el-form-item>
        <el-form-item label="管辖的PLC:" 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.areaId" placeholder="请填写区域号"></el-input>
        </el-form-item>
        <el-form-item label="站台状态:" required>
          <el-select v-model="formData.standStatus" placeholder="请选择站台状态" clearable>
            <el-option v-for="item in trueFalseTagStyleFormatter.canUseTagStyle" :key="item.value" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="站台类型:" required>
          <el-select v-model="formData.standType" placeholder="请选择站台类型" clearable>
            <el-option v-for="item in conveyPickStandTypeFormatter.pickStandType" :key="item.value" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="路向:" required>
          <el-input-number v-model="formData.router" :min="1" :max="30000"/>
        </el-form-item>
        <el-form-item label="读取信息地址:">
          <el-input v-model="formData.readArriveAddress" placeholder="请填写读取信息地址"></el-input>
        </el-form-item>
        <el-form-item label="写入任务地址:">
          <el-input v-model="formData.writeTaskAddress" placeholder="请填写写入任务地址"></el-input>
        </el-form-item>
        <el-form-item label="备注:">
          <el-input v-model="formData.remark" placeholder="请填写备注"></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>
</template>

<style scoped>

</style>