<template>
  <admin-layout
    :page-title="pageTitle"
    :is-admin="isAdmin"
    @menu-change="handleMenuChange"
  >
    <!-- 环境管理页面 -->
    <env-management
      v-if="currentPage === 'env'"
      ref="envManagement"
    />

    <!-- 生成周报页面 -->
    <weekly-report
      v-else-if="currentPage === 'weekly-report'"
      ref="weeklyReport"
    />

    <!-- SQL 生成页面 -->
    <sql-generator
      v-else-if="currentPage === 'sql-generator'"
      ref="sqlGenerator"
    />

    <!-- JIRA 工时查询页面 -->
    <jira-worklog
      v-else-if="currentPage === 'worklog'"
      ref="jiraWorklog"
    />

    <!-- 消息同步页面 -->
    <message-sync
      v-else-if="currentPage === 'message-sync'"
      ref="messageSync"
    />

    <!-- 事件消费者同步页面 -->
    <event-consumer-sync
      v-else-if="currentPage === 'event-consumer-sync'"
      ref="eventConsumerSync"
    />

    <!-- 消息分发异常查询页面 -->
    <message-dispatch
      v-else-if="currentPage === 'message-dispatch'"
      ref="messageDispatch"
    />

    <!-- 系统设置页面 -->
    <system-settings v-else-if="currentPage === 'settings'" />

    <!-- 操作日志页面 -->
    <operation-logs v-else-if="currentPage === 'logs'" :is-admin="isAdmin" :current-ip="currentIp" />

    <!-- IP 用户映射页面 -->
    <ip-user-mappings v-else-if="currentPage === 'ip-mappings'" />
  </admin-layout>
</template>

<script>
import AdminLayout from './AdminLayout.vue';
import EnvManagement from '../env/EnvManagement.vue';
import WeeklyReport from '../jira/WeeklyReport.vue';
import SqlGenerator from '../tools/SqlGenerator.vue';
import JiraWorklog from '../jira/JiraWorklog.vue';
import MessageSync from '../message-sync/MessageSync.vue';
import EventConsumerSync from '../message-sync/EventConsumerSync.vue';
import MessageDispatch from '../message-sync/MessageDispatch.vue';
import SystemSettings from './SystemSettings.vue';
import OperationLogs from './OperationLogs.vue';
import IpUserMappings from './IpUserMappings.vue';

export default {
  name: 'AdminDashboard',
  components: {
    AdminLayout,
    EnvManagement,
    WeeklyReport,
    SqlGenerator,
    JiraWorklog,
    MessageSync,
    EventConsumerSync,
    MessageDispatch,
    SystemSettings,
    OperationLogs,
    IpUserMappings
  },
  data() {
    return {
      currentPage: 'env',
      pageTitle: '环境配置管理',
      isAdmin: false,
      currentIp: ''
    }
  },
  async mounted() {
    await this.loadAdminMeta();
    this.setCurrentPageFromPath();
  },
  methods: {
    async loadAdminMeta() {
      try {
        const response = await fetch('/api/admin/meta');
        const data = await response.json();

        if (data.success) {
          this.isAdmin = Boolean(data.data.is_admin);
          this.currentIp = data.data.ip || '';
        }
      } catch (error) {
        this.isAdmin = false;
        this.currentIp = '';
      }
    },
    handleMenuChange(menu) {
      if (menu === 'ip-mappings' && !this.isAdmin) {
        this.redirectToDefault();
        return;
      }

      this.currentPage = menu;

      // 更新页面标题
      const titles = {
        'env': '环境配置管理',
        'weekly-report': '生成周报',
        'sql-generator': '生成SQL',
        'worklog': 'JIRA 工时查询',
        'message-sync': '消息同步',
        'event-consumer-sync': '事件消费者同步对比',
        'message-dispatch': '消息分发异常查询',
        'settings': '系统设置',
        'logs': '操作日志',
        'ip-mappings': 'IP 用户映射'
      };

      this.pageTitle = titles[menu] || '环境配置管理';
    },

    setCurrentPageFromPath() {
      const path = window.location.pathname;
      let page = 'env'; // 默认页面

      if (path === '/') {
        page = 'env';
      } else if (path === '/sql-generator') {
        page = 'sql-generator';
      } else if (path === '/weekly-report') {
        page = 'weekly-report';
      } else if (path === '/worklog') {
        page = 'worklog';
      } else if (path === '/message-sync') {
        page = 'message-sync';
      } else if (path === '/event-consumer-sync') {
        page = 'event-consumer-sync';
      } else if (path === '/message-dispatch') {
        page = 'message-dispatch';
      } else if (path === '/settings') {
        page = 'settings';
      } else if (path === '/logs') {
        page = 'logs';
      } else if (path === '/ip-mappings') {
        page = 'ip-mappings';
      }

      if (page === 'ip-mappings' && !this.isAdmin) {
        this.redirectToDefault();
        return;
      }

      this.currentPage = page;
      this.handleMenuChange(page);
    },

    redirectToDefault() {
      this.currentPage = 'env';
      this.pageTitle = '环境配置管理';
      if (window.location.pathname !== '/') {
        window.history.replaceState({}, '', '/');
      }
    }
  }
}
</script>

<style scoped>
/* 主应用样式 */
</style>
