<template>
  <div class="users-page">
    <el-card class="search-card">
      <el-form :inline="true" :model="searchForm">
        <el-form-item label="用户名">
          <el-input v-model="searchForm.username" placeholder="请输入用户名" clearable />
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="searchForm.role" placeholder="请选择角色" clearable>
            <el-option label="管理员" value="admin" />
            <el-option label="员工" value="staff" />
            <el-option label="只读" value="readonly" />
          </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>
    </el-card>

    <div class="toolbar">
      <el-button type="primary" @click="handleAdd">+ 添加用户</el-button>
    </div>

    <el-card>
      <el-table :data="tableData" border stripe>
        <el-table-column prop="username" label="用户名" width="120" />
        <el-table-column prop="name" label="姓名" width="100" />
        <el-table-column prop="role" label="角色" width="100">
          <template #default="{ row }">
            <el-tag :type="getRoleType(row.role)">{{ getRoleName(row.role) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="手机号" width="130" />
        <el-table-column prop="email" label="邮箱" />
        <el-table-column prop="status" label="状态" width="80">
          <template #default="{ row }">
            <el-tag :type="row.status === '正常' ? 'success' : 'danger'">{{ row.status }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="lastLogin" label="最后登录" width="120">
          <template #default="{ row }">{{ row.lastLogin || '-' }}</template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="150">
          <template #default="{ row }">
            <el-button size="small" type="primary" @click="handleEdit(row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加/编辑弹窗 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="450px">
      <el-form :model="form" :rules="rules" ref="formRef" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" placeholder="请输入密码" show-password />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select v-model="form.role" placeholder="请选择角色">
            <el-option label="管理员" value="admin" />
            <el-option label="员工" value="staff" />
            <el-option label="只读" value="readonly" />
          </el-select>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio label="正常">正常</el-radio>
            <el-radio label="禁用">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const searchForm = ref({ username: '', role: '' })
const tableData = ref([
  { _id: '1', username: 'admin', name: '管理员', role: 'admin', phone: '13800138000', email: 'admin@example.com', status: '正常', lastLogin: '2026-03-06' },
  { _id: '2', username: 'staff1', name: '张三', role: 'staff', phone: '13800138001', email: 'zhangsan@example.com', status: '正常', lastLogin: '2026-03-05' },
  { _id: '3', username: 'readonly1', name: '李四', role: 'readonly', phone: '13800138002', email: 'lisi@example.com', status: '正常', lastLogin: null }
])
const dialogVisible = ref(false)
const dialogTitle = ref('添加用户')
const formRef = ref(null)
const form = ref({ _id: '', username: '', password: '', name: '', role: 'staff', phone: '', email: '', status: '正常' })
const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  name: [{ required: true, message: '请输入姓名', trigger: 'blur' }]
}

const handleSearch = () => {
  let data = tableData.value
  if (searchForm.value.username) data = data.filter(u => u.username.includes(searchForm.value.username))
  if (searchForm.value.role) data = data.filter(u => u.role === searchForm.value.role)
  tableData.value = data
}
const handleReset = () => { searchForm.value = { username: '', role: '' }; tableData.value = [
  { _id: '1', username: 'admin', name: '管理员', role: 'admin', phone: '13800138000', email: 'admin@example.com', status: '正常', lastLogin: '2026-03-06' },
  { _id: '2', username: 'staff1', name: '张三', role: 'staff', phone: '13800138001', email: 'zhangsan@example.com', status: '正常', lastLogin: '2026-03-05' },
  { _id: '3', username: 'readonly1', name: '李四', role: 'readonly', phone: '13800138002', email: 'lisi@example.com', status: '正常', lastLogin: null }
]}
const handleAdd = () => { dialogTitle.value = '添加用户'; form.value = { _id: '', username: '', password: '', name: '', role: 'staff', phone: '', email: '', status: '正常' }; dialogVisible.value = true }
const handleEdit = (row) => { dialogTitle.value = '编辑用户'; form.value = { ...row, password: '******' }; dialogVisible.value = true }
const handleSubmit = () => { formRef.value.validate((valid) => { if (valid) { ElMessage.success(form.value._id ? '修改成功' : '添加成功'); dialogVisible.value = false } }) }
const handleDelete = async (row) => { await ElMessageBox.confirm('确定要删除这个用户吗?', '提示', { type: 'warning' }); ElMessage.success('删除成功') }
const getRoleType = (role) => ({ admin: 'danger', staff: 'primary', readonly: 'info' }[role] || 'info')
const getRoleName = (role) => ({ admin: '管理员', staff: '员工', readonly: '只读' }[role] || role)
</script>

<style scoped>
.search-card { margin-bottom: 20px; }
.toolbar { margin-bottom: 20px; display: flex; gap: 10px; }
</style>
