<script setup lang="ts">

import {ref} from "vue";
import type ISingleTaskTableData from "@/model/table/ISingleTaskTableData.ts";
import StockSingleTaskExecuteMachineFormatter from "@/plugin/formatter/StockSingleTaskExecuteMachineFormatter.ts";
import StockSingleTaskTypeFormatter from "@/plugin/formatter/StockSingleTaskTypeFormatter.ts";
import StringUtils from "@/utils/StringUtils.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 stockSingleTaskExecuteMachineFormatter = new StockSingleTaskExecuteMachineFormatter();
const stockSingleTaskTypeFormatter = new StockSingleTaskTypeFormatter();
const modelValue = defineModel('modelValue', {required: true, default: false});
const emit = defineEmits(['reLoadingTableData']);
const close = () => { modelValue.value = false; };
const formData = ref<ISingleTaskTableData>({
  executeMachine: 1,
  taskType: 1,
  vehicleNo: '',
  origin: '',
  destination: '',
  priority: 2,
  vehicleSize: 0,
  weight: 0,
});
// 添加
const add = () => {
  if(StringUtils.isNullOrEmpty(formData.value.vehicleNo) || StringUtils.isNullOrEmpty(formData.value.origin) || StringUtils.isNullOrEmpty(formData.value.destination)) {
    MessageUtils.warningMessageBox('请填写完整信息');
    return;
  }
  MessageUtils.confirmMessageBox('确定添加任务？', '添加任务', '添加', '取消').then(() => {
    const loadingInstance = MessageUtils.loading();
    StockSingleTaskApi.addTask(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;
      }
    }).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:40vh">
        <el-form :model="formData" label-position="right" label-width="100px" require-asterisk-position="right">
          <el-form-item label="执行设备:" required>
            <el-select v-model="formData.executeMachine" placeholder="请选择执行设备" clearable>
              <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" placeholder="请选择任务类型" clearable>
              <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" placeholder="请填写载具编号"></el-input>
          </el-form-item>
          <el-form-item label="任务起点:" required>
            <el-input v-model="formData.origin" placeholder="请填写任务起点"></el-input>
          </el-form-item>
          <el-form-item label="任务目的地:" required>
            <el-input v-model="formData.destination" placeholder="请填写任务目的地"></el-input>
          </el-form-item>
          <el-form-item label="优先级:" required>
            <el-input-number :max="9" :min="0" v-model="formData.priority"></el-input-number>
          </el-form-item>
          <el-form-item label="载具尺寸:" required>
            <el-input-number :max="9" :min="0" v-model="formData.vehicleSize"></el-input-number>
          </el-form-item>
          <el-form-item label="载具重量:" required>
            <el-input-number :max="9" :min="0" v-model="formData.weight"></el-input-number>
          </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>