<script setup lang="ts">
// 菜单详情
import {computed, ref} from "vue";
import {FormatterUtils} from "@/utils/FormatterUtils.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import MenuApi from "@/api/menu.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 props = defineProps(['formData']);
const emit = defineEmits(['reLoadingTableData']);
const close = () => {
  modelValue.value = false;
}
const formData = computed(() => props.formData);

// 删除菜单
const deleteMenu = () => {
  MessageUtils.confirmMessageBox('你确定要删除这个菜单？删除后不可恢复！', '删除二次确认').then(() => {
    const loading = MessageUtils.loading("正在删除");
    MenuApi.deleteWithMenuId(formData.value.menuId).then(res => {
      const response = JSON.parse(JSON.stringify(res.data)) as AppServeResponse;
      if(response.code == AppServeResponseCodeEnum.SUCCESS) {
        MessageUtils.successMessage("删除成功");
        emit('reLoadingTableData');
        close();
        return;
      }
      MessageUtils.warningMessageBox(response.msg, '警告');
    }).catch(() => {}).finally(() => {
      loading.close();
    })
  }).catch(() => {});
}
// 保存修改
const save = () => {
  MessageUtils.confirmMessageBox('确定更新为新数据？', '更新二次确认').then(() => {
    const loading = MessageUtils.loading("正在请求服务器");
    MenuApi.updateMenu(formData.value).then(res => {
      const response = JSON.parse(JSON.stringify(res.data)) as AppServeResponse;
      if(response.code == AppServeResponseCodeEnum.SUCCESS) {
        MessageUtils.successMessage("更新成功");
        emit('reLoadingTableData');
        close();
        return;
      }
      MessageUtils.warningMessageBox(response.msg, '警告');
    }).catch(() => {}).finally(() => {
      loading.close();
    })
  }).catch(() => {});

}
</script>

<template>
  <div>
    <el-dialog :model-value="modelValue" title="修改菜单详细信息" @close="close" :close-on-click-modal="false">
      <el-scrollbar style="height:50vh">
        <el-form :model="formData" label-position="right" label-width="100px" require-asterisk-position="right">
          <el-form-item label="菜单ID:" required>
            <el-text>{{formData.menuId}}</el-text>
          </el-form-item>
          <el-form-item label="菜单名称:" required>
            <el-input v-model="formData.menuName" placeholder="请填写菜单名称"></el-input>
          </el-form-item>
          <el-form-item label="菜单级别:" required>
            <el-select v-model="formData.menuLevel" placeholder="请选择菜单级别" disabled clearable>
              <el-option :key="1" label="一级菜单" :value="1"/>
              <el-option :key="2" label="二级菜单" :value="2"/>
            </el-select>
          </el-form-item>
          <el-form-item label="上级菜单ID:">
            <el-input v-model="formData.fatherMenuId" placeholder="请填写上级菜单ID"></el-input>
          </el-form-item>
          <el-form-item label="菜单状态:" required>
            <el-switch v-model="formData.menuStatus" inline-prompt active-text="启用" :active-value="1" inactive-text="停用" :inactive-value="0"></el-switch>
          </el-form-item>
          <el-form-item label="菜单图标:">
            <el-input v-model="formData.menuIco" placeholder="请填写图标名称，仅限element-plus自带图标"></el-input>
          </el-form-item>
          <el-form-item label="菜单路由名称:">
            <el-input v-model="formData.routerName" placeholder="请填写菜单路由名称"></el-input>
          </el-form-item>
          <el-form-item label="菜单创建时间:">
            <el-text>{{FormatterUtils.formatTime(formData.createTime)}}</el-text>
          </el-form-item>
          <el-form-item label="上次修改时间:">
            <el-text>{{FormatterUtils.formatTime(formData.updateTime)}}</el-text>
          </el-form-item>
        </el-form>
      </el-scrollbar>
      <template #footer>
        <el-button-group>
          <el-button type="warning" @click="deleteMenu">删除菜单(!)</el-button>
          <el-button type="primary" @click="save">保存修改</el-button>
          <el-button type="danger" @click="close">关闭窗口</el-button>
        </el-button-group>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>