<template>
  <div class="permissions-admin">
    <div class="page-header">
      <h2>权限管理</h2>
    </div>

    <div class="search-section">
      <div class="search-form">
        <el-form :inline="true" :model="searchForm" class="search-form-inline">
          <el-form-item label="角色名称">
            <el-input
              v-model="searchForm.roleName"
              placeholder="请输入角色名称"
              clearable
              style="width: 150px"
            />
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="searchForm.status" placeholder="全部" clearable style="width: 120px">
              <el-option label="全部" value="" />
              <el-option label="启用" value="启用" />
              <el-option label="禁用" value="禁用" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">搜索</el-button>
            <el-button @click="handleReset">重置搜索</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="action-buttons">
        <el-button type="primary" @click="handleAdd">新增角色</el-button>
      </div>
    </div>

    <div class="table-section">
      <el-table :data="tableData" style="width: 100%" stripe border v-loading="loading">
        <el-table-column prop="roleName" label="角色名称" width="150" align="center" />
        <el-table-column prop="roleCode" label="角色编码" width="150" align="center" />
        <el-table-column prop="description" label="角色描述" min-width="200" />
        <el-table-column prop="permissions" label="权限" min-width="300">
          <template #default="scope">
            <el-tag 
              v-for="permission in scope.row.permissions" 
              :key="permission" 
              size="small" 
              style="margin-right: 8px; margin-bottom: 4px;"
            >
              {{ permission }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100" align="center">
          <template #default="scope">
            <el-tag :type="scope.row.status === '启用' ? 'success' : 'danger'">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="更新时间" width="180" align="center" />
        <el-table-column label="操作" width="200" align="center" fixed="right">
          <template #default="scope">
            <el-button type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="warning" size="small" @click="handlePermissions(scope.row)">权限</el-button>
            <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="pagination-section">
      <el-pagination
        v-model:current-page="pagination.currentPage"
        v-model:page-size="pagination.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="pagination.total"
        layout="total, sizes, prev, pager, next, jumper"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

defineOptions({
  name: 'PermissionsAdmin'
})

interface RoleItem {
  id: string
  roleName: string
  roleCode: string
  description: string
  permissions: string[]
  status: string
  updateTime: string
}

const searchForm = reactive({
  roleName: '',
  status: ''
})

const tableData = ref<RoleItem[]>([])
const loading = ref(false)

const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
})

const mockData: RoleItem[] = [
  {
    id: '1',
    roleName: '超级管理员',
    roleCode: 'super_admin',
    description: '拥有系统所有权限',
    permissions: ['用户管理', '内容管理', '系统设置', '数据统计'],
    status: '启用',
    updateTime: '2024-10-13 10:00:00'
  },
  {
    id: '2',
    roleName: '内容编辑',
    roleCode: 'editor',
    description: '负责内容的编辑和发布',
    permissions: ['新闻政策', '科技问答', '科技资源'],
    status: '启用',
    updateTime: '2024-10-12 15:30:00'
  },
  {
    id: '3',
    roleName: '普通用户',
    roleCode: 'user',
    description: '只能查看内容',
    permissions: ['内容查看'],
    status: '启用',
    updateTime: '2024-10-11 09:15:00'
  }
]

const fetchData = () => {
  loading.value = true
  setTimeout(() => {
    tableData.value = mockData
    pagination.total = mockData.length
    loading.value = false
  }, 500)
}

const handleSearch = () => {
  fetchData()
}

const handleReset = () => {
  searchForm.roleName = ''
  searchForm.status = ''
  fetchData()
}

const handleAdd = () => {
  ElMessage.info('新增角色功能开发中')
}

const handleEdit = (row: RoleItem) => {
  ElMessage.info('编辑功能开发中')
  console.log(row)
}

const handlePermissions = (row: RoleItem) => {
  ElMessage.info('权限配置功能开发中')
  console.log(row)
}

const handleDelete = (row: RoleItem) => {
  ElMessageBox.confirm(
    `确定要删除角色"${row.roleName}"吗？删除后无法恢复！`,
    '删除确认',
    {
      confirmButtonText: '确定删除',
      cancelButtonText: '取消',
      type: 'warning',
      dangerouslyUseHTMLString: false
    }
  ).then(() => {
    // 模拟删除请求
    const index = tableData.value.findIndex(item => item.id === row.id)
    if (index > -1) {
      tableData.value.splice(index, 1)
      pagination.total = tableData.value.length
      
      // 如果当前页没有数据了，回到上一页
      if (tableData.value.length === 0 && pagination.currentPage > 1) {
        pagination.currentPage--
      }
      
      ElMessage.success('删除成功')
    }
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}

const handleSizeChange = (size: number) => {
  pagination.pageSize = size
  fetchData()
}

const handleCurrentChange = (page: number) => {
  pagination.currentPage = page
  fetchData()
}

onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.permissions-admin {
  background: #ffffff;
  border-radius: 8px;
  padding: 20px;
}

.page-header {
  margin-bottom: 20px;
}

.page-header h2 {
  color: #303133;
  font-size: 24px;
  font-weight: 600;
  margin: 0;
}

.search-section {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 20px;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 8px;
}

.search-form-inline {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}

.table-section {
  margin-bottom: 20px;
}

.pagination-section {
  display: flex;
  justify-content: flex-end;
}
</style>