<script setup lang="ts">
import { Loader2, Users } from 'lucide-vue-next'
import { onMounted, ref } from 'vue'

import api from '@/services/api'

interface User {
  id: number
  username: string
  email: string
  status: string
  created_at: string
  last_login_at: string | null
}

const loading = ref(true)
const users = ref<User[]>([])

onMounted(async () => {
  try {
    const data = await api.get<User[]>('/agent/users')
    users.value = data || []
  }
  catch (error) {
    console.error('获取用户列表失败:', error)
  }
  finally {
    loading.value = false
  }
})

function formatDate(dateStr: string | null) {
  if (!dateStr)
    return '-'
  return new Date(dateStr).toLocaleDateString('zh-CN')
}
</script>

<template>
  <div class="space-y-6">
    <div>
      <h1 class="text-2xl font-bold">
        用户管理
      </h1>
      <p class="text-muted-foreground">
        管理您应用下的用户
      </p>
    </div>

    <UiCard>
      <UiCardContent class="p-0">
        <div v-if="loading" class="flex items-center justify-center py-12">
          <Loader2 class="size-8 animate-spin text-muted-foreground" />
        </div>

        <div v-else-if="users.length === 0" class="text-center py-12 text-muted-foreground">
          <Users class="size-16 mx-auto mb-4 opacity-30" />
          <p>暂无用户记录</p>
        </div>

        <div v-else class="overflow-x-auto">
          <table class="w-full">
            <thead class="bg-muted/50">
              <tr>
                <th class="px-4 py-3 text-left text-sm font-medium">
                  用户名
                </th>
                <th class="px-4 py-3 text-left text-sm font-medium">
                  邮箱
                </th>
                <th class="px-4 py-3 text-left text-sm font-medium">
                  状态
                </th>
                <th class="px-4 py-3 text-left text-sm font-medium">
                  注册时间
                </th>
                <th class="px-4 py-3 text-left text-sm font-medium">
                  最后登录
                </th>
              </tr>
            </thead>
            <tbody class="divide-y">
              <tr v-for="user in users" :key="user.id" class="hover:bg-muted/30">
                <td class="px-4 py-3 font-medium">
                  {{ user.username }}
                </td>
                <td class="px-4 py-3 text-sm text-muted-foreground">
                  {{ user.email || '-' }}
                </td>
                <td class="px-4 py-3">
                  <UiBadge
                    :class="user.status === 'active' ? 'bg-green-500/10 text-green-500' : ''"
                    variant="outline"
                  >
                    {{ user.status === 'active' ? '正常' : user.status }}
                  </UiBadge>
                </td>
                <td class="px-4 py-3 text-sm text-muted-foreground">
                  {{ formatDate(user.created_at) }}
                </td>
                <td class="px-4 py-3 text-sm text-muted-foreground">
                  {{ formatDate(user.last_login_at) }}
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </UiCardContent>
    </UiCard>
  </div>
</template>
