'use client';

import { Display } from '@/components/display';
import { ProList, ProListActions } from '@/components/pro-list';
import useGlobalStore from '@/config/use-global';
import { queryUserBalanceLog } from '@/services/user/user';
import { Card, CardContent } from '@workspace/ui/components/card';
import { useTranslations } from 'next-intl';
import { useRef } from 'react';

import { Empty } from '@/components/empty';
import Recharge from '@/components/subscribe/recharge';
import { Button } from '@workspace/ui/components/button';
import { formatDate } from '@workspace/ui/utils';
import { Copy } from 'lucide-react';
import Link from 'next/link';
import { CopyToClipboard } from 'react-copy-to-clipboard';
import { toast } from 'sonner';

export default function Page() {
  const t = useTranslations('wallet');
  const { user } = useGlobalStore();
  const ref = useRef<ProListActions>(null);
  const totalAssets = (user?.balance || 0) + (user?.commission || 0) + (user?.gift_amount || 0);
  return (
    <>
      <Card className='rounded-[40px] border border-[#D9D9D9] px-[24px] pb-[30px] pt-[20px] shadow-[0px_0px_52.6px_1px_rgba(15,44,83,0.05)] sm:mb-4 sm:px-[30px]'>
        <CardContent className='p-0'>
          <h2 className='mb-4 flex items-center justify-between text-base font-bold text-[#666] sm:text-xl'>
            <span>{t('assetOverview')}</span>
            <Recharge
              className={
                'rounded-full border-[#A8D4ED] bg-[#A8D4ED] px-[35px] py-[9px] text-center text-base font-bold hover:bg-[#225BA9] hover:text-white sm:text-xl'
              }
            />
          </h2>
          <div className='mb-4'>
            <div className='flex items-center justify-between'>
              <div>
                <p className='text-sm font-light text-[#666]'>总资产</p>
                <p className='text-2xl font-bold sm:text-[32px]'>
                  <Display type='currency' value={totalAssets} />
                </p>
              </div>
            </div>
          </div>
          <div className='grid grid-cols-2 gap-2 sm:grid-cols-4 sm:gap-6'>
            <div className='col-span-2 rounded-[20px] bg-[#EAEAEA] p-4 shadow-sm transition-all duration-300 hover:shadow-md sm:col-span-1'>
              <p className='text-sm font-medium text-[#666] opacity-80 sm:mb-3'>账户余额</p>
              <p className='text-xl font-bold text-[#225BA9] sm:text-2xl'>
                <Display type='currency' value={user?.balance} />
              </p>
            </div>
            <div className='rounded-[20px] bg-[#EAEAEA] p-4 shadow-sm transition-all duration-300 hover:shadow-md'>
              <p className='t text-sm font-medium text-[#666] opacity-80 sm:mb-3'>
                {t('giftAmount')}
              </p>
              <p className='text-xl font-bold text-[#225BA9] sm:text-2xl'>
                <Display type='currency' value={user?.gift_amount} />
              </p>
            </div>
            <div className='rounded-[20px] bg-[#EAEAEA] p-4 shadow-sm transition-all duration-300 hover:shadow-md'>
              <p className='t text-sm font-medium text-[#666] opacity-80 sm:mb-3'>
                {t('commission')}
              </p>
              <p className='text-xl font-bold text-[#225BA9] sm:text-2xl'>
                <Display type='currency' value={user?.commission} />
              </p>
            </div>
            <div className='col-span-2 rounded-[20px] border-2 border-[#D9D9D9] p-4 shadow-sm transition-all duration-300 hover:shadow-md sm:col-span-1'>
              <p className='mb-1 flex justify-between text-sm font-medium text-[#666] opacity-80 sm:mb-3'>
                <span>返佣邀请码</span>
                <Link href='/affiliate' className={'text-[#225BA9]'}>
                  返佣详情
                </Link>
              </p>
              <p className='flex justify-between text-base font-bold text-[#225BA9] sm:text-2xl'>
                <span> {user?.refer_code}</span>
                <CopyToClipboard
                  text={`${location?.origin}/?invite=${user?.refer_code}`}
                  onCopy={(text, result) => {
                    if (result) {
                      toast.success(t('copySuccess'));
                    }
                  }}
                >
                  <Button variant='secondary' size='sm' className='gap-2'>
                    <Copy className='h-4 w-4' />
                  </Button>
                </CopyToClipboard>
              </p>
            </div>
          </div>
        </CardContent>
      </Card>
      <ProList<API.UserBalanceLog, Record<string, unknown>>
        action={ref}
        request={async (pagination, filter) => {
          const response = await queryUserBalanceLog({ ...pagination, ...filter });
          return {
            list: response.data.data?.list || [],
            total: response.data.data?.total || 0,
          };
        }}
        renderItem={(item) => {
          return (
            <Card className='rounded-[32px] px-[20px] sm:px-[55px]'>
              <CardContent className='px-0 py-3 text-[10px] sm:p-3 sm:text-sm'>
                <ul className='grid grid-cols-4 gap-3 *:flex *:flex-col'>
                  <li className='font-semibold'>
                    <span className='text-[#225BA9]'>{t('createdAt')}</span>
                    <time>{formatDate(item.created_at)}</time>
                  </li>
                  <li className='font-semibold'>
                    <span className='text-[#225BA9]'>{t('type.0')}</span>
                    <span>{t(`type.${item.type}`)}</span>
                  </li>
                  <li className='font-semibold'>
                    <span className='text-[#225BA9]'>{t('amount')}</span>
                    <span>
                      <Display type='currency' value={item.amount} />
                    </span>
                  </li>

                  <li className='font-semibold'>
                    <span className='text-[#225BA9]'>{t('balance')}</span>
                    <span>
                      <Display type='currency' value={item.balance} />
                    </span>
                  </li>
                </ul>
              </CardContent>
            </Card>
          );
        }}
        empty={<Empty />}
      />
    </>
  );
}
