<script setup lang="ts">
import {computed} from "vue";
import type {IAppConveyTask} from "@/model/table/IAppConveyTask.ts";
import ConveyTaskTypeFormatter from "@/plugin/formatter/ConveyTaskTypeFormatter.ts";
import ConveyTaskStatusFormatter from "@/plugin/formatter/ConveyTaskStatusFormatter.ts";
import {FormatterUtils} from "@/utils/FormatterUtils.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import ConveyTaskApi from "@/api/conveyTask.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 emit = defineEmits(['reLoadingTableData']);
const props = defineProps<{
  formData: IAppConveyTask
}>();
const formData = computed(() => props.formData)
const conveyTaskTypeFormatter = new ConveyTaskTypeFormatter();
const conveyTaskStatusFormatter = new ConveyTaskStatusFormatter();
const close = () => {
  modelValue.value = false;
};
// 上报任务完成
const reportComplete = () => {
  if(!formData.value.arriveLocation ||  formData.value.arriveLocation.trim() == '') {
    MessageUtils.errMessage('请填写目标位置');
    return;
  }
  if(!formData.value.taskId ||  formData.value.taskId.trim() == '') {
    MessageUtils.errMessage('请填写任务ID');
    return;
  }
  MessageUtils.confirmMessageBox('确定上报任务完成？', '提示').then(() => {
    const loadingInstance = MessageUtils.loading();
    ConveyTaskApi.reportBakComplete({
      taskId: formData.value.taskId,
      arriveLocation: formData.value.arriveLocation
    }).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 reportTaskCancel = () => {
  MessageUtils.confirmMessageBox('确定上报任务取消？', '提示').then(() => {
    const loadingInstance = MessageUtils.loading();
    ConveyTaskApi.reportBakCancel({
      taskId: formData.value.taskId,
      arriveLocation: ""
    }).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="100px" require-asterisk-position="right">
          <el-form-item label="任务ID:" required>
            <el-text type="primary">{{formData.taskId}}</el-text>
          </el-form-item>
          <el-form-item label="任务组:" required>
            <el-text type="primary">{{formData.taskGroup}}</el-text>
          </el-form-item>
          <el-form-item label="任务类型:" required>
            <el-select v-model="formData.taskType" disabled>
              <el-option v-for="item in conveyTaskTypeFormatter.taskType" :label="item.label" :key="item.value" :value="item.value"></el-option>
            </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="订单号:">
            <el-input v-model="formData.orderId" disabled></el-input>
          </el-form-item>
          <el-form-item label="目的位置:" required>
            <el-input v-model="formData.location" disabled></el-input>
          </el-form-item>
          <el-form-item label="到达位置:" required>
            <el-input v-model="formData.arriveLocation" placeholder="请填写目标位置"></el-input>
          </el-form-item>
          <el-form-item label="任务状态:" required>
            <el-select v-model="formData.taskStatus" placeholder="请选择任务类型" disabled>
              <el-option v-for="item in conveyTaskStatusFormatter.taskStatus" :label="item.label" :key="item.value" :value="item.value"></el-option>
            </el-select>
          </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="PLC任务号:" required>
            <el-text type="primary">{{formData.plcId}}</el-text>
          </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="到达时间:" required>
            <el-text type="primary">{{ FormatterUtils.formatTime(formData.arriveTime?.toString() ?? '') }}</el-text>
          </el-form-item>
          <el-form-item label="完成时间:" required>
            <el-text type="primary">{{FormatterUtils.formatTime(formData.completeTime?.toString() ?? '')}}</el-text>
          </el-form-item>
          <el-form-item label="尺寸:" required>
            <el-input v-model="formData.size" disabled></el-input>
          </el-form-item>
          <el-form-item label="重量:" required>
            <el-input-number v-model="formData.weight" :min="0" :max="99999" disabled/>
          </el-form-item>
          <el-form-item label="长度:" required>
            <el-input-number v-model="formData.length" :min="0" :max="99999" disabled/>
          </el-form-item>
          <el-form-item label="宽度:" required>
            <el-input-number v-model="formData.width" :min="0" :max="999999" disabled/>
          </el-form-item>
          <el-form-item label="高度:" required>
            <el-input-number v-model="formData.height" :min="0" :max="99999" disabled/>
          </el-form-item>
          <el-form-item label="备注:">
            <el-input v-model="formData.remark" disabled></el-input>
          </el-form-item>
        </el-form>
      </el-scrollbar>
      <template #footer>
        <el-button-group style="margin-right: 20px">
          <el-button type="success" @click="reportComplete">上报任务完成</el-button>
          <el-button type="warning" @click="reportTaskCancel">上报任务取消</el-button>
        </el-button-group>
        <el-button-group>
          <el-button type="danger" @click="close">关闭窗口</el-button>
        </el-button-group>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>