<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="showAddRoleModal = true" class="bg-blue-600 text-white px-4 py-2 rounded-md hover:bg-blue-700">
              创建角色
            </button>
            <button @click="exportRoleData" 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">{{ totalRoles }}</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">{{ systemRoles }}</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">{{ customRoles }}</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">{{ activeRoles }}</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="system">系统角色</option>
              <option value="custom">自定义角色</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>
            </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="date" v-model="filters.startDate" 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="date" v-model="filters.endDate" 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>
            </tr>
          </thead>
          <tbody class="bg-white dark:bg-gray-800 divide-y divide-gray-200 dark:divide-gray-700">
            <tr v-for="role in filteredRoles" :key="role.id">
              <td class="px-6 py-4">
                <div class="text-sm font-medium text-gray-900 dark:text-white">{{ role.name }}</div>
                <div class="text-sm text-gray-500 dark:text-gray-400">{{ role.description }}</div>
              </td>
              <td class="px-6 py-4 whitespace-nowrap">
                <span :class="getTypeClass(role.type)" class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full">
                  {{ getTypeName(role.type) }}
                </span>
              </td>
              <td class="px-6 py-4">
                <div class="flex flex-wrap gap-1">
                  <span v-for="permission in role.permissions" :key="permission" class="px-2 py-1 text-xs rounded-full bg-blue-100 text-blue-800 dark:bg-blue-700 dark:text-blue-300">
                    {{ getPermissionName(permission) }}
                  </span>
                </div>
              </td>
              <td class="px-6 py-4 whitespace-nowrap">
                <span :class="getStatusClass(role.status)" class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full">
                  {{ getStatusName(role.status) }}
                </span>
              </td>
              <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                <button @click="viewRole(role)" class="text-blue-600 hover:text-blue-900 dark:text-blue-400 dark:hover:text-blue-300 mr-3">查看</button>
                <button @click="editRole(role)" class="text-green-600 hover:text-green-900 dark:text-green-400 dark:hover:text-green-300 mr-3">编辑</button>
                <button @click="deleteRole(role)" 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="showAddRoleModal" 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="addRole">
          <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="newRole.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>
            <textarea v-model="newRole.description" rows="3" 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"></textarea>
          </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="newRole.type" 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 value="system">系统角色</option>
              <option value="custom">自定义角色</option>
            </select>
          </div>
          <div class="mb-4">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">权限设置</label>
            <div class="space-y-2">
              <div v-for="permission in permissions" :key="permission.id" class="flex items-center">
                <input type="checkbox" :id="'permission-' + permission.id" v-model="newRole.permissions" :value="permission.id" class="rounded border-gray-300 text-blue-600 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600">
                <label :for="'permission-' + permission.id" class="ml-2 text-sm text-gray-700 dark:text-gray-300">{{ permission.name }}</label>
              </div>
            </div>
          </div>
          <div class="flex justify-end space-x-3">
            <button type="button" @click="showAddRoleModal = 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 roles = ref([
  {
    id: 1,
    name: '超级管理员',
    description: '系统最高权限角色',
    type: 'system',
    permissions: [1, 2, 3, 4, 5, 6, 7, 8],
    status: 'active',
    createdAt: '2024-01-01T00:00:00'
  },
  {
    id: 2,
    name: '部门管理员',
    description: '部门管理权限',
    type: 'system',
    permissions: [2, 3, 4, 5],
    status: 'active',
    createdAt: '2024-01-01T00:00:00'
  },
  {
    id: 3,
    name: '普通用户',
    description: '基础操作权限',
    type: 'system',
    permissions: [3, 4],
    status: 'active',
    createdAt: '2024-01-01T00:00:00'
  },
  {
    id: 4,
    name: '项目管理员',
    description: '项目管理权限',
    type: 'custom',
    permissions: [3, 4, 5, 6],
    status: 'active',
    createdAt: '2024-03-01T00:00:00'
  }
])

// 权限数据
const permissions = ref([
  { id: 1, name: '系统管理' },
  { id: 2, name: '用户管理' },
  { id: 3, name: '角色管理' },
  { id: 4, name: '权限管理' },
  { id: 5, name: '数据管理' },
  { id: 6, name: '日志管理' },
  { id: 7, name: '系统设置' },
  { id: 8, name: '高级功能' }
])

// 筛选条件
const filters = ref({
  type: '',
  status: '',
  startDate: '',
  endDate: '',
  search: ''
})

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

// 新角色表单
const newRole = ref({
  name: '',
  description: '',
  type: 'custom',
  permissions: [],
  status: 'active'
})

// 计算属性
const filteredRoles = computed(() => {
  return roles.value.filter(role => {
    const matchesType = !filters.value.type || role.type === filters.value.type
    const matchesStatus = !filters.value.status || role.status === filters.value.status
    const matchesDate = (!filters.value.startDate || role.createdAt >= filters.value.startDate) &&
                       (!filters.value.endDate || role.createdAt <= filters.value.endDate)
    const matchesSearch = !filters.value.search ||
      role.name.toLowerCase().includes(filters.value.search.toLowerCase()) ||
      role.description.toLowerCase().includes(filters.value.search.toLowerCase())
    return matchesType && matchesStatus && matchesDate && matchesSearch
  })
})

const totalRoles = computed(() => {
  return roles.value.length
})

const systemRoles = computed(() => {
  return roles.value.filter(role => role.type === 'system').length
})

const customRoles = computed(() => {
  return roles.value.filter(role => role.type === 'custom').length
})

const activeRoles = computed(() => {
  return roles.value.filter(role => role.status === 'active').length
})

// 获取角色类型名称
const getTypeName = (type) => {
  const names = {
    system: '系统角色',
    custom: '自定义角色'
  }
  return names[type] || type
}

// 获取角色类型样式
const getTypeClass = (type) => {
  const classes = {
    system: 'bg-purple-100 text-purple-800 dark:bg-purple-700 dark:text-purple-300',
    custom: 'bg-blue-100 text-blue-800 dark:bg-blue-700 dark:text-blue-300'
  }
  return classes[type] || classes.custom
}

// 获取权限名称
const getPermissionName = (permissionId) => {
  const permission = permissions.value.find(p => p.id === permissionId)
  return permission ? permission.name : '未知权限'
}

// 获取状态名称
const getStatusName = (status) => {
  const names = {
    active: '启用',
    inactive: '禁用'
  }
  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-red-100 text-red-800 dark:bg-red-700 dark:text-red-300'
  }
  return classes[status] || classes.active
}

// 添加角色
const addRole = () => {
  const role = {
    id: roles.value.length + 1,
    createdAt: new Date().toISOString(),
    ...newRole.value
  }
  
  roles.value.push(role)
  showAddRoleModal.value = false
  resetNewRole()
}

// 重置新角色表单
const resetNewRole = () => {
  newRole.value = {
    name: '',
    description: '',
    type: 'custom',
    permissions: [],
    status: 'active'
  }
}

// 查看角色
const viewRole = (role) => {
  // 实现查看角色的逻辑
  console.log('查看角色:', role)
}

// 编辑角色
const editRole = (role) => {
  // 实现编辑角色的逻辑
  console.log('编辑角色:', role)
}

// 删除角色
const deleteRole = (role) => {
  if (confirm(`确定要删除角色 ${role.name} 吗？`)) {
    const index = roles.value.findIndex(r => r.id === role.id)
    if (index !== -1) {
      roles.value.splice(index, 1)
    }
  }
}

// 导出角色数据
const exportRoleData = () => {
  // 实现导出角色数据的逻辑
  console.log('导出角色数据')
}
</script> 