<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"
    />

    <!-- 提测邮件生成页面 -->
    <test-mail-generator
      v-else-if="currentPage === 'test-mail'"
      ref="testMailGenerator"
      :is-admin="isAdmin"
    />

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

    <!-- 进产诊断页面 -->
    <production-diagnosis
      v-else-if="currentPage === 'production-diagnosis'"
      ref="productionDiagnosis"
    />

    <!-- 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"
    />

    <!-- SLS 日志分析页面 -->
    <log-analysis
      v-else-if="currentPage === 'log-analysis'"
      :is-admin="isAdmin"
    />

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

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

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

    <!-- 项目配置管理页面 -->
    <project-management v-else-if="currentPage === 'projects'" />

    <!-- 定时任务管理页面 -->
    <scheduled-tasks v-else-if="currentPage === 'scheduled-tasks'" />
  </admin-layout>
</template>

<script>
import AdminLayout from './AdminLayout.vue';
import EnvManagement from '../env/EnvManagement.vue';
import WeeklyReport from '../jira/WeeklyReport.vue';
import TestMailGenerator from '../jira/TestMailGenerator.vue';
import SqlGenerator from '../tools/SqlGenerator.vue';
import ProductionDiagnosis from '../tools/ProductionDiagnosis.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 LogAnalysis from '../log-analysis/LogAnalysis.vue';
import SystemSettings from './SystemSettings.vue';
import OperationLogs from './OperationLogs.vue';
import IpUserMappings from './IpUserMappings.vue';
import ProjectManagement from './ProjectManagement.vue';
import ScheduledTasks from './ScheduledTasks.vue';

export default {
  name: 'AdminDashboard',
  components: {
    AdminLayout,
    EnvManagement,
    WeeklyReport,
    TestMailGenerator,
    SqlGenerator,
    ProductionDiagnosis,
    JiraWorklog,
    MessageSync,
    EventConsumerSync,
    MessageDispatch,
    LogAnalysis,
    SystemSettings,
    OperationLogs,
    IpUserMappings,
    ProjectManagement,
    ScheduledTasks
  },
  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' || menu === 'projects' || menu === 'scheduled-tasks') && !this.isAdmin) {
        this.redirectToDefault();
        return;
      }

      this.currentPage = menu;

      // 更新页面标题
      const titles = {
        'env': '环境配置管理',
        'weekly-report': '生成周报',
        'test-mail': '生成提测邮件',
        'sql-generator': '生成SQL',
        'production-diagnosis': '进产诊断',
        'worklog': 'JIRA 工时查询',
        'message-sync': '消息同步',
        'event-consumer-sync': '事件消费者同步对比',
        'message-dispatch': '消息分发异常查询',
        'log-analysis': 'SLS 日志分析',
        'settings': '系统设置',
        'logs': '操作日志',
        'ip-mappings': 'IP 用户映射',
        'projects': '项目配置管理',
        'scheduled-tasks': '定时任务管理'
      };

      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 === '/production-diagnosis') {
        page = 'production-diagnosis';
      } else if (path === '/weekly-report') {
        page = 'weekly-report';
      } else if (path === '/test-mail') {
        page = 'test-mail';
      } 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 === '/log-analysis') {
        page = 'log-analysis';
      } else if (path === '/settings') {
        page = 'settings';
      } else if (path === '/logs') {
        page = 'logs';
      } else if (path === '/ip-mappings') {
        page = 'ip-mappings';
      } else if (path === '/projects') {
        page = 'projects';
      } else if (path === '/scheduled-tasks') {
        page = 'scheduled-tasks';
      }

      if ((page === 'ip-mappings' || page === 'projects' || page === 'scheduled-tasks') && !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>
