<script setup lang="ts">

import type {IAppConveyTask} from "@/model/table/IAppConveyTask.ts";
import {ref} from "vue";
import ConveyTaskTypeFormatter from "@/plugin/formatter/ConveyTaskTypeFormatter.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 formData = ref<IAppConveyTask>({
  size: '0',
  taskType: 1,
  weight: 0,
  length: 0,
  width: 0,
  height: 0,
});
const conveyTaskTypeFormatter = new ConveyTaskTypeFormatter();
const close = () => {
  modelValue.value = false;
};

// 添加任务
const add = () => {
  const errText = validate();
  if(errText.length > 0) {
    MessageUtils.errMessageBox(errText, '错误');
  } else {
    MessageUtils.confirmMessageBox('确定添加任务？', '添加任务', '添加', '取消').then(() => {
      const loadingInstance = MessageUtils.loading();
      ConveyTaskApi.add(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.errMessage('添加失败:' + response.msg);
      }).catch(() => {}).finally(() => {
        loadingInstance.close();
      })
    })
  }
};

// 校验数据
const validate = (): string => {
  if(formData == null || formData.value == null) {
    return '数据不能为空';
  }
  if(formData.value.taskType == null || formData.value.taskType == 0) {
    return '请选择任务类型';
  }
  if(formData.value.vehicleNo == null || formData.value.vehicleNo.trim().length == 0) {
    return '请填写载具号';
  }
  if(formData.value.location == null || formData.value.location.trim().length == 0) {
    return '请填写目标位置';
  }
  if(formData.value.size == null || formData.value.size.trim().length == 0) {
    return '请填写尺寸';
  }
  return '';
}



</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="任务类型:" required>
            <el-select v-model="formData.taskType" placeholder="请选择任务类型" clearable>
              <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" placeholder="请填写载具号"></el-input>
          </el-form-item>
          <el-form-item label="订单号:">
            <el-input v-model="formData.orderId" placeholder="请填写订单号"></el-input>
          </el-form-item>
          <el-form-item label="目的位置:" required>
            <el-input v-model="formData.location" placeholder="请填写目标位置"></el-input>
          </el-form-item>
          <el-form-item label="尺寸:" required>
            <el-input v-model="formData.size" placeholder="请填写尺寸"></el-input>
          </el-form-item>
          <el-form-item label="重量:" required>
            <el-input-number v-model="formData.weight" :min="0" :max="99999"/>
          </el-form-item>
          <el-form-item label="长度:" required>
            <el-input-number v-model="formData.length" :min="0" :max="99999"/>
          </el-form-item>
          <el-form-item label="宽度:" required>
            <el-input-number v-model="formData.width" :min="0" :max="999999"/>
          </el-form-item>
          <el-form-item label="高度:" required>
            <el-input-number v-model="formData.height" :min="0" :max="99999"/>
          </el-form-item>
          <el-form-item label="备注:">
            <el-input v-model="formData.remark"></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>
  </div>
</template>

<style scoped>

</style>