<template>
  <div class="share-container">
    <!-- 导航栏 -->
    <div class="nav-bar">
      <div class="back-btn" @click="$router.back()">
        <i class="arrow-left"></i>
      </div>
      <div class="title">邀请好友</div>
    </div>

    <!-- 页面内容 -->
    <div class="content vh100">
      <div class="pt30">
        <div class="tc">
          <div class="tc_100">
            <img class="logo-image" src="@/assets/logo.png" alt="Logo" />
          </div>
        </div>

        <div class="invite-container">
          <div class="qrcode-wrapper">
            <img class="qrcode-image" :src="qrCodeUrl" alt="QR Code" />
          </div>

          <div class="invite-link">
            <div class="link-text">{{ inviteLink }}</div>
          </div>

          <div class="action-buttons">
            <div class="action-btn" @click="saveQRCode">保存二维码</div>
            <div class="action-btn" @click="copyLink">复制链接</div>
          </div>

          <!-- <div class="divider">
            <div class="divider-text">领取任务</div>
          </div> -->

          <!-- <div class="task-list">
            <div v-for="task in tasks" :key="task.people" class="task-card">
              <div class="task-header-row">
                <div class="task-title">邀请{{ task.people }}人</div>
                <div class="task-divider"></div>
              </div>

              <div class="task-content">
                <div class="task-info-row">
                  <span class="task-label">完成奖励</span>
                  <span class="task-value reward-value">邀请{{ task.people }}人奖励财政补贴{{ task.money }}万元+{{ task.coins }}养生币</span>
                </div>
                <div class="task-info-row">
                  <span class="task-label">完成标准</span>
                  <span class="task-value">需实名认证</span>
                </div>
              </div>

              <div class="task-footer">
                <div class="task-action-btn" @click="claimReward(task)">领取</div>
              </div>
            </div>
          </div> -->
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getUserInfo } from '@/api/home';
import { getShareReward } from '@/api/home';
import QRCode from 'qrcode';

export default {
  name: 'Share',
  data() {
    return {
      userInfo: {},
      qrCodeUrl: '',  // Will be generated dynamically
      tasks: [
        { people: 6, money: 10, coins: 100 },
        { people: 15, money: 20, coins: 200 },
        { people: 30, money: 40, coins: 400 },
        { people: 60, money: 88, coins: 800 },
        { people: 100, money: 168, coins: 1200 },
        { people: 200, money: 268, coins: 2000 }
      ]
    }
  },
  computed: {
    inviteLink() {
      return this.userInfo.inviteLink || '';
    }
  },
  methods: {
    async getUserInfo() {
      try {
        const res = await getUserInfo();
        this.userInfo = res.data;
        // Generate QR code after getting user info
        await this.generateQRCode();
      } catch (error) {
        console.error('获取用户信息失败:', error);
      }
    },
    async generateQRCode() {
      try {
        if (!this.inviteLink) {
          console.error('邀请链接不存在');
          return;
        }
        // Add a random parameter to make QR code unique but still point to same URL
        const timestamp = new Date().getTime();
        const randomStr = Math.random().toString(36).substring(7);
        const urlWithNoise = `${this.inviteLink}&_=${timestamp}-${randomStr}`;
        
        // Generate QR code
        const qrOptions = {
          errorCorrectionLevel: 'H',
          type: 'image/png',
          quality: 0.92,
          margin: 1,
          color: {
            dark: '#000000',
            light: '#FFFFFF'
          }
        };
        
        this.qrCodeUrl = await QRCode.toDataURL(urlWithNoise, qrOptions);
      } catch (error) {
        console.error('生成二维码失败:', error);
      }
    },
    saveQRCode() {
      // Create a temporary link to download the QR code
      const link = document.createElement('a');
      link.download = 'invite-qrcode.png';
      link.href = this.qrCodeUrl;
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      alert('二维码已保存');
    },
    copyLink() {
      // Copy link to clipboard
      const textarea = document.createElement('textarea');
      textarea.value = this.inviteLink;
      document.body.appendChild(textarea);
      textarea.select();
      document.execCommand('copy');
      document.body.removeChild(textarea);
      alert('链接已复制到剪贴板');
    },
    async claimReward(task) {
      try {
        const res = await getShareReward(task.people);
        alert(res.msg);
      } catch (error) {
        alert(error.msg || '领取失败');
      }
    }
  },
  created() {
    this.getUserInfo();
  }
}
</script>

<style scoped>
.share-container {
  min-height: 100vh;
  background: url('../assets/dabag.png');
  background-size: 100% 100%;
  background-position: 0 0;
  background-repeat: no-repeat;
}

.nav-bar {
  height: 44px;
  background-color: #ed4b39;
  color: #fff;
  display: flex;
  align-items: center;
  position: relative;
  padding: 0 15px;
}

.back-btn {
  width: 24px;
  height: 44px;
  display: flex;
  align-items: center;
  cursor: pointer;
}

.arrow-left {
  width: 12px;
  height: 12px;
  border-left: 2px solid #fff;
  border-bottom: 2px solid #fff;
  transform: rotate(45deg);
}

.title {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  font-size: 16px;
  font-weight: 500;
}

.vh100 {
  min-height: calc(100vh - 44px);
}

.pt30 {
  padding-top: 30px;
}

.tc {
  text-align: center;
}

.tc_100 {
  margin-bottom: 30px;
}

.logo-image {
  height: 80px;
  object-fit: contain;
}

.invite-container {
  padding: 20px;
  background: #fff;
  border-radius: 12px;
  margin: 0 15px;
}

.qrcode-wrapper {
  text-align: center;
  margin-bottom: 20px;
}

.qrcode-image {
  width: 200px;
  height: 200px;
}

.invite-link {
  background: #f8f8f8;
  padding: 10px;
  border-radius: 8px;
  margin-bottom: 20px;
}

.link-text {
  color: #666;
  font-size: 14px;
  word-break: break-all;
}

.action-buttons {
  display: flex;
  justify-content: space-around;
  margin-bottom: 30px;
}

.action-btn {
  background: #ed4b39;
  color: #fff;
  padding: 8px 20px;
  border-radius: 20px;
  font-size: 14px;
  cursor: pointer;
}

.divider {
  text-align: center;
  position: relative;
  margin: 30px 0;
}

.divider::before,
.divider::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 30%;
  height: 1px;
  background: #eee;
}

.divider::before {
  left: 0;
}

.divider::after {
  right: 0;
}

.divider-text {
  display: inline-block;
  padding: 0 15px;
  color: #999;
  background: #fff;
  position: relative;
  z-index: 1;
}

.task-list {
  margin-top: 20px;
}

.task-card {
  background: #fff;
  border: 1px solid #eee;
  border-radius: 8px;
  padding: 15px;
  margin-bottom: 15px;
}

.task-header-row {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}

.task-title {
  font-size: 16px;
  font-weight: 500;
  color: #333;
}

.task-divider {
  flex: 1;
  height: 1px;
  background: #eee;
  margin-left: 15px;
}

.task-info-row {
  display: flex;
  margin-bottom: 10px;
}

.task-label {
  color: #666;
  width: 70px;
}

.task-value {
  flex: 1;
  color: #333;
}

.reward-value {
  color: #ed4b39;
}

.task-footer {
  margin-top: 15px;
  text-align: right;
}

.task-action-btn {
  display: inline-block;
  padding: 6px 15px;
  background: #ed4b39;
  color: #fff;
  border-radius: 15px;
  font-size: 14px;
  cursor: pointer;
}

.task-action-btn:active {
  opacity: 0.9;
}
</style> 