<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">
      <div class="logo-section">
        <div class="logo-wrapper">
          <img class="logo-image" src="@/assets/hongqi.png" alt="Logo" />
        </div>
      </div>

      <div class="invite-container">
        <div class="qrcode-section">
          <div class="qrcode-wrapper">
            <img class="qrcode-image" :src="qrCodeUrl" alt="QR Code" />
          </div>
          <div class="qrcode-label">扫码邀请好友</div>
        </div>

        <div class="invite-link-section">
          <div class="section-title">邀请链接</div>
          <div class="invite-link">
            <div class="link-text">{{ inviteLink }}</div>
          </div>
        </div>

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

    <!-- 实名认证提示弹窗 -->
    <div v-if="showAuthDialog" class="custom-dialog-mask">
      <div class="custom-dialog auth-dialog">
        <div class="dialog-header">
          <div class="dialog-title">温馨提示</div>
          <div class="dialog-close" @click="showAuthDialog = false">
            <i class="close-icon"></i>
          </div>
        </div>
        <div class="dialog-icon">
          <svg viewBox="0 0 1024 1024" width="60px" height="60px" fill="#ff4d4f" aria-hidden="true">
            <path d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64zm0 736c-159.1 0-288-128.9-288-288S352.9 224 512 224s288 128.9 288 288-128.9 288-288 288z"></path>
            <path d="M464 688a48 48 0 1 0 96 0 48 48 0 1 0-96 0zm72-112c4.4 0 8-3.6 8-8V296c0-4.4-3.6-8-8-8h-48c-4.4 0-8 3.6-8 8v272c0 4.4 3.6 8 8 8h48z"></path>
          </svg>
        </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-header">
          <div class="dialog-title">保存成功</div>
          <div class="dialog-close" @click="showQrSavedDialog = false">
            <i class="close-icon"></i>
          </div>
        </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="#2f9580" 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-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-header">
          <div class="dialog-title">复制成功</div>
          <div class="dialog-close" @click="showLinkCopiedDialog = false">
            <i class="close-icon"></i>
          </div>
        </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="#2f9580" 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-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: linear-gradient(to bottom, #2f9580 0%, #4fb3a1 100%);
}

.nav-bar {
  height: 56px;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  color: #fff;
  display: flex;
  align-items: center;
  position: relative;
  padding: 0 15px;
}

.back-btn {
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
}

.back-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: scale(1.05);
}

.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: 18px;
  font-weight: 600;
}

.content {
  padding: 20px;
  min-height: calc(100vh - 56px);
}

.logo-section {
  text-align: center;
  margin-bottom: 30px;
}

.logo-wrapper {
  width: 80px;
  height: 80px;
  background: #fff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

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

.invite-container {
  background: #fff;
  border-radius: 16px;
  padding: 30px 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.qrcode-section {
  text-align: center;
  margin-bottom: 30px;
}

.qrcode-wrapper {
  background: #f8f9fa;
  border-radius: 16px;
  padding: 20px;
  margin-bottom: 16px;
  display: inline-block;
}

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

.qrcode-label {
  font-size: 16px;
  color: #374151;
  font-weight: 500;
}

.invite-link-section {
  margin-bottom: 30px;
}

.section-title {
  font-size: 16px;
  color: #374151;
  font-weight: 600;
  margin-bottom: 12px;
}

.invite-link {
  background: #f8f9fa;
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  padding: 16px;
}

.link-text {
  color: #6b7280;
  font-size: 14px;
  word-break: break-all;
  line-height: 1.5;
}

.action-buttons {
  display: flex;
  gap: 12px;
}

.action-btn {
  flex: 1;
  height: 48px;
  line-height: 48px;
  text-align: center;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.action-btn.primary {
  background: #2f9580;
  color: #fff;
  box-shadow: 0 4px 12px rgba(47, 149, 128, 0.3);
}

.action-btn.primary:hover {
  background: #268371;
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(47, 149, 128, 0.4);
}

.action-btn.secondary {
  background: #f8f9fa;
  color: #374151;
  border: 1px solid #e5e7eb;
}

.action-btn.secondary:hover {
  background: #e5e7eb;
  transform: translateY(-1px);
}

.action-btn:active {
  transform: translateY(0);
}

.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 {
  width: 320px;
  background: #fff;
  border-radius: 16px;
  overflow: hidden;
  animation: dialogFadeIn 0.3s ease;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
}

.dialog-header {
  background: linear-gradient(135deg, #2f9580 0%, #4fb3a1 100%);
  color: #fff;
  padding: 20px;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.dialog-title {
  font-size: 18px;
  font-weight: 600;
  text-align: center;
}

.dialog-close {
  position: absolute;
  top: 50%;
  right: 15px;
  transform: translateY(-50%);
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.2);
}

.dialog-close:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: translateY(-50%) rotate(90deg);
}

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

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

.close-icon::before {
  transform: translateY(-50%) rotate(45deg);
}

.close-icon::after {
  transform: translateY(-50%) rotate(-45deg);
}

.dialog-icon {
  width: 80px;
  height: 80px;
  margin: 30px auto 20px;
  display: flex;
  align-items: center;
  justify-content: center;
}

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

.dialog-content {
  font-size: 15px;
  color: #374151;
  line-height: 1.6;
  margin: 0 20px 30px;
  text-align: center;
}

.dialog-btn {
  width: calc(100% - 40px);
  height: 48px;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  color: #fff;
  cursor: pointer;
  transition: all 0.3s ease;
  margin: 0 20px 20px;
  display: block;
}

.auth-btn {
  background: #2f9580;
}

.success-btn {
  background: #2f9580;
}

.dialog-btn:hover {
  opacity: 0.9;
  transform: translateY(-1px);
}

.dialog-btn:active {
  transform: translateY(0);
}

@keyframes dialogFadeIn {
  from {
    opacity: 0;
    transform: scale(0.95) translateY(-20px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

/* 移动端适配 */
@media (max-width: 768px) {
  .nav-bar {
    height: 50px;
    padding: 0 12px;
  }
  
  .back-btn {
    width: 36px;
    height: 36px;
  }
  
  .title {
    font-size: 16px;
  }
  
  .content {
    padding: 16px;
  }
  
  .logo-wrapper {
    width: 70px;
    height: 70px;
  }
  
  .logo-image {
    width: 45px;
    height: 45px;
  }
  
  .invite-container {
    padding: 24px 16px;
  }
  
  .qrcode-image {
    width: 180px;
    height: 180px;
  }
  
  .action-buttons {
    flex-direction: column;
    gap: 12px;
  }
  
  .custom-dialog {
    width: 90%;
    max-width: 300px;
  }
  
  .dialog-header {
    padding: 16px;
  }
  
  .dialog-title {
    font-size: 16px;
  }
  
  .dialog-content {
    margin: 0 16px 24px;
    font-size: 14px;
  }
  
  .dialog-btn {
    width: calc(100% - 32px);
    margin: 0 16px 16px;
    height: 44px;
    font-size: 15px;
  }
}
</style> 