<template>
  <div>
    <h2>用户管理</h2>
    <el-form :model="form" inline>
      <el-form-item label="用户名">
        <el-input v-model="form.username" placeholder="用户名" />
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password" type="password" placeholder="密码" />
      </el-form-item>
      <el-form-item label="真实姓名">
        <el-input v-model="form.realName" placeholder="真实姓名" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleCreate">创建</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="users" border>
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="username" label="用户名" />
      <el-table-column prop="realName" label="真实姓名" />
      <el-table-column prop="roleCodes" label="角色" />
      <el-table-column prop="status" label="状态" />
    </el-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { createUser, listUsers } from '@/api/account'

const form = reactive({
  username: '',
  password: '',
  realName: '',
})

const users = ref<any[]>([])

async function loadUsers() {
  try {
    const res: any = await listUsers()
    users.value = res.data || []
  } catch (e: any) {
    ElMessage.error(e.message)
  }
}

async function handleCreate() {
  try {
    await createUser({ ...form, roleCodes: ['tenant_admin'] })
    ElMessage.success('创建成功')
    await loadUsers()
  } catch (e: any) {
    ElMessage.error(e.message)
  }
}

onMounted(loadUsers)
</script>
