<script setup lang="ts">

import {computed} from "vue";
import StockSingleTaskExecuteMachineFormatter from "@/plugin/formatter/StockSingleTaskExecuteMachineFormatter.ts";
import StockSingleTaskStatusFormatter from "@/plugin/formatter/StockSingleTaskStatusFormatter.ts";
import StockSingleTaskTypeFormatter from "@/plugin/formatter/StockSingleTaskTypeFormatter.ts";
import TrueFalseTagStyleFormatter from "@/plugin/formatter/TrueFalseTagStyleFormatter.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import StockSingleTaskApi from "@/api/stockSingleTask.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 formData = computed(() => props.formData);
const stockSingleTaskExecuteMachineFormatter = new StockSingleTaskExecuteMachineFormatter();
const stockSingleTaskStatusFormatter = new StockSingleTaskStatusFormatter();
const stockSingleTaskTypeFormatter = new StockSingleTaskTypeFormatter();
const trueOrFalseFormatter = new TrueFalseTagStyleFormatter();
// 完成任务
const completeTask = () => {
  MessageUtils.confirmMessageBox('确定完成任务？', '风险提示').then(() => {
    const loadingInstance = MessageUtils.loading();
    StockSingleTaskApi.completeTask(formData.value.taskId).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 cancelTask = () => {
  MessageUtils.confirmMessageBox('确定取消任务？', '风险提示').then(() => {
    const loadingInstance = MessageUtils.loading();
    StockSingleTaskApi.cancelTask(formData.value.taskId).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 changeNewPlcTaskId = () => {
  MessageUtils.confirmMessageBox('确定更换任务号？若设备正在执行该任务更换任务号后将无法定位执行的任务', '风险提示').then(() => {
    const loadingInstance = MessageUtils.loading();
    StockSingleTaskApi.newPlcTaskId(formData.value.taskId).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 updateTaskStatus = () => {
  MessageUtils.confirmMessageBox('确定修改任务状态？', '风险提示').then(() => {
    const loadingInstance = MessageUtils.loading();
    StockSingleTaskApi.updateTaskStatus(formData.value.taskId, formData.value.taskStatus).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(() => {});
}


</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="120px" require-asterisk-position="right" style="width: 100%">
          <el-form-item label="任务ID:" required>
            <el-input v-model="formData.taskId" disabled></el-input>
          </el-form-item>
          <el-form-item label="任务组:" required>
            <el-input v-model="formData.taskGroup" disabled></el-input>
          </el-form-item>
          <el-form-item label="plc任务号:" required>
            <el-row>
              <el-col :span="20">
                <el-input v-model="formData.plcTaskId" disabled></el-input>
              </el-col>
              <el-col :span="4">
                <el-button type="success" @click="changeNewPlcTaskId">换一个</el-button>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item label="上游任务号:" required>
            <el-input v-model="formData.upperTaskId" disabled></el-input>
          </el-form-item>
          <el-form-item label="执行设备:" required>
            <el-select v-model="formData.executeMachine" disabled>
              <el-option v-for="item in stockSingleTaskExecuteMachineFormatter.stockSingleTaskExecuteMachineTagStyle" :key="item.value" :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="任务类型:" required>
            <el-select v-model="formData.taskType" disabled>
              <el-option v-for="item in stockSingleTaskTypeFormatter.taskTypeTagStyle" :key="item.value" :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="载具编号:" required>
            <el-input v-model="formData.vehicleNo" disabled></el-input>
          </el-form-item>
          <el-form-item label="任务状态:" required>
            <el-row>
              <el-col :span="20">
                <el-select v-model="formData.taskStatus">
                  <el-option v-for="item in stockSingleTaskStatusFormatter.taskStatusTagStyle" :key="item.value" :label="item.label" :value="item.value"/>
                </el-select>
              </el-col>
              <el-col :span="4">
                <el-button type="success" @click="updateTaskStatus">修改状态</el-button>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item label="任务起点:" required>
            <el-input v-model="formData.origin" disabled></el-input>
          </el-form-item>
          <el-form-item label="任务目的地:" required>
            <el-input v-model="formData.destination" disabled></el-input>
          </el-form-item>
          <el-form-item label="最终目的地:" required>
            <el-input v-model="formData.composeDestination" disabled></el-input>
          </el-form-item>
          <el-form-item label="优先级:" required>
            <el-input-number :max="9" :min="0" v-model="formData.priority" disabled></el-input-number>
          </el-form-item>
          <el-form-item label="载具尺寸:" required>
            <el-input-number :max="9" :min="0" v-model="formData.vehicleSize" disabled></el-input-number>
          </el-form-item>
          <el-form-item label="载具重量:" required>
            <el-input-number :max="9" :min="0" v-model="formData.weight" disabled></el-input-number>
          </el-form-item>
          <el-form-item label="是否可取消:" required>
            <el-select v-model="formData.canCancel" disabled>
              <el-option v-for="item in trueOrFalseFormatter.trueFalseTagStyle" :key="item.value" :label="item.label" :value="item.value"/>
            </el-select>
          </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="任务开始时间:" required>
            <el-text type="primary">{{formData.startTime}}</el-text>
          </el-form-item>
          <el-form-item label="任务完成时间:" required>
            <el-text type="primary">{{formData.completeTime}}</el-text>
          </el-form-item>
          <el-form-item label="任务结束时间:" required>
            <el-text type="primary">{{formData.endTime}}</el-text>
          </el-form-item>
          <el-form-item label="任务来源:" required>
            <el-text type="primary">{{formData.taskSource}}</el-text>
          </el-form-item>
          <el-form-item label="创建人:" required>
            <el-text type="primary">{{formData.createPerson}}</el-text>
          </el-form-item>
          <el-form-item label="任务信息:" required>
            <el-text type="primary">{{formData.taskMsg}}</el-text>
          </el-form-item>
        </el-form>
      </el-scrollbar>
      <template #footer>
        <el-button-group>
          <el-button type="success" @click="completeTask">完成任务</el-button>
          <el-button type="warning" @click="cancelTask">取消任务</el-button>
          <el-button type="danger" @click="close">关闭窗口</el-button>
        </el-button-group>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>