<template>
  <view class="invite-page">
    <!-- 顶部标题 -->
    <view class="page-header">
      <text class="header-title">邀请好友</text>
      <text class="header-subtitle">分享赚钱 轻松躺赚</text>
    </view>

    <!-- 海报卡片 -->
    <view class="poster-card" id="poster">
      <!-- 顶部渐变区 -->
      <view class="poster-hero">
        <view class="hero-badge">
          <text class="badge-text">限时高返</text>
        </view>
        <text class="hero-title">邀请有礼</text>
        <text class="hero-subtitle">好友下单 立享收益</text>
      </view>

      <!-- 收益展示 -->
      <view class="earnings-wrapper">
        <view class="earnings-item">
          <text class="earnings-icon">🎁</text>
          <text class="earnings-percent">5%</text>
          <text class="earnings-label">首单返现</text>
        </view>
        <view class="earnings-divider"></view>
        <view class="earnings-item">
          <text class="earnings-icon">💰</text>
          <text class="earnings-percent">0.5%</text>
          <text class="earnings-label">持续收益</text>
        </view>
      </view>

      <!-- 二维码 -->
      <view class="qrcode-wrapper">
        <view class="qrcode-box">
          <text class="qrcode-placeholder">小程序码</text>
        </view>
        <text class="qrcode-hint">长按识别二维码</text>
      </view>

      <!-- 邀请码 -->
      <view class="code-wrapper">
        <text class="code-label">我的邀请码</text>
        <view class="code-display">
          <text class="code-text">{{ inviteCode }}</text>
        </view>
      </view>

      <!-- 说明 -->
      <view class="info-wrapper">
        <view class="info-item">
          <text class="info-icon">✓</text>
          <text class="info-text">好友注册下单即可获得返现</text>
        </view>
        <view class="info-item">
          <text class="info-icon">✓</text>
          <text class="info-text">收益实时到账 随时可提现</text>
        </view>
        <view class="info-item">
          <text class="info-icon">✓</text>
          <text class="info-text">邀请越多赚得越多 无上限</text>
        </view>
      </view>
    </view>

    <!-- 操作按钮 -->
    <view class="action-wrapper">
      <button class="action-btn btn-primary" @tap="savePoster">
        保存海报
      </button>
      <button class="action-btn btn-secondary" @tap="copyLink">
        复制邀请码
      </button>
    </view>

    <!-- 提示 -->
    <view class="tip-wrapper">
      <text class="tip-text">💡 分享给好友即可获得收益</text>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { getInviteStats } from '@/api/user/invite';

const inviteCode = ref('------');
const loading = ref(true);

onMounted(async () => {
  await loadInviteCode();
});

async function loadInviteCode() {
  try {
    loading.value = true;
    const res = await getInviteStats();
    inviteCode.value = res.data?.inviteCode || '------';
  } catch (error) {
    console.error('获取邀请码失败:', error);
    uni.showToast({
      title: '获取邀请码失败',
      icon: 'none',
    });
  } finally {
    loading.value = false;
  }
}

function savePoster() {
  uni.showToast({
    title: '请长按海报图片保存',
    icon: 'none',
    duration: 2000
  });
}

function copyLink() {
  if (!inviteCode.value || inviteCode.value === '------') {
    uni.showToast({ title: '邀请码加载中', icon: 'none' });
    return;
  }

  uni.setClipboardData({
    data: inviteCode.value,
    success: () => {
      uni.showToast({ title: '邀请码已复制', icon: 'success' });
    },
  });
}
</script>

<style lang="scss" scoped>
@import '@/static/styles/common.scss';

.invite-page {
  min-height: 100vh;
  background: linear-gradient(180deg, #f8fafc 0%, #eef2f6 100%);
  padding: 48rpx 32rpx 32rpx;
}

.page-header {
  text-align: center;
  margin-bottom: 40rpx;
}

.header-title {
  display: block;
  font-size: 52rpx;
  font-weight: 800;
  color: #1e293b;
  margin-bottom: 12rpx;
}

.header-subtitle {
  display: block;
  font-size: 28rpx;
  color: #64748b;
  font-weight: 500;
}

.poster-card {
  background: #ffffff;
  border-radius: 32rpx;
  overflow: hidden;
  box-shadow: 0 20rpx 60rpx rgba(0, 0, 0, 0.08);
  margin-bottom: 32rpx;
}

.poster-hero {
  background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
  padding: 56rpx 40rpx;
  text-align: center;
  position: relative;
  overflow: hidden;
}

.poster-hero::before {
  content: '';
  position: absolute;
  top: -50%;
  right: -20%;
  width: 400rpx;
  height: 400rpx;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.1) 0%, transparent 70%);
  border-radius: 50%;
}

.poster-hero::after {
  content: '';
  position: absolute;
  bottom: -30%;
  left: -10%;
  width: 300rpx;
  height: 300rpx;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.08) 0%, transparent 70%);
  border-radius: 50%;
}

.hero-badge {
  position: relative;
  z-index: 2;
  display: inline-block;
  padding: 10rpx 24rpx;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 100rpx;
  margin-bottom: 24rpx;
  backdrop-filter: blur(10rpx);
  border: 1rpx solid rgba(255, 255, 255, 0.3);
}

.badge-text {
  font-size: 24rpx;
  color: #ffffff;
  font-weight: 700;
  letter-spacing: 2rpx;
}

.hero-title {
  position: relative;
  z-index: 2;
  display: block;
  font-size: 68rpx;
  font-weight: 900;
  color: #ffffff;
  margin-bottom: 12rpx;
  letter-spacing: 2rpx;
}

.hero-subtitle {
  position: relative;
  z-index: 2;
  display: block;
  font-size: 30rpx;
  color: rgba(255, 255, 255, 0.95);
  font-weight: 600;
}

.earnings-wrapper {
  display: flex;
  align-items: center;
  padding: 48rpx 40rpx;
  background: #fafbfc;
}

.earnings-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16rpx;
}

.earnings-icon {
  font-size: 56rpx;
  margin-bottom: 8rpx;
}

.earnings-percent {
  font-size: 64rpx;
  font-weight: 900;
  color: #6366f1;
  line-height: 1;
}

.earnings-label {
  font-size: 26rpx;
  color: #64748b;
  font-weight: 600;
}

.earnings-divider {
  width: 2rpx;
  height: 100rpx;
  background: linear-gradient(180deg, transparent 0%, #e2e8f0 50%, transparent 100%);
}

.qrcode-wrapper {
  padding: 48rpx 40rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  background: #ffffff;
}

.qrcode-box {
  width: 280rpx;
  height: 280rpx;
  background: #f8fafc;
  border-radius: 24rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 20rpx;
  border: 3rpx dashed #cbd5e1;
  position: relative;
}

.qrcode-box::before,
.qrcode-box::after {
  content: '';
  position: absolute;
  background: #6366f1;
}

.qrcode-box::before {
  top: -6rpx;
  left: -6rpx;
  width: 40rpx;
  height: 6rpx;
  border-radius: 6rpx 0 0 0;
}

.qrcode-box::after {
  top: -6rpx;
  left: -6rpx;
  width: 6rpx;
  height: 40rpx;
  border-radius: 6rpx 0 0 0;
}

.qrcode-placeholder {
  font-size: 28rpx;
  color: #94a3b8;
  font-weight: 500;
}

.qrcode-hint {
  font-size: 26rpx;
  color: #64748b;
  font-weight: 500;
}

.code-wrapper {
  padding: 40rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20rpx;
  background: #fafbfc;
}

.code-label {
  font-size: 26rpx;
  color: #64748b;
  font-weight: 600;
}

.code-display {
  padding: 20rpx 48rpx;
  background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
  border-radius: 16rpx;
  box-shadow: 0 8rpx 24rpx rgba(99, 102, 241, 0.3);
}

.code-text {
  font-size: 44rpx;
  font-weight: 900;
  color: #ffffff;
  letter-spacing: 8rpx;
  font-family: 'Courier New', Courier, monospace;
}

.info-wrapper {
  padding: 40rpx;
  display: flex;
  flex-direction: column;
  gap: 24rpx;
  background: #ffffff;
}

.info-item {
  display: flex;
  align-items: flex-start;
  gap: 16rpx;
}

.info-icon {
  width: 36rpx;
  height: 36rpx;
  background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20rpx;
  color: #ffffff;
  font-weight: 900;
  flex-shrink: 0;
  line-height: 36rpx;
  text-align: center;
}

.info-text {
  flex: 1;
  font-size: 26rpx;
  color: #475569;
  line-height: 1.6;
  font-weight: 500;
  padding-top: 4rpx;
}

.action-wrapper {
  display: flex;
  gap: 20rpx;
  margin-bottom: 24rpx;
}

.action-btn {
  flex: 1;
  height: 92rpx;
  border-radius: 20rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32rpx;
  font-weight: 700;
  border: none;
}

.btn-primary {
  background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
  color: #ffffff;
  box-shadow: 0 8rpx 24rpx rgba(99, 102, 241, 0.3);
}

.btn-primary:active {
  opacity: 0.9;
  transform: translateY(2rpx);
}

.btn-secondary {
  background: #ffffff;
  color: #6366f1;
  border: 2rpx solid #e2e8f0;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
}

.btn-secondary:active {
  background: #f8fafc;
}

.tip-wrapper {
  text-align: center;
  padding: 24rpx 32rpx;
  background: rgba(255, 255, 255, 0.6);
  border-radius: 16rpx;
  backdrop-filter: blur(10rpx);
  border: 1rpx solid rgba(226, 232, 240, 0.8);
}

.tip-text {
  font-size: 24rpx;
  color: #64748b;
  line-height: 1.6;
}
</style>
