<template>
  <view class="promotion-page">
    <!-- 顶部背景 -->
    <view class="header-bg">
      <image src="/static/promotion/bg.png" class="bg-image" mode="aspectFill"></image>
    </view>

    <!-- 主要内容 -->
    <view class="content">
      <!-- 邀请卡片 -->
      <view class="invite-card">
        <view class="card-header">
          <text class="title">邀请好友得奖励</text>
          <text class="subtitle">每成功邀请1位好友，最高可得50元奖励</text>
        </view>
        
        <view class="reward-stats">
          <view class="stat-item">
            <text class="stat-value">{{ promotionData.totalReward || 0 }}</text>
            <text class="stat-label">累计奖励(元)</text>
          </view>
          <view class="stat-item">
            <text class="stat-value">{{ promotionData.invitedCount || 0 }}</text>
            <text class="stat-label">已邀请好友</text>
          </view>
          <view class="stat-item">
            <text class="stat-value">{{ promotionData.successCount || 0 }}</text>
            <text class="stat-label">成功邀请</text>
          </view>
        </view>

        <view class="invite-code">
          <text class="code-label">我的邀请码</text>
          <text class="code-value">{{ promotionData.inviteCode || '加载中...' }}</text>
          <view class="copy-btn" @click="copyInviteCode">
            <uni-icons type="copy" size="16" color="#007aff"></uni-icons>
            <text>复制</text>
          </view>
        </view>
      </view>

      <!-- 邀请方式 -->
      <view class="invite-methods">
        <text class="section-title">邀请方式</text>
        
        <view class="method-list">
          <view class="method-item" @click="shareToWechat">
            <view class="method-icon wechat">
              <uni-icons type="weixin" size="24" color="#07c160"></uni-icons>
            </view>
            <text class="method-text">微信好友</text>
          </view>
          
          <view class="method-item" @click="shareToMoment">
            <view class="method-icon moment">
              <uni-icons type="chat" size="24" color="#07c160"></uni-icons>
            </view>
            <text class="method-text">朋友圈</text>
          </view>
          
          <view class="method-item" @click="saveQRCode">
            <view class="method-icon qrcode">
              <uni-icons type="scan" size="24" color="#007aff"></uni-icons>
            </view>
            <text class="method-text">保存二维码</text>
          </view>
          
          <view class="method-item" @click="copyLink">
            <view class="method-icon link">
              <uni-icons type="link" size="24" color="#ff9500"></uni-icons>
            </view>
            <text class="method-text">复制链接</text>
          </view>
        </view>
      </view>

      <!-- 奖励规则 -->
      <view class="reward-rules">
        <text class="section-title">奖励规则</text>
        
        <view class="rule-list">
          <view class="rule-item">
            <uni-icons type="checkmark" size="16" color="#52c41a"></uni-icons>
            <text>每成功邀请1位新用户注册，您可获得10元奖励</text>
          </view>
          
          <view class="rule-item">
            <uni-icons type="checkmark" size="16" color="#52c41a"></uni-icons>
            <text>好友完成首单消费，您再获得20元奖励</text>
          </view>
          
          <view class="rule-item">
            <uni-icons type="checkmark" size="16" color="#52c41a"></uni-icons>
            <text>好友成为活跃用户，您额外获得20元奖励</text>
          </view>
          
          <view class="rule-item">
            <uni-icons type="checkmark" size="16" color="#52c41a"></uni-icons>
            <text>奖励累计满50元即可提现</text>
          </view>
        </view>
      </view>

      <!-- 邀请记录 -->
      <view class="invite-records">
        <view class="records-header">
          <text class="section-title">邀请记录</text>
          <text class="view-all" @click="viewAllRecords">查看全部</text>
        </view>
        
        <view class="record-list">
          <view class="record-item" v-for="record in recentRecords" :key="record.id">
            <image :src="record.avatar" class="avatar" mode="aspectFill"></image>
            <view class="record-info">
              <text class="username">{{ record.username }}</text>
              <text class="time">{{ record.inviteTime }}</text>
            </view>
            <view class="record-status">
              <text class="status" :class="getStatusClass(record.status)">{{ getStatusText(record.status) }}</text>
              <text class="reward" v-if="record.reward > 0">+{{ record.reward }}元</text>
            </view>
          </view>
          
          <view class="empty-tip" v-if="recentRecords.length === 0">
            <uni-icons type="info" size="20" color="#999"></uni-icons>
            <text>暂无邀请记录</text>
          </view>
        </view>
      </view>

      <!-- 活动分享 -->
      <view class="activity-share">
        <text class="section-title">活动分享</text>
        <view class="share-tips">
          <text>分享您参与的活动，邀请好友一起参加，可获得额外奖励！</text>
        </view>
        <view class="share-actions">
          <button class="share-btn" @click="shareActivity">分享活动</button>
        </view>
      </view>

      <!-- 认养分享 -->
      <view class="adoption-share">
        <text class="section-title">认养分享</text>
        <view class="share-tips">
          <text>分享您认养的动物，邀请好友一起认养，可获得丰厚奖励！</text>
        </view>
        <view class="share-actions">
          <button class="share-btn" @click="shareAdoption">分享认养</button>
        </view>
      </view>

      <!-- 多人认养 -->
      <view class="group-adoption">
        <text class="section-title">多人认养</text>
        <view class="share-tips">
          <text>邀请好友共同认养一个动物，享受更多乐趣和优惠！</text>
        </view>
        <view class="share-actions">
          <button class="invite-btn" @click="inviteGroupAdoption">发起多人认养</button>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { promotionService } from '../../api/services.js'

export default {
  data() {
    return {
      promotionData: {
        totalReward: 0,
        invitedCount: 0,
        successCount: 0,
        inviteCode: ''
      },
      recentRecords: [],
      loading: false
    }
  },

  onLoad() {
    this.loadPromotionData()
    this.loadRecentRecords()
  },

  onShareAppMessage() {
    return {
      title: '结伴客 - 邀请好友得奖励',
      path: '/pages/index/index?inviteCode=' + this.promotionData.inviteCode,
      imageUrl: '/static/promotion/share.jpg'
    }
  },

  methods: {
    async loadPromotionData() {
      this.loading = true
      try {
        const response = await promotionService.getPromotionData()
        this.promotionData = response
      } catch (error) {
        console.error('加载推广数据失败:', error)
        uni.showToast({
          title: '加载失败',
          icon: 'none'
        })
      } finally {
        this.loading = false
      }
    },

    async loadRecentRecords() {
      try {
        const response = await promotionService.getRecentRecords()
        this.recentRecords = response.list || []
      } catch (error) {
        console.error('加载邀请记录失败:', error)
      }
    },

    copyInviteCode() {
      if (!this.promotionData.inviteCode) return
      
      uni.setClipboardData({
        data: this.promotionData.inviteCode,
        success: () => {
          uni.showToast({
            title: '邀请码已复制',
            icon: 'success'
          })
        }
      })
    },

    shareToWechat() {
      uni.share({
        provider: 'weixin',
        scene: 'WXSceneSession',
        type: 0,
        title: '结伴客 - 邀请好友得奖励',
        summary: '每成功邀请1位好友，最高可得50元奖励！',
        href: 'https://jiebanke.com?inviteCode=' + this.promotionData.inviteCode,
        imageUrl: '/static/promotion/share.jpg',
        success: () => {
          uni.showToast({
            title: '分享成功',
            icon: 'success'
          })
        }
      })
    },

    shareToMoment() {
      uni.share({
        provider: 'weixin',
        scene: 'WXSceneTimeline',
        type: 0,
        title: '结伴客 - 邀请好友得奖励',
        summary: '每成功邀请1位好友，最高可得50元奖励！',
        href: 'https://jiebanke.com?inviteCode=' + this.promotionData.inviteCode,
        imageUrl: '/static/promotion/share.jpg',
        success: () => {
          uni.showToast({
            title: '分享成功',
            icon: 'success'
          })
        }
      })
    },

    saveQRCode() {
      uni.showLoading({
        title: '生成二维码中...'
      })
      
      // 模拟生成二维码
      setTimeout(() => {
        uni.hideLoading()
        uni.showToast({
          title: '二维码已保存到相册',
          icon: 'success'
        })
      }, 1000)
    },

    copyLink() {
      const link = 'https://jiebanke.com?inviteCode=' + this.promotionData.inviteCode
      uni.setClipboardData({
        data: link,
        success: () => {
          uni.showToast({
            title: '链接已复制',
            icon: 'success'
          })
        }
      })
    },

    getStatusClass(status) {
      const statusMap = {
        registered: 'registered',
        ordered: 'ordered',
        active: 'active',
        completed: 'completed'
      }
      return statusMap[status] || 'registered'
    },

    getStatusText(status) {
      const statusTextMap = {
        registered: '已注册',
        ordered: '已下单',
        active: '已活跃',
        completed: '已完成'
      }
      return statusTextMap[status] || '已注册'
    },

    viewAllRecords() {
      uni.navigateTo({
        url: '/pages/promotion/records'
      })
    },

    shareActivity() {
      uni.navigateTo({
        url: '/pages/promotion/share-activity'
      })
    },

    shareAdoption() {
      uni.navigateTo({
        url: '/pages/promotion/share-adoption'
      })
    },

    inviteGroupAdoption() {
      uni.navigateTo({
        url: '/pages/promotion/group-adoption'
      })
    }
  }
}
</script>

<style scoped>
.promotion-page {
  background-color: #f8f9fa;
  min-height: 100vh;
}

.header-bg {
  height: 200rpx;
  position: relative;
}

.bg-image {
  width: 100%;
  height: 100%;
}

.content {
  padding: 20rpx;
  margin-top: -60rpx;
}

.invite-card {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 20rpx;
  padding: 40rpx 30rpx;
  color: #fff;
  margin-bottom: 30rpx;
  box-shadow: 0 10rpx 30rpx rgba(102, 126, 234, 0.3);
}

.card-header {
  text-align: center;
  margin-bottom: 30rpx;
}

.title {
  font-size: 36rpx;
  font-weight: bold;
  display: block;
  margin-bottom: 10rpx;
}

.subtitle {
  font-size: 26rpx;
  opacity: 0.9;
}

.reward-stats {
  display: flex;
  justify-content: space-around;
  margin-bottom: 30rpx;
  padding: 20rpx 0;
  border-bottom: 1rpx solid rgba(255, 255, 255, 0.2);
}

.stat-item {
  text-align: center;
}

.stat-value {
  font-size: 32rpx;
  font-weight: bold;
  display: block;
  margin-bottom: 5rpx;
}

.stat-label {
  font-size: 24rpx;
  opacity: 0.8;
}

.invite-code {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(255, 255, 255, 0.1);
  padding: 20rpx;
  border-radius: 12rpx;
}

.code-label {
  font-size: 26rpx;
}

.code-value {
  font-size: 28rpx;
  font-weight: bold;
  letter-spacing: 1rpx;
}

.copy-btn {
  display: flex;
  align-items: center;
  background: rgba(255, 255, 255, 0.2);
  padding: 8rpx 16rpx;
  border-radius: 8rpx;
  font-size: 24rpx;
}

.invite-methods,
.reward-rules,
.invite-records,
.activity-share,
.adoption-share,
.group-adoption {
  background-color: #fff;
  border-radius: 16rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.1);
}

.section-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  display: block;
  margin-bottom: 20rpx;
}

.method-list {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20rpx;
}

.method-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 30rpx 20rpx;
  border: 1rpx solid #eee;
  border-radius: 12rpx;
  background-color: #fafafa;
}

.method-icon {
  width: 60rpx;
  height: 60rpx;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 15rpx;
}

.method-icon.wechat {
  background-color: #e6f7e6;
}

.method-icon.moment {
  background-color: #e6f7ff;
}

.method-icon.qrcode {
  background-color: #e6f7ff;
}

.method-icon.link {
  background-color: #fff7e6;
}

.method-text {
  font-size: 26rpx;
  color: #333;
}

.rule-list {
  padding-left: 10rpx;
}

.rule-item {
  display: flex;
  align-items: flex-start;
  margin-bottom: 20rpx;
  font-size: 26rpx;
  color: #666;
  line-height: 1.5;
}

.rule-item uni-icons {
  margin-right: 15rpx;
  margin-top: 4rpx;
  flex-shrink: 0;
}

.records-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
}

.view-all {
  font-size: 26rpx;
  color: #007aff;
}

.record-list {
  max-height: 400rpx;
  overflow-y: auto;
}

.record-item {
  display: flex;
  align-items: center;
  padding: 20rpx 0;
  border-bottom: 1rpx solid #f0f0f0;
}

.record-item:last-child {
  border-bottom: none;
}

.avatar {
  width: 80rpx;
  height: 80rpx;
  border-radius: 50%;
  margin-right: 20rpx;
}

.record-info {
  flex: 1;
}

.username {
  font-size: 28rpx;
  color: #333;
  display: block;
  margin-bottom: 5rpx;
}

.time {
  font-size: 24rpx;
  color: #999;
}

.record-status {
  text-align: right;
}

.status {
  font-size: 24rpx;
  display: block;
  margin-bottom: 5rpx;
}

.status.registered {
  color: #1890ff;
}

.status.ordered {
  color: #52c41a;
}

.status.active {
  color: #fa8c16;
}

.status.completed {
  color: #722ed1;
}

.reward {
  font-size: 24rpx;
  color: #ff6b35;
  font-weight: bold;
}

.empty-tip {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 60rpx 0;
  color: #999;
}

.empty-tip uni-icons {
  margin-bottom: 15rpx;
}

.share-tips {
  margin-bottom: 25rpx;
}

.share-tips text {
  font-size: 26rpx;
  color: #666;
  line-height: 1.5;
}

.share-actions {
  text-align: center;
}

.share-btn,
.invite-btn {
  background: linear-gradient(135deg, #ff6b8b 0%, #ff8fb1 100%);
  color: #fff;
  border: none;
  border-radius: 40rpx;
  padding: 20rpx 40rpx;
  font-size: 28rpx;
  font-weight: bold;
}

.invite-btn {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
</style>