<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="showAddPermissionModal = true" class="bg-blue-600 text-white px-4 py-2 rounded-md hover:bg-blue-700">
              添加权限
            </button>
            <button @click="showAddGroupModal = true" class="bg-green-600 text-white px-4 py-2 rounded-md hover:bg-green-700">
              创建权限组
            </button>
            <button @click="exportPermissionData" 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">{{ totalPermissions }}</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">{{ totalGroups }}</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">{{ systemPermissions }}</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">{{ customPermissions }}</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.group" 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="group in permissionGroups" :key="group.id" :value="group.id">{{ group.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>
            </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="permission in filteredPermissions" :key="permission.id">
              <td class="px-6 py-4">
                <div class="text-sm font-medium text-gray-900 dark:text-white">{{ permission.name }}</div>
                <div class="text-sm text-gray-500 dark:text-gray-400">{{ permission.description }}</div>
              </td>
              <td class="px-6 py-4 whitespace-nowrap">
                <span :class="getTypeClass(permission.type)" class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full">
                  {{ getTypeName(permission.type) }}
                </span>
              </td>
              <td class="px-6 py-4">
                <span class="px-2 py-1 text-xs rounded-full bg-blue-100 text-blue-800 dark:bg-blue-700 dark:text-blue-300">
                  {{ getGroupName(permission.groupId) }}
                </span>
              </td>
              <td class="px-6 py-4 whitespace-nowrap">
                <span :class="getStatusClass(permission.status)" class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full">
                  {{ getStatusName(permission.status) }}
                </span>
              </td>
              <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                <button @click="viewPermission(permission)" class="text-blue-600 hover:text-blue-900 dark:text-blue-400 dark:hover:text-blue-300 mr-3">查看</button>
                <button @click="editPermission(permission)" class="text-green-600 hover:text-green-900 dark:text-green-400 dark:hover:text-green-300 mr-3">编辑</button>
                <button @click="deletePermission(permission)" 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="showAddPermissionModal" 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="addPermission">
          <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="newPermission.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="newPermission.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="newPermission.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>
            <select v-model="newPermission.groupId" 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="group in permissionGroups" :key="group.id" :value="group.id">{{ group.name }}</option>
            </select>
          </div>
          <div class="flex justify-end space-x-3">
            <button type="button" @click="showAddPermissionModal = 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 v-if="showAddGroupModal" 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="addGroup">
          <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="newGroup.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="newGroup.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="flex justify-end space-x-3">
            <button type="button" @click="showAddGroupModal = 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 permissions = ref([
  {
    id: 1,
    name: '系统管理',
    description: '系统基础管理权限',
    type: 'system',
    groupId: 1,
    status: 'active',
    createdAt: '2024-01-01T00:00:00'
  },
  {
    id: 2,
    name: '用户管理',
    description: '用户信息管理权限',
    type: 'system',
    groupId: 1,
    status: 'active',
    createdAt: '2024-01-01T00:00:00'
  },
  {
    id: 3,
    name: '角色管理',
    description: '角色配置管理权限',
    type: 'system',
    groupId: 1,
    status: 'active',
    createdAt: '2024-01-01T00:00:00'
  },
  {
    id: 4,
    name: '项目管理',
    description: '项目相关操作权限',
    type: 'custom',
    groupId: 2,
    status: 'active',
    createdAt: '2024-03-01T00:00:00'
  }
])

// 权限组数据
const permissionGroups = ref([
  {
    id: 1,
    name: '系统权限组',
    description: '系统基础功能权限组',
    createdAt: '2024-01-01T00:00:00'
  },
  {
    id: 2,
    name: '业务权限组',
    description: '业务功能相关权限组',
    createdAt: '2024-01-01T00:00:00'
  }
])

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

// 模态框状态
const showAddPermissionModal = ref(false)
const showAddGroupModal = ref(false)

// 新权限表单
const newPermission = ref({
  name: '',
  description: '',
  type: 'custom',
  groupId: '',
  status: 'active'
})

// 新权限组表单
const newGroup = ref({
  name: '',
  description: ''
})

// 计算属性
const filteredPermissions = computed(() => {
  return permissions.value.filter(permission => {
    const matchesType = !filters.value.type || permission.type === filters.value.type
    const matchesGroup = !filters.value.group || permission.groupId === filters.value.group
    const matchesStatus = !filters.value.status || permission.status === filters.value.status
    const matchesSearch = !filters.value.search ||
      permission.name.toLowerCase().includes(filters.value.search.toLowerCase()) ||
      permission.description.toLowerCase().includes(filters.value.search.toLowerCase())
    return matchesType && matchesGroup && matchesStatus && matchesSearch
  })
})

const totalPermissions = computed(() => {
  return permissions.value.length
})

const totalGroups = computed(() => {
  return permissionGroups.value.length
})

const systemPermissions = computed(() => {
  return permissions.value.filter(p => p.type === 'system').length
})

const customPermissions = computed(() => {
  return permissions.value.filter(p => p.type === 'custom').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 getGroupName = (groupId) => {
  const group = permissionGroups.value.find(g => g.id === groupId)
  return group ? group.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 addPermission = () => {
  const permission = {
    id: permissions.value.length + 1,
    createdAt: new Date().toISOString(),
    ...newPermission.value
  }
  
  permissions.value.push(permission)
  showAddPermissionModal.value = false
  resetNewPermission()
}

// 重置新权限表单
const resetNewPermission = () => {
  newPermission.value = {
    name: '',
    description: '',
    type: 'custom',
    groupId: '',
    status: 'active'
  }
}

// 添加权限组
const addGroup = () => {
  const group = {
    id: permissionGroups.value.length + 1,
    createdAt: new Date().toISOString(),
    ...newGroup.value
  }
  
  permissionGroups.value.push(group)
  showAddGroupModal.value = false
  resetNewGroup()
}

// 重置新权限组表单
const resetNewGroup = () => {
  newGroup.value = {
    name: '',
    description: ''
  }
}

// 查看权限
const viewPermission = (permission) => {
  // 实现查看权限的逻辑
  console.log('查看权限:', permission)
}

// 编辑权限
const editPermission = (permission) => {
  // 实现编辑权限的逻辑
  console.log('编辑权限:', permission)
}

// 删除权限
const deletePermission = (permission) => {
  if (confirm(`确定要删除权限 ${permission.name} 吗？`)) {
    const index = permissions.value.findIndex(p => p.id === permission.id)
    if (index !== -1) {
      permissions.value.splice(index, 1)
    }
  }
}

// 导出权限数据
const exportPermissionData = () => {
  // 实现导出权限数据的逻辑
  console.log('导出权限数据')
}
</script> 