<template>
  <Teleport to="body">
    <transition name="modal-fade">
      <div v-if="visible" class="promotion-modal-overlay" @click.self="closeModal">
        <div class="promotion-modal">
          <div class="modal-header">
            <h3>您的专属推广链接</h3>
            <button @click="closeModal" class="close-btn">
              X
            </button>
          </div>
          
          <div class="modal-body">
            <!-- 推广链接 -->
            <div class="link-section">
              <div class="link-input-group">
                <input 
                  type="text" 
                  :value="promotionLink" 
                  readonly 
                  class="link-input"
                  placeholder="生成推广链接中..."
                />
                <button 
                  @click="copyLink" 
                  class="copy-btn"
                  :class="{ 'copied': copied }"
                  :disabled="!promotionLink"
                >
                  {{ copied ? '已复制' : '复制链接' }}
                </button>
              </div>
            </div>
            
            <!-- 提示信息 -->
            <div class="tips-section">
              <div class="tip-item">
                <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                  <path d="M8 0C3.58172 0 0 3.58172 0 8C0 12.4183 3.58172 16 8 16C12.4183 16 16 12.4183 16 8C16 3.58172 12.4183 0 8 0ZM8 14C4.68629 14 2 11.3137 2 8C2 4.68629 4.68629 2 8 2C11.3137 2 14 4.68629 14 8C14 11.3137 11.3137 14 8 14Z" fill="#1AC3FF"/>
                  <path d="M8 11C8.55228 11 9 10.5523 9 10V8C9 7.44772 8.55228 7 8 7C7.44772 7 7 7.44772 7 8V10C7 10.5523 7.44772 11 8 11Z" fill="#1AC3FF"/>
                  <path d="M8 6C8.55228 6 9 5.55228 9 5C9 4.44772 8.55228 4 8 4C7.44772 4 7 4.44772 7 5C7 5.55228 7.44772 6 8 6Z" fill="#1AC3FF"/>
                </svg>
                <span>分享此链接给好友，好友注册即可绑定邀请关系</span>
              </div>
              <div class="tip-item">
                <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                  <path d="M8 0C3.58172 0 0 3.58172 0 8C0 12.4183 3.58172 16 8 16C12.4183 16 16 12.4183 16 8C16 3.58172 12.4183 0 8 0ZM8 14C4.68629 14 2 11.3137 2 8C2 4.68629 4.68629 2 8 2C11.3137 2 14 4.68629 14 8C14 11.3137 11.3137 14 8 14Z" fill="#1AC3FF"/>
                  <path d="M8 11C8.55228 11 9 10.5523 9 10V8C9 7.44772 8.55228 7 8 7C7.44772 7 7 7.44772 7 8V10C7 10.5523 7.44772 11 8 11Z" fill="#1AC3FF"/>
                  <path d="M8 6C8.55228 6 9 5.55228 9 5C9 4.44772 8.55228 4 8 4C7.44772 4 7 4.44772 7 5C7 5.55228 7.44772 6 8 6Z" fill="#1AC3FF"/>
                </svg>
                <span>好友付费购买会员后，您可获得分成收益</span>
              </div>
              <div class="tip-item">
                <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                  <path d="M8 0C3.58172 0 0 3.58172 0 8C0 12.4183 3.58172 16 8 16C12.4183 16 16 12.4183 16 8C16 3.58172 12.4183 0 8 0ZM8 14C4.68629 14 2 11.3137 2 8C2 4.68629 4.68629 2 8 2C11.3137 2 14 4.68629 14 8C14 11.3137 11.3137 14 8 14Z" fill="#1AC3FF"/>
                  <path d="M8 11C8.55228 11 9 10.5523 9 10V8C9 7.44772 8.55228 7 8 7C7.44772 7 7 7.44772 7 8V10C7 10.5523 7.44772 11 8 11Z" fill="#1AC3FF"/>
                  <path d="M8 6C8.55228 6 9 5.55228 9 5C9 4.44772 8.55228 4 8 4C7.44772 4 7 4.44772 7 5C7 5.55228 7.44772 6 8 6Z" fill="#1AC3FF"/>
                </svg>
                <span>好友注册后，即可获得系统奖励500积分</span>
              </div>
            </div>
            
            <!-- 查看详情按钮 -->
            <div class="action-section">
              <button @click="goToPromotionPage" class="detail-btn">
                查看推广详情
                <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                  <path d="M6 12L10 8L6 4" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                </svg>
              </button>
            </div>
          </div>
        </div>
      </div>
    </transition>
  </Teleport>
</template>

<script setup>
import { ref, watch, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getMyPromotionInfo, generateInviteCode } from '../../api/user.js'
import { useRouter } from 'vue-router'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['close'])

const router = useRouter()
const promotionLink = ref('')
const inviteCode = ref('')
const copied = ref(false) // 复制状态

// 获取推广信息
const fetchPromotionInfo = async () => {
  try {
    const response = await getMyPromotionInfo()
    if (response.code === 200) {
      inviteCode.value = response.data?.inviteCode || ''
      if (inviteCode.value) {
        promotionLink.value = `https://www.1818AI.com/home?invite_code=${inviteCode.value}`
      } else {
        // 如果没有邀请码，自动生成
        await generateCode()
      }
    }
  } catch (err) {
    console.error('获取推广信息失败:', err)
  }
}

// 生成邀请码
const generateCode = async () => {
  try {
    const response = await generateInviteCode()
    if (response.code === 200) {
      inviteCode.value = response.data || ''
      promotionLink.value = `https://www.1818AI.com/home?invite_code=${inviteCode.value}`
    }
  } catch (err) {
    console.error('生成邀请码失败:', err)
  }
}

// 复制链接
const copyLink = async () => {
  if (!promotionLink.value) {
    ElMessage.warning({
      message: '推广链接生成中，请稍候',
      zIndex: 10001
    })
    return
  }
  
  try {
    await navigator.clipboard.writeText(promotionLink.value)
    copied.value = true
    ElMessage.success({
      message: '推广链接已复制',
      zIndex: 10001
    })
    
    // 2秒后恢复按钮状态
    setTimeout(() => {
      copied.value = false
    }, 2000)
  } catch (err) {
    ElMessage.error({
      message: '复制失败，请手动复制',
      zIndex: 10001
    })
  }
}

// 关闭弹窗
const closeModal = () => {
  copied.value = false // 重置复制状态
  emit('close')
}

// 跳转到推广页面
const goToPromotionPage = () => {
  router.push('/personal-center/promotion')
  closeModal()
}

// 监听弹窗显示，获取推广信息
watch(() => props.visible, (newVal) => {
  if (newVal) {
    fetchPromotionInfo()
  }
})

// 组件挂载时获取推广信息
onMounted(() => {
  if (props.visible) {
    fetchPromotionInfo()
  }
})
</script>

<style scoped>
/* 弹窗遮罩 */
.promotion-modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10000;
}

/* 弹窗主体 */
.promotion-modal {
  width: 90%;
  max-width: 560px;
  background: rgba(21, 21, 21, 0.95);
  backdrop-filter: blur(20px);
  border-radius: 24px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow: 0 25px 80px rgba(0, 0, 0, 0.5);
  overflow: hidden;
}

/* 弹窗头部 */
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 24px 28px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.modal-header h3 {
  font-family: 'PingFang SC', -apple-system, BlinkMacSystemFont, sans-serif;
  font-size: 20px;
  font-weight: 600;
  color: #1AC3FF;
  margin: 0;
}

.close-btn {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 50%;
  cursor: pointer;
  transition: all 0.3s ease;
  color: rgba(255, 255, 255, 0.6);
}

.close-btn:hover {
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.2);
  color: #fff;
  transform: scale(1.05);
}

/* 弹窗内容 */
.modal-body {
  padding: 32px 28px;
}

/* 链接区域 */
.link-section {
  margin-bottom: 28px;
}

.link-input-group {
  display: flex;
  gap: 12px;
  align-items: center;
}

.link-input {
  flex: 1;
  height: 52px;
  padding: 0 20px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  font-family: 'PingFang SC', -apple-system, BlinkMacSystemFont, sans-serif;
  font-size: 14px;
  color: rgba(255, 255, 255, 0.9);
  outline: none;
  transition: all 0.3s ease;
}

.link-input:focus {
  border-color: #1AC3FF;
  background: rgba(255, 255, 255, 0.08);
  box-shadow: 0 0 0 3px rgba(26, 195, 255, 0.1);
}

.link-input::placeholder {
  color: rgba(255, 255, 255, 0.3);
}

.copy-btn {
  height: 52px;
  padding: 0 28px;
  background: linear-gradient(135deg, #1AC3FF 0%, #0F9CFF 100%);
  color: #fff;
  border: none;
  border-radius: 12px;
  font-family: 'PingFang SC', -apple-system, BlinkMacSystemFont, sans-serif;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  white-space: nowrap;
  box-shadow: 0 4px 16px rgba(26, 195, 255, 0.3);
}

.copy-btn:hover:not(:disabled) {
  background: linear-gradient(135deg, #33DDE5 0%, #1AC3FF 100%);
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(26, 195, 255, 0.4);
}

.copy-btn:disabled {
  background: rgba(255, 255, 255, 0.1);
  color: rgba(255, 255, 255, 0.3);
  cursor: not-allowed;
  box-shadow: none;
}

.copy-btn.copied {
  background: linear-gradient(135deg, #52c41a 0%, #73d13d 100%);
  box-shadow: 0 4px 16px rgba(82, 196, 26, 0.3);
}

.copy-btn.copied:hover {
  background: linear-gradient(135deg, #73d13d 0%, #95de64 100%);
  box-shadow: 0 8px 24px rgba(82, 196, 26, 0.4);
}

/* 提示信息区域 */
.tips-section {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 24px 20px;
  background: rgba(26, 195, 255, 0.05);
  border: 1px solid rgba(26, 195, 255, 0.15);
  border-radius: 16px;
  margin-bottom: 24px;
}

.tip-item {
  display: flex;
  align-items: flex-start;
  gap: 12px;
}

.tip-item svg {
  flex-shrink: 0;
  margin-top: 2px;
}

.tip-item span {
  font-family: 'PingFang SC', -apple-system, BlinkMacSystemFont, sans-serif;
  font-size: 14px;
  line-height: 1.6;
  color: rgba(255, 255, 255, 0.8);
}

/* 操作区域 */
.action-section {
  display: flex;
  justify-content: center;
}

.detail-btn {
  height: 48px;
  padding: 0 32px;
  background: rgba(255, 255, 255, 0.05);
  color: #1AC3FF;
  border: 1px solid rgba(26, 195, 255, 0.3);
  border-radius: 24px;
  font-family: 'PingFang SC', -apple-system, BlinkMacSystemFont, sans-serif;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 8px;
}

.detail-btn:hover {
  background: rgba(26, 195, 255, 0.1);
  border-color: #1AC3FF;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(26, 195, 255, 0.2);
}

.detail-btn svg {
  transition: transform 0.3s ease;
}

.detail-btn:hover svg {
  transform: translateX(4px);
}

/* 过渡动画 */
.modal-fade-enter-active,
.modal-fade-leave-active {
  transition: all 0.3s ease;
}

.modal-fade-enter-from {
  opacity: 0;
}

.modal-fade-leave-to {
  opacity: 0;
}

.modal-fade-enter-from .promotion-modal {
  transform: scale(0.9) translateY(-20px);
}

.modal-fade-leave-to .promotion-modal {
  transform: scale(0.9) translateY(-20px);
}

.modal-fade-enter-active .promotion-modal {
  transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.modal-fade-leave-active .promotion-modal {
  transition: all 0.3s ease;
}

/* 响应式 */
@media (max-width: 768px) {
  .promotion-modal {
    width: 95%;
    max-width: none;
  }
  
  .modal-header {
    padding: 20px 24px;
  }
  
  .modal-header h3 {
    font-size: 18px;
  }
  
  .modal-body {
    padding: 24px 20px;
  }
  
  .link-input-group {
    flex-direction: column;
  }
  
  .link-input {
    width: 100%;
  }
  
  .copy-btn {
    width: 100%;
  }
  
  .tips-section {
    padding: 20px 16px;
  }
  
  .tip-item span {
    font-size: 13px;
  }
}
</style>

