<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="showAddUserModal = true" class="bg-blue-600 text-white px-4 py-2 rounded-md hover:bg-blue-700">
              添加用户
            </button>
            <button @click="exportUserData" 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">{{ totalUsers }}</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">{{ activeUsers }}</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">{{ adminUsers }}</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">{{ weeklyNewUsers }}</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.department" 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="dept in departments" :key="dept.id" :value="dept.id">{{ dept.name }}</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.role" 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="role in roles" :key="role.id" :value="role.id">{{ role.name }}</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.status" 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="active">活跃</option>
              <option value="inactive">未激活</option>
              <option value="locked">已锁定</option>
            </select>
          </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>
            </tr>
          </thead>
          <tbody class="bg-white dark:bg-gray-800 divide-y divide-gray-200 dark:divide-gray-700">
            <tr v-for="user in filteredUsers" :key="user.id">
              <td class="px-6 py-4">
                <div class="flex items-center">
                  <div class="flex-shrink-0 h-10 w-10">
                    <img class="h-10 w-10 rounded-full" :src="user.avatar" :alt="user.name">
                  </div>
                  <div class="ml-4">
                    <div class="text-sm font-medium text-gray-900 dark:text-white">{{ user.name }}</div>
                    <div class="text-sm text-gray-500 dark:text-gray-400">{{ user.email }}</div>
                  </div>
                </div>
              </td>
              <td class="px-6 py-4">
                <div class="text-sm text-gray-900 dark:text-white">{{ getDepartmentName(user.departmentId) }}</div>
                <div class="text-sm text-gray-500 dark:text-gray-400">{{ user.position }}</div>
              </td>
              <td class="px-6 py-4">
                <div class="flex flex-wrap gap-1">
                  <span v-for="roleId in user.roles" :key="roleId" class="px-2 py-1 text-xs rounded-full bg-blue-100 text-blue-800 dark:bg-blue-700 dark:text-blue-300">
                    {{ getRoleName(roleId) }}
                  </span>
                </div>
              </td>
              <td class="px-6 py-4 whitespace-nowrap">
                <span :class="getStatusClass(user.status)" class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full">
                  {{ getStatusName(user.status) }}
                </span>
              </td>
              <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                <button @click="viewUser(user)" class="text-blue-600 hover:text-blue-900 dark:text-blue-400 dark:hover:text-blue-300 mr-3">查看</button>
                <button @click="editUser(user)" class="text-green-600 hover:text-green-900 dark:text-green-400 dark:hover:text-green-300 mr-3">编辑</button>
                <button @click="deleteUser(user)" class="text-red-600 hover:text-red-900 dark:text-red-400 dark:hover:text-red-300">删除</button>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>

    <!-- 添加用户模态框 -->
    <div v-if="showAddUserModal" 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-md w-full p-6">
        <h2 class="text-xl font-bold mb-4 text-gray-900 dark:text-white">添加用户</h2>
        <form @submit.prevent="addUser">
          <div class="mb-4">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">用户名</label>
            <input type="text" v-model="newUser.username" required 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 class="mb-4">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">姓名</label>
            <input type="text" v-model="newUser.name" required 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 class="mb-4">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">邮箱</label>
            <input type="email" v-model="newUser.email" required 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 class="mb-4">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">部门</label>
            <select v-model="newUser.departmentId" required 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 v-for="dept in departments" :key="dept.id" :value="dept.id">{{ dept.name }}</option>
            </select>
          </div>
          <div class="mb-4">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">职位</label>
            <input type="text" v-model="newUser.position" required 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 class="mb-4">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">角色</label>
            <select v-model="newUser.roles" multiple 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 v-for="role in roles" :key="role.id" :value="role.id">{{ role.name }}</option>
            </select>
          </div>
          <div class="flex justify-end space-x-3">
            <button type="button" @click="showAddUserModal = false" class="px-4 py-2 border border-gray-300 rounded-md text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700">
              取消
            </button>
            <button type="submit" class="px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700">
              创建
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

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

// 用户数据
const users = ref([
  {
    id: 1,
    username: 'admin',
    name: '管理员',
    email: 'admin@example.com',
    avatar: 'https://ui-avatars.com/api/?name=管理员&background=0D8ABC&color=fff',
    departmentId: 1,
    position: '系统管理员',
    roles: [1],
    status: 'active',
    lastLogin: '2024-03-20T10:30:00'
  },
  {
    id: 2,
    username: 'user1',
    name: '张三',
    email: 'user1@example.com',
    avatar: 'https://ui-avatars.com/api/?name=张三&background=0D8ABC&color=fff',
    departmentId: 2,
    position: '开发工程师',
    roles: [2],
    status: 'active',
    lastLogin: '2024-03-20T09:15:00'
  },
  {
    id: 3,
    username: 'user2',
    name: '李四',
    email: 'user2@example.com',
    avatar: 'https://ui-avatars.com/api/?name=李四&background=0D8ABC&color=fff',
    departmentId: 3,
    position: '产品经理',
    roles: [2],
    status: 'inactive',
    lastLogin: '2024-03-19T16:45:00'
  }
])

// 部门数据
const departments = ref([
  { id: 1, name: '管理部' },
  { id: 2, name: '技术部' },
  { id: 3, name: '产品部' },
  { id: 4, name: '市场部' }
])

// 角色数据
const roles = ref([
  { id: 1, name: '管理员' },
  { id: 2, name: '普通用户' },
  { id: 3, name: '访客' }
])

// 筛选条件
const filters = ref({
  department: '',
  role: '',
  status: '',
  search: ''
})

// 模态框状态
const showAddUserModal = ref(false)

// 新用户表单
const newUser = ref({
  username: '',
  name: '',
  email: '',
  departmentId: '',
  position: '',
  roles: [],
  status: 'active'
})

// 计算属性
const filteredUsers = computed(() => {
  return users.value.filter(user => {
    const matchesDepartment = !filters.value.department || user.departmentId === filters.value.department
    const matchesRole = !filters.value.role || user.roles.includes(filters.value.role)
    const matchesStatus = !filters.value.status || user.status === filters.value.status
    const matchesSearch = !filters.value.search ||
      user.username.toLowerCase().includes(filters.value.search.toLowerCase()) ||
      user.name.toLowerCase().includes(filters.value.search.toLowerCase()) ||
      user.email.toLowerCase().includes(filters.value.search.toLowerCase())
    return matchesDepartment && matchesRole && matchesStatus && matchesSearch
  })
})

const totalUsers = computed(() => {
  return users.value.length
})

const activeUsers = computed(() => {
  return users.value.filter(user => user.status === 'active').length
})

const adminUsers = computed(() => {
  return users.value.filter(user => user.roles.includes(1)).length
})

const weeklyNewUsers = computed(() => {
  const oneWeekAgo = new Date()
  oneWeekAgo.setDate(oneWeekAgo.getDate() - 7)
  return users.value.filter(user => new Date(user.lastLogin) >= oneWeekAgo).length
})

// 获取部门名称
const getDepartmentName = (departmentId) => {
  const department = departments.value.find(d => d.id === departmentId)
  return department ? department.name : '未知部门'
}

// 获取角色名称
const getRoleName = (roleId) => {
  const role = roles.value.find(r => r.id === roleId)
  return role ? role.name : '未知角色'
}

// 获取状态名称
const getStatusName = (status) => {
  const names = {
    active: '活跃',
    inactive: '未激活',
    locked: '已锁定'
  }
  return names[status] || status
}

// 获取状态样式
const getStatusClass = (status) => {
  const classes = {
    active: 'bg-green-100 text-green-800 dark:bg-green-700 dark:text-green-300',
    inactive: 'bg-yellow-100 text-yellow-800 dark:bg-yellow-700 dark:text-yellow-300',
    locked: 'bg-red-100 text-red-800 dark:bg-red-700 dark:text-red-300'
  }
  return classes[status] || classes.active
}

// 添加用户
const addUser = () => {
  const user = {
    id: users.value.length + 1,
    avatar: `https://ui-avatars.com/api/?name=${newUser.value.name}&background=0D8ABC&color=fff`,
    lastLogin: new Date().toISOString(),
    ...newUser.value
  }
  
  users.value.push(user)
  showAddUserModal.value = false
  resetNewUser()
}

// 重置新用户表单
const resetNewUser = () => {
  newUser.value = {
    username: '',
    name: '',
    email: '',
    departmentId: '',
    position: '',
    roles: [],
    status: 'active'
  }
}

// 查看用户
const viewUser = (user) => {
  // 实现查看用户的逻辑
  console.log('查看用户:', user)
}

// 编辑用户
const editUser = (user) => {
  // 实现编辑用户的逻辑
  console.log('编辑用户:', user)
}

// 删除用户
const deleteUser = (user) => {
  if (confirm(`确定要删除用户 ${user.name} 吗？`)) {
    const index = users.value.findIndex(u => u.id === user.id)
    if (index !== -1) {
      users.value.splice(index, 1)
    }
  }
}

// 导出用户数据
const exportUserData = () => {
  // 实现导出用户数据的逻辑
  console.log('导出用户数据')
}
</script> 