<script setup lang="ts">

import {ref} from "vue";
import type {IComposeTaskTableData} from "@/model/table/IComposeTaskTableData.ts";
import StockComposeTaskTypeFormatter from "@/plugin/formatter/StockComposeTaskTypeFormatter.ts";
import StringUtils from "@/utils/StringUtils.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import StockComposeTaskApi from "@/api/stockComposeTask.ts";
import type {AppServeResponse} from "@/interface/api/AppServeResponse.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";
import AppPermission from "@/components/manage/AppPermission.vue";
import {useI18n} from "vue-i18n";

const { t } = useI18n();
const stockComposeTaskTypeFormatter = new StockComposeTaskTypeFormatter();
const modelValue = defineModel('modelValue', {required: true, default: false});
const emit = defineEmits(['reLoadingTableData']);
const formData = ref<IComposeTaskTableData>({
  taskType: 0,
  priority: 5,
  vehicleSize: 0,
  weight: 0,
}); // 表格数据
const close = () => { modelValue.value = false; };

// 添加任务
const add = () => {
  if(formData.value.taskType == null || StringUtils.isNullOrEmpty(formData.value.vehicleNo)){
    MessageUtils.warningMessageBox(t('baseTip.SomeDataIsRequired'));
    return;
  }
  MessageUtils.confirmMessageBox(t('customMessage.confirmAdd'), t('baseTip.confirmTip')).then(()=>{
    const loadingInstance = MessageUtils.loading();
    StockComposeTaskApi.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(t('baseTip.success'));
        emit('reLoadingTableData');
        close();
        return;
      }
      MessageUtils.warningMessageBox(response.msg, t('baseTip.warning'));
    }).catch(() => {}).finally(() => {
      loadingInstance.close();
    });
  }).catch(()=>{});
}



</script>

<template>
  <div>
    <el-dialog :model-value="modelValue" :title="t('tabPage.stockComposeTask.addTitle')" @close="close" :close-on-click-modal="false">
      <el-scrollbar style="height:40vh">
        <el-form :model="formData" label-position="right" label-width="120px" require-asterisk-position="right">
          <el-form-item :label="t('tabPage.stockComposeTask.taskType')" required>
            <el-select v-model="formData.taskType" :placeholder="t('baseTip.pleaseSelect', {msg: t('tabPage.stockComposeTask.taskType')})" clearable>
              <el-option v-for="item in stockComposeTaskTypeFormatter.taskTypeTagStyle" :key="item.value" :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item :label="t('tabPage.stockComposeTask.vehicleNo')" required>
            <el-input v-model="formData.vehicleNo" :placeholder="t('baseTip.pleaseInput', {msg: t('tabPage.stockComposeTask.vehicleNo')})"></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.stockComposeTask.origin')" required>
            <el-input v-model="formData.origin" :placeholder="t('baseTip.pleaseInput', {msg: t('tabPage.stockComposeTask.origin')})"></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.stockComposeTask.destination')" required>
            <el-input v-model="formData.destination" :placeholder="t('baseTip.pleaseInput', {msg: t('tabPage.stockComposeTask.destination')})"></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.stockComposeTask.priority')" required>
            <el-input-number :max="9" :min="0" v-model="formData.priority"></el-input-number>
          </el-form-item>
          <el-form-item :label="t('tabPage.stockComposeTask.vehicleSize')" required>
            <el-input-number :max="9" :min="0" v-model="formData.vehicleSize"></el-input-number>
          </el-form-item>
          <el-form-item :label="t('tabPage.stockComposeTask.weight')" 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>
          <app-permission permission="stockComposeTask:add"><el-button type="primary" @click="add">{{t('baseButton.add')}}</el-button></app-permission>
          <el-button type="danger" @click="close">{{t('baseButton.close')}}</el-button>
        </el-button-group>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>