<template>
  <div class="min-h-screen bg-gray-100 dark:bg-gray-900">
    <!-- 顶部导航 -->
    <div class="bg-white dark:bg-gray-800 shadow">
      <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-4">
        <div class="flex justify-between items-center">
          <h1 class="text-2xl font-bold text-gray-900 dark:text-white">日志管理</h1>
          <div class="flex space-x-4">
            <button @click="clearLogs" class="bg-red-600 text-white px-4 py-2 rounded-md hover:bg-red-700">
              清空日志
            </button>
            <button @click="exportLogs" class="bg-purple-600 text-white px-4 py-2 rounded-md hover:bg-purple-700">
              导出日志
            </button>
          </div>
        </div>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
      <!-- 日志统计卡片 -->
      <div class="grid grid-cols-1 md:grid-cols-4 gap-6 mb-6">
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
          <h3 class="text-lg font-medium text-gray-900 dark:text-white">总日志数</h3>
          <p class="mt-2 text-3xl font-bold text-blue-600 dark:text-blue-400">{{ totalLogs }}</p>
        </div>
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
          <h3 class="text-lg font-medium text-gray-900 dark:text-white">错误日志</h3>
          <p class="mt-2 text-3xl font-bold text-red-600 dark:text-red-400">{{ errorLogs }}</p>
        </div>
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
          <h3 class="text-lg font-medium text-gray-900 dark:text-white">警告日志</h3>
          <p class="mt-2 text-3xl font-bold text-yellow-600 dark:text-yellow-400">{{ warningLogs }}</p>
        </div>
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
          <h3 class="text-lg font-medium text-gray-900 dark:text-white">信息日志</h3>
          <p class="mt-2 text-3xl font-bold text-green-600 dark:text-green-400">{{ infoLogs }}</p>
        </div>
      </div>

      <!-- 搜索和筛选 -->
      <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6 mb-6">
        <div class="grid grid-cols-1 md:grid-cols-4 gap-4">
          <div>
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">日志类型</label>
            <select v-model="filters.type" class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
              <option value="">全部</option>
              <option value="error">错误日志</option>
              <option value="warning">警告日志</option>
              <option value="info">信息日志</option>
              <option value="debug">调试日志</option>
            </select>
          </div>
          <div>
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">模块</label>
            <select v-model="filters.module" class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
              <option value="">全部</option>
              <option v-for="module in modules" :key="module" :value="module">{{ module }}</option>
            </select>
          </div>
          <div>
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">时间范围</label>
            <div class="flex space-x-2">
              <input type="datetime-local" v-model="filters.startTime" class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
              <input type="datetime-local" v-model="filters.endTime" class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
            </div>
          </div>
          <div>
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">搜索</label>
            <input type="text" v-model="filters.search" placeholder="日志内容/用户" class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
          </div>
        </div>
      </div>

      <!-- 日志列表 -->
      <div class="bg-white dark:bg-gray-800 rounded-lg shadow overflow-hidden">
        <table class="min-w-full divide-y divide-gray-200 dark:divide-gray-700">
          <thead class="bg-gray-50 dark:bg-gray-700">
            <tr>
              <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">时间</th>
              <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">类型</th>
              <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">模块</th>
              <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">用户</th>
              <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">内容</th>
              <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">操作</th>
            </tr>
          </thead>
          <tbody class="bg-white dark:bg-gray-800 divide-y divide-gray-200 dark:divide-gray-700">
            <tr v-for="log in filteredLogs" :key="log.id">
              <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500 dark:text-gray-400">
                {{ formatDateTime(log.timestamp) }}
              </td>
              <td class="px-6 py-4 whitespace-nowrap">
                <span :class="getTypeClass(log.type)" class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full">
                  {{ getTypeName(log.type) }}
                </span>
              </td>
              <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500 dark:text-gray-400">
                {{ log.module }}
              </td>
              <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500 dark:text-gray-400">
                {{ log.user }}
              </td>
              <td class="px-6 py-4">
                <div class="text-sm text-gray-900 dark:text-white">{{ log.content }}</div>
                <div v-if="log.details" class="text-xs text-gray-500 dark:text-gray-400 mt-1">{{ log.details }}</div>
              </td>
              <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                <button @click="viewLog(log)" class="text-blue-600 hover:text-blue-900 dark:text-blue-400 dark:hover:text-blue-300">查看详情</button>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>

    <!-- 日志详情模态框 -->
    <div v-if="showLogDetailModal" class="fixed inset-0 bg-gray-500 bg-opacity-75 flex items-center justify-center">
      <div class="bg-white dark:bg-gray-800 rounded-lg shadow-xl max-w-2xl w-full p-6">
        <h2 class="text-xl font-bold mb-4 text-gray-900 dark:text-white">日志详情</h2>
        <div class="space-y-4">
          <div>
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300">时间</label>
            <p class="mt-1 text-sm text-gray-900 dark:text-white">{{ formatDateTime(selectedLog.timestamp) }}</p>
          </div>
          <div>
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300">类型</label>
            <p class="mt-1">
              <span :class="getTypeClass(selectedLog.type)" class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full">
                {{ getTypeName(selectedLog.type) }}
              </span>
            </p>
          </div>
          <div>
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300">模块</label>
            <p class="mt-1 text-sm text-gray-900 dark:text-white">{{ selectedLog.module }}</p>
          </div>
          <div>
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300">用户</label>
            <p class="mt-1 text-sm text-gray-900 dark:text-white">{{ selectedLog.user }}</p>
          </div>
          <div>
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300">内容</label>
            <p class="mt-1 text-sm text-gray-900 dark:text-white">{{ selectedLog.content }}</p>
          </div>
          <div v-if="selectedLog.details">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300">详细信息</label>
            <pre class="mt-1 text-sm text-gray-900 dark:text-white whitespace-pre-wrap">{{ selectedLog.details }}</pre>
          </div>
          <div class="flex justify-end">
            <button @click="showLogDetailModal = false" class="px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700">
              关闭
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'

// 日志数据
const logs = ref([
  {
    id: 1,
    timestamp: '2024-03-15T10:30:00',
    type: 'error',
    module: '用户管理',
    user: 'admin',
    content: '用户登录失败',
    details: 'IP: 192.168.1.1\n原因: 密码错误'
  },
  {
    id: 2,
    timestamp: '2024-03-15T10:31:00',
    type: 'warning',
    module: '系统管理',
    user: 'system',
    content: '系统资源使用率过高',
    details: 'CPU: 85%\n内存: 75%'
  },
  {
    id: 3,
    timestamp: '2024-03-15T10:32:00',
    type: 'info',
    module: '项目管理',
    user: 'user1',
    content: '创建新项目',
    details: '项目名称: 测试项目\n创建人: user1'
  },
  {
    id: 4,
    timestamp: '2024-03-15T10:33:00',
    type: 'debug',
    module: '权限管理',
    user: 'admin',
    content: '权限检查',
    details: '用户: user1\n权限: read'
  }
])

// 模块列表
const modules = ref([
  '用户管理',
  '系统管理',
  '项目管理',
  '权限管理',
  '日志管理'
])

// 筛选条件
const filters = ref({
  type: '',
  module: '',
  startTime: '',
  endTime: '',
  search: ''
})

// 模态框状态
const showLogDetailModal = ref(false)
const selectedLog = ref({})

// 计算属性
const filteredLogs = computed(() => {
  return logs.value.filter(log => {
    const matchesType = !filters.value.type || log.type === filters.value.type
    const matchesModule = !filters.value.module || log.module === filters.value.module
    const matchesTime = (!filters.value.startTime || log.timestamp >= filters.value.startTime) &&
                       (!filters.value.endTime || log.timestamp <= filters.value.endTime)
    const matchesSearch = !filters.value.search ||
      log.content.toLowerCase().includes(filters.value.search.toLowerCase()) ||
      log.user.toLowerCase().includes(filters.value.search.toLowerCase())
    return matchesType && matchesModule && matchesTime && matchesSearch
  })
})

const totalLogs = computed(() => {
  return logs.value.length
})

const errorLogs = computed(() => {
  return logs.value.filter(log => log.type === 'error').length
})

const warningLogs = computed(() => {
  return logs.value.filter(log => log.type === 'warning').length
})

const infoLogs = computed(() => {
  return logs.value.filter(log => log.type === 'info').length
})

// 获取日志类型名称
const getTypeName = (type) => {
  const names = {
    error: '错误',
    warning: '警告',
    info: '信息',
    debug: '调试'
  }
  return names[type] || type
}

// 获取日志类型样式
const getTypeClass = (type) => {
  const classes = {
    error: 'bg-red-100 text-red-800 dark:bg-red-700 dark:text-red-300',
    warning: 'bg-yellow-100 text-yellow-800 dark:bg-yellow-700 dark:text-yellow-300',
    info: 'bg-blue-100 text-blue-800 dark:bg-blue-700 dark:text-blue-300',
    debug: 'bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300'
  }
  return classes[type] || classes.info
}

// 格式化日期时间
const formatDateTime = (datetime) => {
  const date = new Date(datetime)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  })
}

// 查看日志详情
const viewLog = (log) => {
  selectedLog.value = log
  showLogDetailModal.value = true
}

// 清空日志
const clearLogs = () => {
  if (confirm('确定要清空所有日志吗？此操作不可恢复。')) {
    logs.value = []
  }
}

// 导出日志
const exportLogs = () => {
  // 实现导出日志的逻辑
  console.log('导出日志')
}
</script> 