'use client';

import {
  AffiliateDialog,
  AffiliateDialogRef,
} from '@/components/affiliate/components/AffiliateDialog';
import { Display } from '@/components/display';
import { Empty } from '@/components/empty';
import useGlobalStore from '@/config/use-global';
import { queryUserAffiliate, queryUserAffiliateList } from '@/services/user/user';
import { useQuery } from '@tanstack/react-query';
import { Button } from '@workspace/ui/components/button';
import { Card, CardContent } from '@workspace/ui/components/card';
import { formatDate } from '@workspace/ui/utils';
import { Copy } from 'lucide-react';
import { useTranslations } from 'next-intl';
import { useRef, useState } from 'react';
import { CopyToClipboard } from 'react-copy-to-clipboard';
import { toast } from 'sonner';

export default function Affiliate() {
  const t = useTranslations('affiliate');
  const { user, common } = useGlobalStore();
  const [sum, setSum] = useState<number>();
  const { data } = useQuery({
    queryKey: ['queryUserAffiliate'],
    queryFn: async () => {
      const response = await queryUserAffiliate();
      return response.data.data;
    },
  });

  const { data: inviteList = [] } = useQuery({
    queryKey: ['queryUserAffiliateList'],
    queryFn: async () => {
      const response = await queryUserAffiliateList({
        page: 1,
        size: 3,
      });
      return response.data.data?.list || [];
    },
  });
  const dialogRef = useRef<AffiliateDialogRef>(null);
  return (
    <div className='grid grid-cols-1 gap-4 sm:grid-cols-2'>
      <Card
        className={
          'rounded-[20px] border border-[#D9D9D9] p-6 shadow-[0px_0px_52.6px_1px_rgba(15,44,83,0.05)]'
        }
      >
        <CardContent className={'p-0 text-[#666]'}>
          <div className={'sm:mb-6'}>
            <div className={'font-bold sm:text-xl'}>{t('totalCommission')}</div>
            <div className={'text-xs font-light sm:text-[15px]'}>
              佣金金额，邀请成功后自动转入钱包余额
            </div>
          </div>
          <div className={'mb-3 text-xl font-bold text-[#091B33] sm:text-[32px]'}>
            历史推荐用户：7
          </div>
          <div className={'grid grid-cols-2 gap-[10px] sm:gap-5'}>
            <div className='rounded-[20px] bg-[#EAEAEA] px-4 py-2 shadow-sm transition-all duration-300 hover:shadow-md sm:py-4'>
              <p className='font-medium text-[#666] opacity-80 sm:mb-3 sm:text-sm'>佣金总额</p>
              <p className='text-xl font-bold text-[#225BA9] sm:text-2xl'>
                <Display type='currency' value={data?.total_commission} />
              </p>
            </div>
            <div className='rounded-[20px] border-2 border-[#D9D9D9] px-4 py-2 shadow-sm transition-all duration-300 hover:shadow-md sm:py-4'>
              <p className='flex justify-between font-medium text-[#666] opacity-80 sm:mb-3 sm:text-sm'>
                <span className={'flex'}>
                  返佣邀请码
                  <CopyToClipboard
                    text={`${location?.origin}/?invite=${user?.refer_code}`}
                    onCopy={(text, result) => {
                      if (result) {
                        toast.success(t('copySuccess'));
                      }
                    }}
                  >
                    <Button variant='secondary' size='sm' className='px-0 sm:hidden'>
                      <Copy className='h-4 w-4' />
                    </Button>
                  </CopyToClipboard>
                </span>
              </p>
              <p className='flex justify-between text-xl 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='hidden gap-2 sm:block'>
                    <Copy className='h-4 w-4' />
                  </Button>
                </CopyToClipboard>
              </p>
            </div>
          </div>
        </CardContent>
      </Card>
      <Card className='rounded-[20px] border border-[#EAEAEA] bg-gradient-to-b from-white to-[#EAEAEA] p-6'>
        <div className='mb-4 flex items-center justify-between'>
          <h3 className='font-medium text-[#666666] sm:text-xl'>邀请记录</h3>
          <span className='text-sm text-[#225BA9]' onClick={() => dialogRef.current.open()}>
            更多
          </span>
        </div>

        <div className='space-y-2 sm:space-y-4'>
          {inviteList?.length ? (
            <div className='relative space-y-3'>
              <div
                className={
                  'absolute bottom-0 left-0 right-0 h-[60px] bg-white/30 backdrop-blur-[1px]'
                }
              ></div>
              {inviteList?.map((invite) => {
                return (
                  <div className='flex flex-wrap justify-between gap-2 rounded-[20px] bg-white px-6 py-2 text-[10px] sm:text-base'>
                    <div>
                      <div className={'text-[#225BA9]'}>用户识别代码</div>
                      <div className={'font-bold text-[#091B33]'}>{invite.identifier}</div>
                    </div>
                    <div>
                      <div className={'text-[#225BA9]'}>时间</div>
                      <div className={'font-bold text-[#091B33]'}>
                        {formatDate(invite.registered_at)}
                      </div>
                    </div>
                  </div>
                );
              })}
            </div>
          ) : (
            <Empty />
          )}
        </div>
      </Card>
      <AffiliateDialog ref={dialogRef} />
    </div>
  );
}
