<template>
  <div class="share-container">
    <!-- Add Toast component -->
    <Toast ref="toast" />
    <!-- 导航栏 -->
    <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/hongqi.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 v-if="showAuthDialog" class="custom-dialog-mask">
      <div class="custom-dialog auth-dialog">
        <div class="dialog-close" @click="showAuthDialog = false">
          <i class="close-icon"></i>
        </div>
        <div class="dialog-icon">
          <svg t="1710401275044" class="auth-icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6356">
            <path d="M512 512m-448 0a448 448 0 1 0 896 0 448 448 0 1 0-896 0Z" fill="#1890FF" p-id="6357"></path>
            <path d="M512 320c-17.7 0-32 14.3-32 32v256c0 17.7 14.3 32 32 32s32-14.3 32-32V352c0-17.7-14.3-32-32-32z" fill="#FFFFFF" p-id="6358"></path>
            <path d="M512 720m-48 0a48 48 0 1 0 96 0 48 48 0 1 0-96 0Z" fill="#FFFFFF" p-id="6359"></path>
          </svg>
        </div>
        <div class="dialog-title auth-title">温馨提示</div>
        <div class="dialog-content">您还未完成实名认证<br/>请先进行实名认证</div>
        <button class="dialog-btn auth-btn" @click="goToAuth">去认证</button>
      </div>
    </div>

    <!-- 二维码保存成功提示弹窗 -->
    <div v-if="showQrSavedDialog" class="custom-dialog-mask">
      <div class="custom-dialog success-dialog">
        <div class="dialog-close" @click="showQrSavedDialog = false">
          <i class="close-icon"></i>
        </div>
        <div class="dialog-icon">
          <svg t="1710400631664" class="success-icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4266">
            <path d="M512 512m-448 0a448 448 0 1 0 896 0 448 448 0 1 0-896 0Z" fill="#52C41A" p-id="4267"></path>
            <path d="M466.7 679.8c-8.5 0-16.6-3.4-22.6-9.4l-181-181.1c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0l158.4 158.4 249.7-249.6c12.5-12.5 32.8-12.5 45.3 0s12.5 32.8 0 45.3L489.3 670.4c-6 6-14.1 9.4-22.6 9.4z" fill="#FFFFFF" p-id="4268"></path>
          </svg>
        </div>
        <div class="dialog-title success-title">保存成功</div>
        <div class="dialog-content">二维码已成功保存到本地</div>
        <button class="dialog-btn success-btn" @click="showQrSavedDialog = false">确定</button>
      </div>
    </div>

    <!-- 链接复制成功提示弹窗 -->
    <div v-if="showLinkCopiedDialog" class="custom-dialog-mask">
      <div class="custom-dialog success-dialog">
        <div class="dialog-close" @click="showLinkCopiedDialog = false">
          <i class="close-icon"></i>
        </div>
        <div class="dialog-icon">
          <svg t="1710400631664" class="success-icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4266">
            <path d="M512 512m-448 0a448 448 0 1 0 896 0 448 448 0 1 0-896 0Z" fill="#52C41A" p-id="4267"></path>
            <path d="M466.7 679.8c-8.5 0-16.6-3.4-22.6-9.4l-181-181.1c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0l158.4 158.4 249.7-249.6c12.5-12.5 32.8-12.5 45.3 0s12.5 32.8 0 45.3L489.3 670.4c-6 6-14.1 9.4-22.6 9.4z" fill="#FFFFFF" p-id="4268"></path>
          </svg>
        </div>
        <div class="dialog-title success-title">复制成功</div>
        <div class="dialog-content">邀请链接已成功复制到剪贴板</div>
        <button class="dialog-btn success-btn" @click="showLinkCopiedDialog = false">确定</button>
      </div>
    </div>
  </div>
</template>

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

export default {
  name: 'Share',
  components: {
    Toast
  },
  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 }
      ],
      showAuthDialog: false,
      showQrSavedDialog: false,
      showLinkCopiedDialog: false
    }
  },
  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);
      this.showQrSavedDialog = true;
    },
    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);
      this.showLinkCopiedDialog = true;
    },
    async claimReward(task) {
      // 检查实名认证状态
      if (this.userInfo.if_real === 0) {
        this.showAuthDialog = true;
        return;
      }

      try {
        const res = await getShareReward(task.people);
        this.$refs.toast.show(res.msg, 'success');
      } catch (error) {
        this.$refs.toast.show(error.msg || '领取失败', 'error');
      }
    },
    goToAuth() {
      this.$router.push('/user-center');
    }
  },
  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: 10px;
}

.tc {
  text-align: center;
}

.tc_100 {
  margin-bottom: 30px;
  width: 60px;
  height: 60px;
  background: #fff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 30px;
}

.logo-image {
  width: 40px;
  height: 40px;
  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;
}

/* 认证弹窗样式 */
.custom-dialog-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}

.custom-dialog {
  position: relative;
  width: 280px;
  background: #fff;
  border-radius: 12px;
  padding: 20px;
  text-align: center;
  animation: dialogFadeIn 0.3s ease;
}

.dialog-icon {
  width: 60px;
  height: 60px;
  margin: 0 auto 16px;
}

.auth-icon {
  width: 100%;
  height: 100%;
}

.dialog-title {
  font-size: 18px;
  font-weight: 500;
  margin-bottom: 12px;
}

.auth-title {
  color: #1890FF;
}

.dialog-content {
  font-size: 14px;
  color: #666;
  line-height: 1.8;
  margin: 16px 0 24px;
}

.dialog-btn {
  width: 100%;
  height: 40px;
  border: none;
  border-radius: 20px;
  font-size: 16px;
  color: #fff;
  cursor: pointer;
  transition: all 0.3s ease;
}

.auth-btn {
  background: #1890FF;
}

.auth-btn:hover {
  background: #40a9ff;
}

.auth-dialog {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.dialog-close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
}

.dialog-close:hover {
  transform: rotate(90deg);
}

.dialog-close .close-icon {
  position: relative;
  width: 16px;
  height: 16px;
}

.dialog-close .close-icon::before,
.dialog-close .close-icon::after {
  content: '';
  position: absolute;
  width: 100%;
  height: 2px;
  background-color: #999;
  top: 50%;
  left: 0;
}

.dialog-close .close-icon::before {
  transform: rotate(45deg);
}

.dialog-close .close-icon::after {
  transform: rotate(-45deg);
}

@keyframes dialogFadeIn {
  from {
    opacity: 0;
    transform: scale(0.9);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

/* 成功提示弹窗样式 */
.success-dialog {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.success-icon {
  width: 100%;
  height: 100%;
}

.success-title {
  color: #52C41A;
}

.success-btn {
  background: #52C41A;
}

.success-btn:hover {
  background: #73d13d;
}

.success-btn:active {
  transform: scale(0.98);
}
</style> 