<script setup lang="ts">
import {useI18n} from "vue-i18n";
import type {IAppEtagTag} from "@/model/table/IAppEtagTag.ts";
import {computed, ref} from "vue";
import EtagTagTypeFormatter from "@/plugin/formatter/EtagTagTypeFormatter.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import EtagBaseApi from "@/api/etagBase.ts";
import type {AppServeResponse} from "@/interface/api/AppServeResponse.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";
import EtagTagBusinessTypeFormatter from "@/plugin/formatter/EtagTagBusinessTypeFormatter.ts";

const modelValue = defineModel('modelValue', {required: true, default: false});
const emit = defineEmits(['reLoadingTableData']);
const close = () => {
  modelValue.value = false;
};
const props = defineProps<{
  formData: IAppEtagTag;
}>()
const { t } = useI18n();
const etagTagTypeFormatter = new EtagTagTypeFormatter();
const tagBusinessTypeFormatter = new EtagTagBusinessTypeFormatter(); // 标签业务类型格式化
const formData = computed(() => props.formData); // 要编辑的标签

// 保存
const save = () => {
  if(!formData.value.tagName || !formData.value.tagId || !formData.value.tagType
      || !formData.value.controllerId || !formData.value.tunnelNo || !formData.value.areaId) {
    MessageUtils.warningMessageBox('请填写完整信息', '警告');
    return;
  }
  MessageUtils.confirmMessageBox('确定保存上述信息吗？', '操作确认').then(() => {
    const loadingInstance = MessageUtils.loading();
    EtagBaseApi.updateEtagTagInfo(formData.value).then((res) => {
      const response = JSON.parse(JSON.stringify(res.data)) as AppServeResponse;
      if (response && response.code == AppServeResponseCodeEnum.SUCCESS) {
        MessageUtils.successMessage('操作成功');
        emit('reLoadingTableData');
        close();
        return
      }
      MessageUtils.warningMessageBox(response.msg, '警告');
    }).catch(() => {}).finally(() => {
      loadingInstance.close();
    });
  }).catch(() => {});
};

// 删除
const deleteTag = () => {
  MessageUtils.confirmMessageBox('确定要删除吗？', '删除确认').then(() => {
    const loadingInstance = MessageUtils.loading();
    EtagBaseApi.deleteEtagTagInfo(formData.value.tagName).then((res) => {
      const response = JSON.parse(JSON.stringify(res.data)) 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.etagTag.editEtagTag')" @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.etagTag.controllerId') + '：'" required>
            <el-input-number v-model="formData.controllerId" :min="1" :max="3000"/>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagTag.tagName') + '：'" required>
            <el-input v-model="formData.tagName" placeholder="" disabled></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagTag.tagId') + '：'" required>
            <el-input v-model="formData.tagId" placeholder=""></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagTag.tagType') + '：'" required>
            <el-select v-model="formData.tagType" placeholder="" style="width: 250px" clearable>
              <el-option v-for="item in etagTagTypeFormatter.etagTagType" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagTag.businessType') + '：'" required>
            <el-select v-model="formData.businessType" placeholder="" style="width: 250px" clearable>
              <el-option v-for="item in tagBusinessTypeFormatter.tagBusinessType" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagTag.tunnelNo') + '：'" required>
            <el-input v-model="formData.tunnelNo" placeholder=""></el-input>
          </el-form-item>
          <el-form-item :label="t('tabPage.etagTag.areaId') + '：'" required>
            <el-input v-model="formData.areaId" 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="deleteTag">{{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>