<script setup lang="ts">
import TrueFalseTagStyleFormatter from "@/plugin/formatter/TrueFalseTagStyleFormatter.ts";
import {ConveyLocationTypeFormatter} from "@/plugin/formatter/ConveyLocationTypeFormatter.ts";
import type {IAppConveyLocation} from "@/model/table/IAppConveyLocation.ts";
import {onMounted, ref} from "vue";
import MessageUtils from "@/utils/MessageUtils.ts";
import ConveyApi from "@/api/convey.ts";
import type {AppServeResponse} from "@/interface/api/AppServeResponse.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";
import StringUtils from "@/utils/StringUtils.ts";

const modelValue = defineModel('modelValue', {required: true, default: false});
const emit = defineEmits(['reLoadingTableData']);
const trueFalseTagStyleFormatter = new TrueFalseTagStyleFormatter();
const conveyLocationTypeFormatter = new ConveyLocationTypeFormatter();
const formData = ref<IAppConveyLocation>({
  locationId: '',
  businessLocationId: '',
  locationName: '',
  locationStatus: 1,
  locationRouter: 1,
  locationType: 0,
  plcId: 1,
  areaId: '',
});

const close = () => { modelValue.value = false; };
const autoFill = (value: string | number) => {
  formData.value.businessLocationId = value.toString();
  formData.value.locationName = value.toString() + '号站台';
};

// 添加数据
const add = () => {
  if(!formData.value.locationId || StringUtils.isNullOrEmpty(formData.value.locationId)) {
    MessageUtils.warningMessage('请填写位置编号');
    return;
  }
  if(!formData.value.businessLocationId || StringUtils.isNullOrEmpty(formData.value.businessLocationId)) {
    MessageUtils.warningMessage('请填写业务位置编号');
    return;
  }
  if(!formData.value.locationName || StringUtils.isNullOrEmpty(formData.value.locationName))  {
    MessageUtils.warningMessage('请填写位置名称');
    return;
  }
  MessageUtils.confirmMessageBox('确定添加上述数据？','添加确认').then(()=>{
    const loadingInstance = MessageUtils.loading('添加中');
    ConveyApi.addConveyLocation(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.errMessage(response.msg);
    }).catch(() => {}).finally(() => {
      loadingInstance.close();
      close();
    })
  })
};


</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.locationId" placeholder="请填写点位号" @input="autoFill"></el-input>
        </el-form-item>
        <el-form-item label="业务点位号:" required>
          <el-input v-model="formData.businessLocationId" placeholder="请填写业务点位号"></el-input>
        </el-form-item>
        <el-form-item label="点位名称:" required>
          <el-input v-model="formData.locationName" placeholder="请填写点位名称"></el-input>
        </el-form-item>
        <el-form-item label="点位状态:" required>
          <el-select v-model="formData.locationStatus" 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.locationType" placeholder="请选择点位类型" clearable>
            <el-option v-for="item in conveyLocationTypeFormatter.locationType" :key="item.value" :label="item.label" :value="item.value"/>
          </el-select>
        </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-number v-model="formData.locationRouter" :min="1" :max="30000"/>
        </el-form-item>
        <el-form-item label="所属区域:" required>
          <el-input v-model="formData.areaId" placeholder="请填写所属区域"></el-input>
        </el-form-item>
        <el-form-item label="读取状态地址:">
          <el-input v-model="formData.readStatusAddress" 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>