<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { getUsers } from '@/api/modules/system'
import type { SystemUser } from '@/types'

const loading = ref(false)
const users = ref<SystemUser[]>([])

async function loadUsers() {
  loading.value = true
  try {
    users.value = await getUsers()
  } finally {
    loading.value = false
  }
}

onMounted(loadUsers)
</script>

<template>
  <section class="user-settings" v-loading="loading">
    <header class="page-header">
      <div>
        <h1>用户设置</h1>
        <p>管理平台账号、角色状态和页面权限。</p>
      </div>
      <el-button type="primary" @click="$router.push('/user-settings/create')">创建用户</el-button>
    </header>

    <el-table :data="users">
      <el-table-column prop="username" label="账号" min-width="140" />
      <el-table-column prop="display_name" label="显示名称" min-width="160" />
      <el-table-column prop="role" label="角色" width="120" />
      <el-table-column prop="status" label="状态" width="120" />
      <el-table-column label="权限数" width="120">
        <template #default="{ row }">{{ row.permissions?.length || 0 }}</template>
      </el-table-column>
      <el-table-column prop="create_time" label="创建时间" min-width="180" />
    </el-table>
  </section>
</template>

<style scoped>
.user-settings {
  padding: 24px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}

.page-header h1 {
  margin: 0;
  font-size: 24px;
}

.page-header p {
  margin: 8px 0 0;
  color: #64748b;
}
</style>
