"use client"

import { useState, useEffect, Suspense } from "react"
import { Card, CardContent } from "@/components/ui/card"
import { Input } from "@/components/ui/input"
import { Button } from "@/components/ui/button"
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"
import { Badge } from "@/components/ui/badge"
import { useStore, type User } from "@/lib/store"
import { Search, UserPlus, Eye, Trash2 } from "lucide-react"

function UsersContent() {
  const { getAllUsers, deleteUser } = useStore()
  const [users, setUsers] = useState<User[]>([])
  const [searchTerm, setSearchTerm] = useState("")

  useEffect(() => {
    setUsers(getAllUsers())
  }, [getAllUsers])

  const filteredUsers = users.filter((u) => u.nickname.includes(searchTerm) || u.phone.includes(searchTerm))

  const handleDelete = (userId: string) => {
    if (confirm("确定要删除这个用户吗？")) {
      deleteUser(userId)
      setUsers(getAllUsers())
    }
  }

  return (
    <div className="p-8 max-w-7xl mx-auto">
      <div className="flex justify-between items-center mb-8">
        <div>
          <h2 className="text-2xl font-bold text-white">用户管理</h2>
          <p className="text-gray-400 mt-1">共 {users.length} 位注册用户</p>
        </div>
        <div className="flex items-center gap-4">
          <div className="relative">
            <Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-gray-500" />
            <Input
              type="text"
              placeholder="搜索用户..."
              className="pl-10 bg-[#0f2137] border-gray-700 text-white placeholder:text-gray-500 w-64"
              value={searchTerm}
              onChange={(e) => setSearchTerm(e.target.value)}
            />
          </div>
          <Button className="bg-[#38bdac] hover:bg-[#2da396] text-white">
            <UserPlus className="w-4 h-4 mr-2" />
            添加用户
          </Button>
        </div>
      </div>

      <Card className="bg-[#0f2137] border-gray-700/50 shadow-xl">
        <CardContent className="p-0">
          <Table>
            <TableHeader>
              <TableRow className="bg-[#0a1628] hover:bg-[#0a1628] border-gray-700">
                <TableHead className="text-gray-400">用户信息</TableHead>
                <TableHead className="text-gray-400">手机号</TableHead>
                <TableHead className="text-gray-400">购买状态</TableHead>
                <TableHead className="text-gray-400">分销收益</TableHead>
                <TableHead className="text-gray-400">注册时间</TableHead>
                <TableHead className="text-right text-gray-400">操作</TableHead>
              </TableRow>
            </TableHeader>
            <TableBody>
              {filteredUsers.map((user) => (
                <TableRow key={user.id} className="hover:bg-[#0a1628] border-gray-700/50">
                  <TableCell>
                    <div className="flex items-center gap-3">
                      <div className="w-10 h-10 rounded-full bg-[#38bdac]/20 flex items-center justify-center text-sm font-medium text-[#38bdac]">
                        {user.nickname.charAt(0)}
                      </div>
                      <div>
                        <p className="font-medium text-white">{user.nickname}</p>
                        <p className="text-xs text-gray-500">ID: {user.id.slice(0, 8)}</p>
                      </div>
                    </div>
                  </TableCell>
                  <TableCell className="text-gray-300">{user.phone}</TableCell>
                  <TableCell>
                    {user.hasFullBook ? (
                      <Badge className="bg-green-500/20 text-green-400 hover:bg-green-500/20 border-0">全书已购</Badge>
                    ) : user.purchasedSections.length > 0 ? (
                      <Badge className="bg-blue-500/20 text-blue-400 hover:bg-blue-500/20 border-0">
                        已购 {user.purchasedSections.length} 节
                      </Badge>
                    ) : (
                      <Badge variant="outline" className="text-gray-500 border-gray-600">
                        未购买
                      </Badge>
                    )}
                  </TableCell>
                  <TableCell className="text-white font-medium">¥{user.earnings?.toFixed(2) || "0.00"}</TableCell>
                  <TableCell className="text-gray-400">{new Date(user.createdAt).toLocaleDateString()}</TableCell>
                  <TableCell className="text-right">
                    <div className="flex items-center justify-end gap-2">
                      <Button variant="ghost" size="sm" className="text-gray-400 hover:text-white hover:bg-gray-700/50">
                        <Eye className="w-4 h-4" />
                      </Button>
                      <Button
                        variant="ghost"
                        size="sm"
                        className="text-red-400 hover:text-red-300 hover:bg-red-500/10"
                        onClick={() => handleDelete(user.id)}
                      >
                        <Trash2 className="w-4 h-4" />
                      </Button>
                    </div>
                  </TableCell>
                </TableRow>
              ))}
              {filteredUsers.length === 0 && (
                <TableRow>
                  <TableCell colSpan={6} className="text-center py-12 text-gray-500">
                    暂无用户数据
                  </TableCell>
                </TableRow>
              )}
            </TableBody>
          </Table>
        </CardContent>
      </Card>
    </div>
  )
}

export default function UsersPage() {
  return (
    <Suspense fallback={null}>
      <UsersContent />
    </Suspense>
  )
}
