<script setup lang="ts">
import {useI18n} from "vue-i18n";
import type {IAppEtagLocation} from "@/model/table/IAppEtagLocation.ts";
import {ref} from "vue";
import EtagLocationTypeFormatter from "@/plugin/formatter/EtagLocationTypeFormatter.ts";
import StringUtils from "@/utils/StringUtils.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import EtagBaseApi from "@/api/etagBase.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";
import type {AppServeResponse} from "@/interface/api/AppServeResponse.ts";
import AppPermission from "@/components/manage/AppPermission.vue";

const modelValue = defineModel('modelValue', {required: true, default: false});
const emit = defineEmits(['reLoadingTableData']);
const close = () => {
  modelValue.value = false;
};
const { t } = useI18n();
const etagLocationTypeFormatter = new EtagLocationTypeFormatter();
const formData = ref<IAppEtagLocation>({});

const add = () => {
  if(StringUtils.isNullOrEmpty(formData.value.location)) {
    MessageUtils.warningMessageBox(t('tabPage.etagLocation.location') + t('baseTip.isRequired'), t('baseTip.warning'));
    return;
  }
  if(StringUtils.isNullOrEmpty(formData.value.tagName)) {
    MessageUtils.warningMessageBox(t('tabPage.etagLocation.tagName') + t('baseTip.isRequired'), t('baseTip.warning'));
    return;
  }
  if(formData.value.locationType == undefined) {
    MessageUtils.warningMessageBox(t('tabPage.etagLocation.locationType') + t('baseTip.isRequired'), t('baseTip.warning'));
    return;
  }
  MessageUtils.confirmMessageBox("确定要添加吗？", t('baseTip.confirmTip')).then(() => {
    const loadingInstance = MessageUtils.loading();
    EtagBaseApi.addEtagLocationInfo(formData.value).then((res) => {
      const response = JSON.parse(JSON.stringify(res.data)) 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.etagLocation.addEtagLocation')" @close="close" :close-on-click-modal="false">
      <el-scrollbar style="height:30vh">
        <el-form :model="formData" label-position="right" label-width="140px" require-asterisk-position="right">
          <el-form-item :label="t('tabPage.etagLocation.location') + '：'" required>
            <el-input v-model="formData.location" placeholder=""></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagLocation.tagName') + '：'" required>
            <el-input v-model="formData.tagName" placeholder=""></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagLocation.locationType') + '：'" required>
            <el-select v-model="formData.locationType" placeholder="" style="width: 250px" clearable>
              <el-option v-for="item in etagLocationTypeFormatter.etagLocationType" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagLocation.stand') + '：'" >
            <el-input v-model="formData.stand" placeholder=""></el-input>
          </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>
          <app-permission permission="etagLocation"><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>