<script setup lang="ts">
import {useI18n} from "vue-i18n";
import {ref} from "vue";
import type {IAppEtagTask} from "@/model/table/IAppEtagTask.ts";
import {EtagTaskTypeFormatter} from "@/plugin/formatter/EtagTaskTypeFormatter.ts";
import EtagTaskLightModelFormatter from "@/plugin/formatter/EtagTaskLightModelFormatter.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import EtagTaskApi from "@/api/etagTask.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 close = () => {
  modelValue.value = false;
};
const { t } = useI18n();
const etagTaskTypeFormatter = new EtagTaskTypeFormatter();
const etagTagTaskLightModelFormatter = new EtagTaskLightModelFormatter();
const formData = ref<IAppEtagTask>({
  taskType: 1,
  lightModel: 1,
  lightNum: 1,
}); // 表单数据

// 添加任务
const add = () => {
  if(!formData.value.vehicleNo || formData.value.vehicleNo.trim().length == 0) {
    MessageUtils.warningMessage('请填写载具号');
    return;
  }
  if(!formData.value.location || formData.value.location.trim().length == 0) {
    MessageUtils.warningMessage('请填写位置');
    return;
  }
  MessageUtils.confirmMessageBox('确定添加上述数据？', '添加确认').then(() => {
    const loadingInstance = MessageUtils.loading();
    EtagTaskApi.addEtagTask(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.warningMessageBox(response.msg, '警告');
    }).catch(() => {}).finally(() => {
      loadingInstance.close();
    });
  }).catch(() => {});
};


</script>

<template>
  <div>
    <el-dialog :model-value="modelValue" :title="t('tabPage.etagTask.addEtagTask')" @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">
          <el-form-item :label="t('tabPage.etagTask.taskType') + '：'" required>
            <el-select v-model="formData.taskType" placeholder="" style="width: 250px" clearable>
              <el-option v-for="item in etagTaskTypeFormatter.etagTaskType" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagTask.vehicleNo') + '：'" required>
            <el-input v-model="formData.vehicleNo" placeholder=""></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagTask.orderId') + '：'">
            <el-input v-model="formData.orderId" placeholder=""></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagTask.location') + '：'" required>
            <el-input v-model="formData.location" placeholder=""></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagTask.goodsId') + '：'">
            <el-input v-model="formData.goodsId" placeholder=""></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagTask.goodsName') + '：'">
            <el-input v-model="formData.goodsName" placeholder=""></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagTask.lightNum') + '：'" required>
            <el-input-number :min="1" :max="32000" v-model="formData.lightNum" placeholder=""></el-input-number>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagTask.lightModel') + '：'" required>
            <el-select v-model="formData.lightModel" placeholder="" style="width: 250px" clearable>
              <el-option v-for="item in etagTagTaskLightModelFormatter.etagTaskLightModel" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="t('baseColName.remark') + '：'">
            <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">{{t('baseButton.add')}}</el-button>
          <el-button type="danger" @click="close">{{t('baseButton.close')}}</el-button>
        </el-button-group>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>