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

interface User {
  id: number
  username: string
  is_active: boolean
  is_superuser: boolean
}

const users = ref<User[]>([])
const dialogVisible = ref(false)
const isEdit = ref(false)
const editId = ref<number | null>(null)

const form = ref({
  username: '',
  password: '',
  is_active: true,
  is_superuser: false
})

const fetchUsers = async () => {
  try {
    const res = await api.get('/users')
    users.value = res.data
  } catch (e) {
    ElMessage.error('获取用户列表失败')
  }
}

const resetForm = () => {
  form.value = { username: '', password: '', is_active: true, is_superuser: false }
  isEdit.value = false
  editId.value = null
}

const handleAdd = () => {
  resetForm()
  dialogVisible.value = true
}

const handleEdit = (row: User) => {
  isEdit.value = true
  editId.value = row.id
  form.value = {
    username: row.username,
    password: '', // Don't fill password
    is_active: row.is_active,
    is_superuser: row.is_superuser
  }
  dialogVisible.value = true
}

const handleSave = async () => {
  try {
    if (isEdit.value && editId.value) {
      // Remove password if empty to avoid resetting it
      const data: any = { ...form.value }
      if (!data.password) delete data.password
      
      await api.put(`/users/${editId.value}`, data)
      ElMessage.success('更新成功')
    } else {
      if (!form.value.username || !form.value.password) {
        ElMessage.warning('用户名和密码必填')
        return
      }
      await api.post('/users', form.value)
      ElMessage.success('添加成功')
    }
    dialogVisible.value = false
    fetchUsers()
  } catch (e: any) {
    const msg = e.response?.data?.detail || (isEdit.value ? '更新失败' : '添加失败')
    ElMessage.error(msg)
  }
}

const handleDelete = (id: number) => {
  ElMessageBox.confirm('确认删除该用户？', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async () => {
    try {
      await api.delete(`/users/${id}`)
      ElMessage.success('删除成功')
      fetchUsers()
    } catch (e: any) {
      const msg = e.response?.data?.detail || '删除失败'
      ElMessage.error(msg)
    }
  })
}

onMounted(fetchUsers)
</script>

<template>
  <div class="users-view">
    <div class="toolbar">
      <el-button type="primary" @click="handleAdd">添加用户</el-button>
    </div>
    <el-table :data="users" style="width: 100%">
      <el-table-column prop="id" label="ID" width="60" />
      <el-table-column prop="username" label="用户名" />
      <el-table-column label="状态">
        <template #default="scope">
          <el-tag :type="scope.row.is_active ? 'success' : 'danger'">
            {{ scope.row.is_active ? '启用' : '禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="角色">
        <template #default="scope">
          <el-tag :type="scope.row.is_superuser ? 'warning' : 'info'">
            {{ scope.row.is_superuser ? '管理员' : '普通用户' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogVisible" :title="isEdit ? '编辑用户' : '添加用户'">
      <el-form :model="form" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="form.username" :disabled="isEdit" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password" type="password" placeholder="不修改请留空" show-password />
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="form.is_active" active-text="启用" inactive-text="禁用" />
        </el-form-item>
        <el-form-item label="管理员">
          <el-switch v-model="form.is_superuser" active-text="是" inactive-text="否" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSave">保存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.toolbar {
  margin-bottom: 20px;
}
</style>

