<template>
  <div class="admin-users">
    <h2>用户管理</h2>
    <div class="users-actions">
      <el-button type="primary" @click="showCreateDialog">创建用户</el-button>
    </div>
    <el-table :data="users" style="width: 100%" class="users-table">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="username" label="用户名" />
      <el-table-column prop="email" label="邮箱" />
      <el-table-column prop="role" label="角色" width="120">
        <template #default="scope">
          <el-tag :type="getRoleType(scope.row.role)">{{ scope.row.role }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="created_at" label="创建时间" width="180">
        <template #default="scope">
          {{ formatDate(scope.row.created_at) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button size="small" type="primary" @click="showEditDialog(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="deleteUser(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 创建用户对话框 -->
    <el-dialog v-model="createDialogVisible" title="创建用户">
      <el-form :model="createForm" label-width="100px">
        <el-form-item label="用户名" required>
          <el-input v-model="createForm.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="邮箱" required>
          <el-input v-model="createForm.email" type="email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="密码" required>
          <el-input v-model="createForm.password" type="password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="角色" required>
          <el-select v-model="createForm.role" placeholder="请选择角色">
            <el-option label="管理员" value="admin" />
            <el-option label="用户" value="user" />
            <el-option label="访客" value="guest" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="createDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="createUser">确定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 编辑用户对话框 -->
    <el-dialog v-model="editDialogVisible" title="编辑用户">
      <el-form :model="editForm" label-width="100px">
        <el-form-item label="用户名" required>
          <el-input v-model="editForm.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="邮箱" required>
          <el-input v-model="editForm.email" type="email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="editForm.password" type="password" placeholder="请输入密码（留空表示不修改）" />
        </el-form-item>
        <el-form-item label="角色" required>
          <el-select v-model="editForm.role" placeholder="请选择角色">
            <el-option label="管理员" value="admin" />
            <el-option label="用户" value="user" />
            <el-option label="访客" value="guest" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="updateUser">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'

const users = ref<any[]>([
  {
    id: 1,
    username: 'admin',
    email: 'admin@example.com',
    role: 'admin',
    created_at: new Date().toISOString()
  },
  {
    id: 2,
    username: 'user1',
    email: 'user1@example.com',
    role: 'user',
    created_at: new Date().toISOString()
  }
])

const createDialogVisible = ref(false)
const editDialogVisible = ref(false)

const createForm = ref({
  username: '',
  email: '',
  password: '',
  role: 'user'
})

const editForm = ref({
  id: 0,
  username: '',
  email: '',
  password: '',
  role: 'user'
})

const formatDate = (dateString?: string) => {
  if (!dateString) return ''
  const date = new Date(dateString)
  return date.toLocaleString()
}

const getRoleType = (role?: string) => {
  switch (role) {
    case 'admin':
      return 'danger'
    case 'user':
      return 'primary'
    case 'guest':
      return 'info'
    default:
      return 'default'
  }
}

const showCreateDialog = () => {
  createForm.value = {
    username: '',
    email: '',
    password: '',
    role: 'user'
  }
  createDialogVisible.value = true
}

const showEditDialog = (user: any) => {
  editForm.value = {
    id: user.id,
    username: user.username,
    email: user.email,
    password: '',
    role: user.role
  }
  editDialogVisible.value = true
}

const createUser = async () => {
  // 这里应该调用后端API创建用户
  // 暂时只做前端模拟
  const newUser = {
    id: Date.now(),
    ...createForm.value,
    created_at: new Date().toISOString(),
    updated_at: new Date().toISOString()
  }
  users.value.push(newUser)
  createDialogVisible.value = false
}

const updateUser = async () => {
  // 这里应该调用后端API更新用户
  // 暂时只做前端模拟
  const index = users.value.findIndex(u => u.id === editForm.value.id)
  if (index !== -1) {
    const updateData = { ...editForm.value }
    if (!updateData.password) {
      delete updateData.password
    }
    users.value[index] = {
      ...users.value[index],
      ...updateData,
      updated_at: new Date().toISOString()
    }
  }
  editDialogVisible.value = false
}

const deleteUser = async (id: number) => {
  // 这里应该调用后端API删除用户
  // 暂时只做前端模拟
  users.value = users.value.filter(u => u.id !== id)
}
</script>

<style scoped>
.admin-users {
  padding: 20px;
}

.admin-users h2 {
  font-size: 20px;
  margin-bottom: 20px;
  color: #333;
}

.users-actions {
  margin-bottom: 20px;
}

.users-table {
  margin-top: 20px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

@media (max-width: 768px) {
  .admin-users {
    padding: 10px;
  }

  .users-actions {
    margin-bottom: 10px;
  }

  .users-table {
    margin-top: 10px;
  }
}
</style>
