<script setup lang="ts">

import type {IMenuSearch} from "@/interface/page/menu/IMenuSearch.ts";
import {computed, onMounted, ref} from "vue";
import {Lightning} from "@element-plus/icons-vue";
import type {IAppBaseMenu} from "@/model/table/IAppBaseMenu.ts"
import MessageUtils from "@/utils/MessageUtils.ts";
import MenuApi from "@/api/menu.ts";
import type {AppServeDataResponse} from "@/interface/api/AppServeDataResponse.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";
import {FormatterUtils} from "@/utils/FormatterUtils.ts";
import StringUtils from "@/utils/StringUtils.ts";
import MenuTagStyleFormatter from "@/plugin/formatter/MenuTagStyleFormatter.ts"
import AddMenuComponent from "@/components/page/menu/AddMenuComponent.vue";
import MenuDetailComponent from "@/components/page/menu/MenuDetailComponent.vue";

const menuSearch = ref<IMenuSearch>({menuId: '', menuName: '', menuLevel: null}); // 查询参数
const menuTagStyleFormatter = new MenuTagStyleFormatter(); // 表格数据tag格式化
const queryMenuResult = ref<IAppBaseMenu[]>([]); // 查询到的菜单数据
const showAddMenuComponent = ref<boolean>(false); // 是否展示添加菜单的弹窗
const showMenuDetailComponent = ref<boolean>(false); // 是否展示菜单详情的弹窗
const menuDetailComponentData = ref<IAppBaseMenu>({}); // 菜单详情传入的数据

onMounted(() => {
  query();
});

// 查询数据按钮
const query = () => {
  const loading = MessageUtils.loading('正在查询');
  MenuApi.queryMenu(menuSearch.value).then((result) => {
    const responseString = JSON.stringify(result.data);
    const response = JSON.parse(responseString) as AppServeDataResponse<IAppBaseMenu[]>;
    if (response && response.code == AppServeResponseCodeEnum.SUCCESS && response.data != undefined) {
      queryMenuResult.value = response.data;
      MessageUtils.successMessage('加载菜单信息成功');
      return;
    }
    MessageUtils.warningMessageBox(response.msg, '警告');
  }).catch(() => {}).finally(() => {
    loading.close();
  })
}
// 重置输入框按钮
const resetInput = () => {
  menuSearch.value = {menuId: '', menuName: '', menuLevel: null};
}
// 添加菜单按钮
const addMenu = () => {
  showAddMenuComponent.value = true;
}
const handleEdit = (index: number, row: any) => {
  menuDetailComponentData.value = {
    menuId: row.menuId,
    menuName: row.menuName,
    menuLevel: row.menuLevel,
    menuIco: row.menuIco,
    menuStatus: row.menuStatus,
    fatherMenuId: row.fatherMenuId,
    routerName: row.routerName,
    createTime: row.createTime,
    updateTime: row.updateTime,
  }
  showMenuDetailComponent.value = true;
}

</script>

<template>
  <div style="margin-left: 10px;margin-top: 10px; width: calc(100vw - 280px);">
    <h3>系统菜单管理</h3>
    <el-row class="searchCard">
      <el-row style="width: 100%;">
        <el-form class="searchForm" v-model="menuSearch" label-position="top" label-width="150px" inline>
          <el-form-item label="菜单ID：" style="width: 250px;">
            <el-input v-model="menuSearch.menuId" placeholder="请输入菜单ID" clearable></el-input>
          </el-form-item>
          <el-form-item label="菜单名称：" style="width: 250px;">
            <el-input v-model="menuSearch.menuName"  placeholder="请输入菜单名称" clearable></el-input>
          </el-form-item>
          <el-form-item label="菜单级别：" style="width: 250px;">
            <el-select v-model="menuSearch.menuLevel" placeholder="请选择菜单级别" style="width: 250px" clearable>
              <el-option :key="1" label="一级菜单" :value="1"/>
              <el-option :key="2" label="二级菜单" :value="2"/>
            </el-select>
          </el-form-item>
        </el-form>
      </el-row>
      <el-row>
        <el-button-group style="width: 100%;margin-left: 10px; margin-bottom: 10px">
          <el-button type="primary" @click="query">查询/刷新</el-button>
          <el-button type="warning" @click="resetInput">重置查询参数</el-button>
          <el-button type="success" @click="addMenu"><el-icon><Lightning /></el-icon>添加菜单</el-button>
        </el-button-group>
      </el-row>
    </el-row>
    <el-row class="dataTable">
      <el-table :data="queryMenuResult" stripe border style="width: 100%" max-height="calc(100vh - 270px)">
        <el-table-column type="index" width="80" label="序号" align="center" show-overflow-tooltip/>
        <el-table-column prop="menuId" label="菜单ID" width="100" align="center" show-overflow-tooltip />
        <el-table-column prop="menuName" label="菜单名称" align="center" show-overflow-tooltip/>
        <el-table-column prop="menuLevel" label="菜单级别" align="center" show-overflow-tooltip >
          <template #default="scope">
            <el-tag size="small" :type="menuTagStyleFormatter.menuLevelTagStyleFormatter(scope.row.menuLevel).type">
              {{menuTagStyleFormatter.menuLevelTagStyleFormatter(scope.row.menuLevel).label}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="fatherMenuId" label="上级菜单ID" width="100" align="center" show-overflow-tooltip/>
        <el-table-column prop="menuStatus" label="菜单状态" width="100" align="center" show-overflow-tooltip>
          <template #default="scope">
            <el-tag size="small" :type="menuTagStyleFormatter.menuStatusTagStyleFormatter(scope.row.menuStatus).type">
              {{menuTagStyleFormatter.menuStatusTagStyleFormatter(scope.row.menuStatus).label}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="menuIco" label="菜单图标" align="center" show-overflow-tooltip >
          <template #default="scope">
            <el-icon v-if="!StringUtils.isNullOrEmpty(scope.row.menuIco)" color="#000"><component :is='scope.row.menuIco'></component></el-icon>
          </template>
        </el-table-column>
        <el-table-column prop="routerName" label="菜单路由名称" align="center" show-overflow-tooltip/>
        <el-table-column prop="createTime" label="菜单创建时间" align="center" :formatter="FormatterUtils.formatCellValueTime" show-overflow-tooltip/>
        <el-table-column prop="updateTime" label="菜单更新时间" align="center" :formatter="FormatterUtils.formatCellValueTime" show-overflow-tooltip/>
        <el-table-column label="操作" width="80" align="center">
          <template #default="scope">
            <el-button size="small" type="primary" @click="handleEdit(scope.$index, scope.row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
  </div>

  <!-- 添加菜单 -->
  <AddMenuComponent v-model="showAddMenuComponent" @reLoadingTableData="query"></AddMenuComponent>
  <!-- 菜单详情 -->
  <MenuDetailComponent v-model="showMenuDetailComponent" :form-data="menuDetailComponentData"  @reLoadingTableData="query"></MenuDetailComponent>
</template>

<style scoped>
.searchCard {
  width: 100%;
  border: #ccc solid 1px;
  border-radius: 4px;
  background-color: #fff;
}
.dataTable {
  margin: 10px 10px 0 auto;
  width: 100%;
  border: #ccc solid 1px;
  border-radius: 4px;
  background-color: #fff;
}
.searchForm {
  margin: 5px 10px 0 10px;
  width: 100%;
}
.searchForm :deep(.el-form-item__label){
  font-size: 13px;
}
</style>