import Image from 'next/image'
import {Card, CardContent} from '@/components/ui/card'
import {getProfile} from '@/actions/auth'
import {format} from 'date-fns'
import {CheckCircleIcon, CircleAlertIcon} from 'lucide-react'
import {Button, buttonVariants} from '@/components/ui/button'
import RechargeModal from '@/components/composites/recharge'
import {merge} from '@/lib/utils'
import Link from 'next/link'
import actionBill from '../_assets/action-bill.webp'
import actionBuy from '../_assets/action-buy.webp'
import actionLogout from '../_assets/action-logout.webp'

async function UserCenter() {
  const resp = await getProfile()
  if (!resp.success) {
    return (
      <div className="col-start-4 row-start-1 row-span-2 flex justify-center items-center">
        获取用户数据失败
      </div>
    )
  }

  const profile = resp.data
  return (
    <Card className="h-full">
      <CardContent className="flex-auto flex flex-col justify-between gap-4">
        <div className="flex flex-col gap-1">
          <p>{profile.username?.trim() || profile.email || profile.phone}</p>
          <p className="text-sm text-weak">{`最后登录：${format(profile.last_login, 'yyyy-MM-dd HH:mm')}`}</p>
        </div>
        <div className={merge(
          `flex justify-between p-2 rounded-md`,
          profile.id_token ? `bg-done-muted` : `bg-warn-muted`,
        )}
        >
          {profile.id_token
            ? (
              <>
                <div className="flex gap-2 items-center ">
                  <CheckCircleIcon size={20} className="text-done"/>
                  <span>已实名</span>
                </div>
                <div className="flex flex-col items-end">
                  <span className="text-sm">{profile.name}</span>
                  <span className="text-xs text-weak">{profile.id_no}</span>
                </div>
              </>
            )
            : (
              <>
                <span className="flex gap-2 items-center">
                  <CircleAlertIcon className="text-warn"/>
                  <span>未实名</span>
                </span>
                <Link href="/admin/identify">
                  <Button className="h-9">去实名</Button>
                </Link>
              </>
            )}
        </div>
        <div className="flex flex-col gap-1">
          <h4 className="text-sm text-weak">账户余额</h4>
          <div className="flex justify-between items-baseline">
            <p className="text-xl text-accent">
              ￥
              {profile.balance}
            </p>
            <RechargeModal/>
          </div>
        </div>
        <div className="flex flex-col gap-3">
          <h4 className="text-sm text-weak">快捷入口</h4>
          <div className="flex justify-around gap-2">
            <Link href="/admin/bills" className={merge(buttonVariants({theme: `ghost`}), `flex flex-col gap-2 py-2 px-3 h-auto`)}>
              <Image alt="bill icon" src={actionBill} height={48}/>
              <span className="text-sm text-weak">我的帐单</span>
            </Link>
            <Link href="/admin/purchase" className={merge(buttonVariants({theme: `ghost`}), `flex flex-col gap-2 py-2 px-3 h-auto`)}>
              <Image alt="buy icon" src={actionBuy} height={48}/>
              <span className="text-sm text-weak">购买产品</span>
            </Link>
            <Link href="/admin/profile" className={merge(buttonVariants({theme: `ghost`}), `flex flex-col gap-2 py-2 px-3 h-auto`)}>
              <Image alt="logout icon" src={actionLogout} height={48}/>
              <span className="text-sm text-weak">个人中心</span>
            </Link>
          </div>
        </div>
      </CardContent>
    </Card>
  )
}
export default UserCenter
