<script setup lang="ts">
import {useI18n} from "vue-i18n";
import EtagLocationTypeFormatter from "@/plugin/formatter/EtagLocationTypeFormatter.ts";
import {computed, ref} from "vue";
import type {IAppEtagLocation} from "@/model/table/IAppEtagLocation.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";

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

// 保存新数据
const save = () => {
  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.updateEtagLocationInfo(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(() => {})
};
// 删除数据
const deleteData = () => {
  MessageUtils.confirmMessageBox('确定删除该数据？', '删除确认').then(() => {
    const loading = MessageUtils.loading("正在删除");
    EtagBaseApi.deleteEtagLocationInfo(formData.value.recordId).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(() => {
      loading.close();
    })
  }).catch(() => {});
}
</script>

<template>
  <div>
    <el-dialog :model-value="modelValue" :title="t('tabPage.etagLocation.editEtagLocation')" @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.recordId') + '：'" required>
            <el-text type="primary">{{formData.recordId}}</el-text>
          </el-form-item>
          <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>
          <el-button type="primary" @click="save">{{t('baseButton.save')}}</el-button>
          <el-button type="warning" @click="deleteData">{{t('baseButton.delete')}}</el-button>
          <el-button type="danger" @click="close">{{t('baseButton.close')}}</el-button>
        </el-button-group>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>