<template>
  <div class="admin-layout h-screen bg-gray-50">
    <!-- 侧边栏 -->
    <div class="fixed inset-y-0 left-0 z-50 w-64 bg-white shadow-lg border-r border-gray-200 flex flex-col">
      <!-- Logo区域 -->
      <div class="flex items-center justify-center h-16 px-6 bg-gradient-to-r from-blue-600 to-blue-700 border-b border-blue-800 flex-shrink-0">
        <div class="flex items-center space-x-3">
          <div class="w-8 h-8 bg-white rounded-lg flex items-center justify-center">
            <svg class="w-5 h-5 text-blue-600" fill="currentColor" viewBox="0 0 20 20">
              <path d="M3 4a1 1 0 011-1h12a1 1 0 011 1v2a1 1 0 01-1 1H4a1 1 0 01-1-1V4zM3 10a1 1 0 011-1h6a1 1 0 011 1v6a1 1 0 01-1 1H4a1 1 0 01-1-1v-6zM14 9a1 1 0 00-1 1v6a1 1 0 001 1h2a1 1 0 001-1v-6a1 1 0 00-1-1h-2z"/>
            </svg>
          </div>
          <div class="text-white">
            <div class="text-lg font-bold">Tradewind Toolbox</div>
            <div class="text-xs text-blue-100">Development Tools</div>
          </div>
        </div>
      </div>

      <!-- 导航菜单 -->
      <nav class="mt-6 px-3 flex flex-col flex-1">
        <div class="space-y-1 flex-1">
          <a
            href="#"
            @click.prevent="setActiveMenu('env')"
            :class="[
              'group flex items-center px-3 py-2 text-sm font-medium rounded-lg transition-colors duration-200',
              activeMenu === 'env'
                ? 'bg-blue-50 text-blue-700 border-r-2 border-blue-700'
                : 'text-gray-700 hover:bg-gray-50 hover:text-gray-900'
            ]"
          >
            <svg
              :class="[
                'mr-3 h-5 w-5 transition-colors duration-200',
                activeMenu === 'env' ? 'text-blue-500' : 'text-gray-400 group-hover:text-gray-500'
              ]"
              fill="none"
              stroke="currentColor"
              viewBox="0 0 24 24"
            >
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
            </svg>
            环境配置管理
          </a>

          <a
            href="#"
            @click.prevent="setActiveMenu('sql-generator')"
            :class="[
              'group flex items-center px-3 py-2 text-sm font-medium rounded-lg transition-colors duration-200',
              activeMenu === 'sql-generator'
                ? 'bg-blue-50 text-blue-700 border-r-2 border-blue-700'
                : 'text-gray-700 hover:bg-gray-50 hover:text-gray-900'
            ]"
          >
            <svg
              :class="[
                'mr-3 h-5 w-5 transition-colors duration-200',
                activeMenu === 'sql-generator' ? 'text-blue-500' : 'text-gray-400 group-hover:text-gray-500'
              ]"
              fill="none"
              stroke="currentColor"
              viewBox="0 0 24 24"
            >
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
            </svg>
            生成SQL
          </a>

          <!-- JIRA 相关菜单项 -->

          <a
            href="#"
            @click.prevent="setActiveMenu('weekly-report')"
            :class="[
              'group flex items-center px-3 py-2 text-sm font-medium rounded-lg transition-colors duration-200',
              activeMenu === 'weekly-report'
                ? 'bg-blue-50 text-blue-700 border-r-2 border-blue-700'
                : 'text-gray-700 hover:bg-gray-50 hover:text-gray-900'
            ]"
          >
            <svg
              :class="[
                'mr-3 h-5 w-5 transition-colors duration-200',
                activeMenu === 'weekly-report' ? 'text-blue-500' : 'text-gray-400 group-hover:text-gray-500'
              ]"
              fill="none"
              stroke="currentColor"
              viewBox="0 0 24 24"
            >
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
            </svg>
            生成周报
          </a>

          <a
            href="#"
            @click.prevent="setActiveMenu('worklog')"
            :class="[
              'group flex items-center px-3 py-2 text-sm font-medium rounded-lg transition-colors duration-200',
              activeMenu === 'worklog'
                ? 'bg-blue-50 text-blue-700 border-r-2 border-blue-700'
                : 'text-gray-700 hover:bg-gray-50 hover:text-gray-900'
            ]"
          >
            <svg
              :class="[
                'mr-3 h-5 w-5 transition-colors duration-200',
                activeMenu === 'worklog' ? 'text-blue-500' : 'text-gray-400 group-hover:text-gray-500'
              ]"
              fill="none"
              stroke="currentColor"
              viewBox="0 0 24 24"
            >
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"/>
            </svg>
            JIRA 工时查询
          </a>

          <a
            href="#"
            @click.prevent="setActiveMenu('message-sync')"
            :class="[
              'group flex items-center px-3 py-2 text-sm font-medium rounded-lg transition-colors duration-200',
              activeMenu === 'message-sync'
                ? 'bg-blue-50 text-blue-700 border-r-2 border-blue-700'
                : 'text-gray-700 hover:bg-gray-50 hover:text-gray-900'
            ]"
          >
            <svg
              :class="[
                'mr-3 h-5 w-5 transition-colors duration-200',
                activeMenu === 'message-sync' ? 'text-blue-500' : 'text-gray-400 group-hover:text-gray-500'
              ]"
              fill="none"
              stroke="currentColor"
              viewBox="0 0 24 24"
            >
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z"/>
            </svg>
            消息同步
          </a>

          <a
            href="#"
            @click.prevent="setActiveMenu('event-consumer-sync')"
            :class="[
              'group flex items-center px-3 py-2 text-sm font-medium rounded-lg transition-colors duration-200',
              activeMenu === 'event-consumer-sync'
                ? 'bg-blue-50 text-blue-700 border-r-2 border-blue-700'
                : 'text-gray-700 hover:bg-gray-50 hover:text-gray-900'
            ]"
          >
            <svg
              :class="[
                'mr-3 h-5 w-5 transition-colors duration-200',
                activeMenu === 'event-consumer-sync' ? 'text-blue-500' : 'text-gray-400 group-hover:text-gray-500'
              ]"
              fill="none"
              stroke="currentColor"
              viewBox="0 0 24 24"
            >
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z"/>
            </svg>
            事件消费者同步
          </a>

          <a
            href="#"
            @click.prevent="setActiveMenu('message-dispatch')"
            :class="[
              'group flex items-center px-3 py-2 text-sm font-medium rounded-lg transition-colors duration-200',
              activeMenu === 'message-dispatch'
                ? 'bg-blue-50 text-blue-700 border-r-2 border-blue-700'
                : 'text-gray-700 hover:bg-gray-50 hover:text-gray-900'
            ]"
          >
            <svg
              :class="[
                'mr-3 h-5 w-5 transition-colors duration-200',
                activeMenu === 'message-dispatch' ? 'text-blue-500' : 'text-gray-400 group-hover:text-gray-500'
              ]"
              fill="none"
              stroke="currentColor"
              viewBox="0 0 24 24"
            >
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"/>
            </svg>
            消息分发异常
          </a>

          <a
            href="#"
            @click.prevent="setActiveMenu('log-analysis')"
            :class="[
              'group flex items-center px-3 py-2 text-sm font-medium rounded-lg transition-colors duration-200',
              activeMenu === 'log-analysis'
                ? 'bg-blue-50 text-blue-700 border-r-2 border-blue-700'
                : 'text-gray-700 hover:bg-gray-50 hover:text-gray-900'
            ]"
          >
            <svg
              :class="[
                'mr-3 h-5 w-5 transition-colors duration-200',
                activeMenu === 'log-analysis' ? 'text-blue-500' : 'text-gray-400 group-hover:text-gray-500'
              ]"
              fill="none"
              stroke="currentColor"
              viewBox="0 0 24 24"
            >
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z"/>
            </svg>
            SLS 日志分析
          </a>

          <a
            href="#"
            @click.prevent="setActiveMenu('settings')"
            :class="[
              'group flex items-center px-3 py-2 text-sm font-medium rounded-lg transition-colors duration-200',
              activeMenu === 'settings'
                ? 'bg-blue-50 text-blue-700 border-r-2 border-blue-700'
                : 'text-gray-700 hover:bg-gray-50 hover:text-gray-900'
            ]"
          >
            <svg
              :class="[
                'mr-3 h-5 w-5 transition-colors duration-200',
                activeMenu === 'settings' ? 'text-blue-500' : 'text-gray-400 group-hover:text-gray-500'
              ]"
              fill="none"
              stroke="currentColor"
              viewBox="0 0 24 24"
            >
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"/>
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/>
            </svg>
            系统设置
          </a>

          <a
            href="#"
            @click.prevent="setActiveMenu('logs')"
            :class="[
              'group flex items-center px-3 py-2 text-sm font-medium rounded-lg transition-colors duration-200',
              activeMenu === 'logs'
                ? 'bg-blue-50 text-blue-700 border-r-2 border-blue-700'
                : 'text-gray-700 hover:bg-gray-50 hover:text-gray-900'
            ]"
          >
            <svg
              :class="[
                'mr-3 h-5 w-5 transition-colors duration-200',
                activeMenu === 'logs' ? 'text-blue-500' : 'text-gray-400 group-hover:text-gray-500'
              ]"
              fill="none"
              stroke="currentColor"
              viewBox="0 0 24 24"
            >
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 17v-2m3 2v-4m3 4v-6m2 10H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
            </svg>
            操作日志
          </a>

          <a
            href="#"
            @click.prevent="setActiveMenu('ip-mappings')"
            v-if="isAdmin"
            :class="[
              'group flex items-center px-3 py-2 text-sm font-medium rounded-lg transition-colors duration-200',
              activeMenu === 'ip-mappings'
                ? 'bg-blue-50 text-blue-700 border-r-2 border-blue-700'
                : 'text-gray-700 hover:bg-gray-50 hover:text-gray-900'
            ]"
          >
            <svg
              :class="[
                'mr-3 h-5 w-5 transition-colors duration-200',
                activeMenu === 'ip-mappings' ? 'text-blue-500' : 'text-gray-400 group-hover:text-gray-500'
              ]"
              fill="none"
              stroke="currentColor"
              viewBox="0 0 24 24"
            >
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"/>
            </svg>
            IP 用户映射
          </a>
        </div>

        <!-- 底部信息 -->
        <div class="mt-auto pt-6 border-t border-gray-200">
          <div class="px-3 py-2">
            <div class="text-xs text-gray-500 mb-1">系统信息</div>
            <div class="text-xs text-gray-400">版本 v1.0.0</div>
            <div class="text-xs text-gray-400">Laravel 12 + Vue 3</div>
          </div>
        </div>
      </nav>
    </div>

    <!-- 主内容区域 -->
    <div class="pl-64 h-screen">
      <!-- 页面内容 -->
      <main class="h-full">
        <slot></slot>
      </main>
    </div>
  </div>
</template>

<script>
export default {
  name: 'AdminLayout',
  props: {
    pageTitle: {
      type: String,
      default: '环境配置管理'
    },
    isAdmin: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      activeMenu: 'env'
    }
  },
  watch: {
    isAdmin() {
      this.setActiveMenuFromPath();
    }
  },
  mounted() {
    // 根据 URL 路径设置初始菜单
    this.setActiveMenuFromPath();

    // 监听浏览器前进后退按钮
    window.addEventListener('popstate', this.setActiveMenuFromPath);
  },
  beforeUnmount() {
    window.removeEventListener('popstate', this.setActiveMenuFromPath);
  },
  methods: {
    setActiveMenu(menu) {
      if (menu === 'ip-mappings' && !this.isAdmin) {
        return;
      }

      this.activeMenu = menu;
      // 更新 URL 路径
      const path = menu === 'env' ? '/' : `/${menu}`;
      window.history.pushState({}, '', path);
      this.$emit('menu-change', menu);
    },

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

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

      if (menu === 'ip-mappings' && !this.isAdmin) {
        menu = 'env';
      }

      this.activeMenu = menu;
      this.$emit('menu-change', menu);
    }
  }
}
</script>

<style scoped>
.admin-layout {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .admin-layout .pl-64 {
    padding-left: 0;
  }
  
  .admin-layout .fixed.w-64 {
    transform: translateX(-100%);
    transition: transform 0.3s ease-in-out;
  }
}
</style>
