<script setup lang="ts">
import TrueFalseTagStyleFormatter from "@/plugin/formatter/TrueFalseTagStyleFormatter.ts";
import TrayConveyLocationTypeFormatter from "@/plugin/formatter/TrayConveyLocationTypeFormatter.ts";
import {ref} from "vue";
import type {ITrayConveyTableData} from "@/model/table/ITrayConveyTableData.ts";
import {FormatterUtils} from "@/utils/FormatterUtils.ts";
import type {IConveyPlcStatus} from "@/interface/page/trayConvey/IConveyPlcStatus.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import TrayConveyLocationApi from "@/api/trayConveyLocation.ts";
import type {AppServeResponse} from "@/interface/api/AppServeResponse.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";
import type {AppServeDataResponse} from "@/interface/api/AppServeDataResponse.ts";

const modelValue = defineModel('modelValue', {required: true, default: false});
const formData = defineModel<ITrayConveyTableData>('rowData', {required: true, default: false});
const emit = defineEmits(['reLoadingTableData']);
const trueFalseTagStyleFormatter = new TrueFalseTagStyleFormatter();
const trayConveyLocationTypeFormatter = new TrayConveyLocationTypeFormatter();
const trayConveyPlcStatus = ref<IConveyPlcStatus>({}); // 输送机当前数据，从PLC读取的 数据


const close = () => {
  modelValue.value = false;
};
// 保存信息
const save = () => {
  MessageUtils.confirmMessageBox('确定要保存吗？', '请确认').then(() => {
    const loadingInstance = MessageUtils.loading('正在保存中...');
    TrayConveyLocationApi.update(formData.value).then((res) => {
      const responseString = JSON.stringify(res.data);
      const response = JSON.parse(responseString) as AppServeResponse;
      if (response && response.code == AppServeResponseCodeEnum.SUCCESS) {
        MessageUtils.successMessage('保存成功');
        emit('reLoadingTableData');
        close();
        return;
      }
      MessageUtils.errMessageBox('保存失败：' + response.msg, '错误');
    }).catch(() => {}).finally(() => {
      loadingInstance.close();
    });
  }).catch(() => {})

};
// 获取输送机当前数据
const getConveyPlcStatus = () => {
  const loadingInstance = MessageUtils.loading('正在获取输送机数据...');
  TrayConveyLocationApi.queryTrayConveyStatus(formData.value.locationId).then((res) => {
    const responseString = JSON.stringify(res.data);
    const response = JSON.parse(responseString) as AppServeDataResponse<IConveyPlcStatus>;
    if (response && response.code == AppServeResponseCodeEnum.SUCCESS) {
      trayConveyPlcStatus.value = response.data ?? {};
      return;
    }
    MessageUtils.errMessageBox('请求PLC数据失败：' + response.msg, '错误');
  }).catch(() => {}).finally(() => {
    loadingInstance.close();
  });
}
</script>

<template>
  <el-dialog v-model="modelValue" title="托盘输送点位详细信息" @close="close" :close-on-click-modal="false">
    <el-scrollbar style="height:60vh;">
      <el-row style="width: calc(100% - 10px)">
        <el-col :span="11">
          <el-form :model="formData" label-position="right" label-width="120px" require-asterisk-position="right" style="width: calc(100% - 10px)">
            <el-form-item label="记录号:" required>
              <el-text type="primary">{{formData.id}}</el-text>
            </el-form-item>
            <el-form-item label="点位号:" required>
              <el-input v-model="formData.locationId" placeholder="请填写点位号"></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 trayConveyLocationTypeFormatter.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="点位坐标:">
              <el-input v-model="formData.locationXyz" placeholder="请填写点位坐标"></el-input>
            </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="记录创建时间:" required>
              <el-text type="primary">{{FormatterUtils.formatTime(formData.createTime?.toString() ?? '')}}</el-text>
            </el-form-item>
            <el-form-item label="上次更新时间:" required>
              <el-text type="primary">{{FormatterUtils.formatTime(formData.updateTime?.toString() ?? '')}}</el-text>
            </el-form-item>
            <el-form-item label="备注:">
              <el-input v-model="formData.remark" placeholder="请填写备注"></el-input>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="12" :offset="1">
          <el-row>
            <el-card shadow="never" style="width: 100%">
              <template #header>
                <span style="font-weight: bold">{{formData.locationName}}当前状态</span>
              </template>
              <el-form :model="trayConveyPlcStatus" label-position="right" label-width="120px" require-asterisk-position="right" style="width: calc(100% - 10px)">
                <el-form-item label="设备号:" required>
                  <el-input v-model="trayConveyPlcStatus.conveyNo" disabled/>
                </el-form-item>
                <el-form-item label="PLC任务号:" required>
                  <el-input  v-model="trayConveyPlcStatus.plcTaskId" disabled/>
                </el-form-item>
                <el-form-item label="任务允许:" required>
                  <el-input  v-model="trayConveyPlcStatus.taskAllow" disabled/>
                </el-form-item>
                <el-form-item label="控制方式:" required>
                  <el-input  v-model="trayConveyPlcStatus.controllerModel" disabled/>
                </el-form-item>
                <el-form-item label="设备状态:" required>
                  <el-input  v-model="trayConveyPlcStatus.conveyStatus" disabled/>
                </el-form-item>
                <el-form-item label="是否有AGV:" required>
                  <el-input  v-model="trayConveyPlcStatus.haveAgv" disabled/>
                </el-form-item>
                <el-form-item label="载具尺寸:" required>
                  <el-input  v-model="trayConveyPlcStatus.vehicleSize" disabled/>
                </el-form-item>
                <el-form-item label="载具重量:" required>
                  <el-input  v-model="trayConveyPlcStatus.vehicleWeight" disabled/>
                </el-form-item>
                <el-form-item label="错误码:" required>
                  <el-input  v-model="trayConveyPlcStatus.errorCode" disabled/>
                </el-form-item>
                <el-form-item label="条码:" required>
                  <el-input  v-model="trayConveyPlcStatus.vehicleCode" disabled/>
                </el-form-item>
              </el-form>
              <template #footer>
                <el-button type="primary" @click="getConveyPlcStatus">查询/刷新</el-button>
              </template>
            </el-card>
          </el-row>

        </el-col>
      </el-row>

    </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>
</template>

<style scoped>

</style>