<script setup lang="ts">
// 添加菜单的组件
import type {IAppBaseMenu} from "@/model/table/IAppBaseMenu.ts";
import {ref} from "vue";
import StringUtils from "@/utils/StringUtils.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 emit = defineEmits(['reLoadingTableData']);
const formData = ref<IAppBaseMenu>(
    {
      menuId: '',
      menuName: '',
      menuLevel: null,
      fatherMenuId: null,
      menuStatus: 1,
      menuIco: null,
      routerName: null
    }
)
const close = () => {
  modelValue.value = false;
}
// 添加菜单
const add = () => {
  if(StringUtils.isNullOrEmpty(formData.value.menuId??'') || StringUtils.isNullOrEmpty(formData.value.menuName??'')
  || formData.value.menuLevel == null || formData.value.menuStatus == null) {
    MessageUtils.warningMessageBox('您有必填项未填', '提示');
    return;
  }
  const loading = MessageUtils.loading("请稍后");
  MenuApi.addMenu(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();
  })
}



</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-input v-model="formData.menuId" placeholder="请填写菜单ID"></el-input>
          </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="请选择菜单级别" 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 :disabled="formData.menuLevel != 2" v-model="formData.routerName" placeholder="请填写菜单路由名称"></el-input>
          </el-form-item>
        </el-form>
      </el-scrollbar>
      <template #footer>
        <el-button-group>
          <el-button type="primary" @click="add">确定添加</el-button>
          <el-button type="danger" @click="close">关闭窗口</el-button>
        </el-button-group>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>