import { useShareAppMessage } from '@tarojs/taro';

import { Tabs } from '@taroify/core';
import { useState } from 'react';

import PartnerIntro from '@/components/partner-intro';
import PartnerInviteList from '@/components/partner-invite-list';
import PartnerProfit from '@/components/partner-profit';
import { PageUrl } from '@/constants/app';
import useInviteCode from '@/hooks/use-invite-code';
import { generateMembershipCoupon } from '@/utils/coupon';
import { getCommonShareMessage } from '@/utils/share';
import './index.less';

const PREFIX = 'partner';

export default function Partner() {
  const inviteCode = useInviteCode();
  const [tab, setTab] = useState<number>(0);
  const handleChange = v => {
    setTab(v);
  };
  useShareAppMessage(async () => {
    console.log('Partner inviteCode', inviteCode);
    const { code } = await generateMembershipCoupon();
    return getCommonShareMessage({
      useCapture: false,
      inviteCode,
      title: '宝子，送你个播络会员，找工作更方便',
      path: PageUrl.GiveVip,
      params: { d: code },
      imageUrl: 'https://publiccdn.neighbourhood.com.cn/img/share-coupon1.png',
    });
  });

  return (
    <div className={PREFIX}>
      <Tabs className={`${PREFIX}__tabs`} value={tab} onChange={handleChange}>
        <Tabs.TabPane value={0} title="简介">
          <PartnerIntro />
        </Tabs.TabPane>
        <Tabs.TabPane value={1} title="邀请名单">
          <PartnerInviteList refreshDisabled={tab !== 1} visible={tab === 1} />
        </Tabs.TabPane>
        <Tabs.TabPane value={2} title="我的收益">
          <PartnerProfit />
        </Tabs.TabPane>
      </Tabs>
    </div>
  );
}
