<template>
  <div class="usremei-container">
    <div class="invite-header">
      邀请好友获得红旗资产
    </div>
    <div class="invite-list">
      <div class="invite-item" v-for="item in rewards" :key="item.renshu">
        <div class="invite-info">
          <div class="invite-title">{{ item.title }}</div>
          <div class="invite-reward">{{ item.reward }}</div>
        </div>
        <button
          class="btn"
          :class="{ received: item.received }"
          :disabled="item.received"
          @click="receiveReward(item.renshu, item.received)"
        >
          {{ item.received ? '已领取' : '领取' }}
        </button>
      </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>
</template>

<script>
import { getShareccb, getShareccbList } from '@/api/profile';
import { getUserInfo } from '@/api/home';

export default {
  name: 'Dynamic',
  data() {
    return {
      rewards: [
        { renshu: 5, title: '邀请5人实名认证', reward: '奖励60000元红旗资产', received: false },
        { renshu: 10, title: '邀请10人实名认证', reward: '奖励150000元红旗资产', received: false },
        { renshu: 20, title: '邀请20人实名认证', reward: '奖励300000元红旗资产', received: false },
        { renshu: 50, title: '邀请50人实名认证', reward: '奖励800000元红旗资产', received: false },
        { renshu: 100, title: '邀请100人实名认证', reward: '奖励1800000元红旗资产', received: false },
        { renshu: 150, title: '邀请150人实名认证', reward: '获得国家神秘奖励', received: false }
      ],
      userInfo: {},
      showAuthDialog: false
    }
  },
  methods: {
    async getUserInfo() {
      try {
        const res = await getUserInfo();
        this.userInfo = res.data;
        // 检查是否已实名认证
        if (this.userInfo.if_real === 0) {
          this.showAuthDialog = true;
        }
      } catch (error) {
        console.error('获取用户信息失败:', error);
      }
    },
    async getShareccbList() {
      try {
        const res = await getShareccbList();
        if (res.code === 1 && Array.isArray(res.data)) {
          // 根据接口返回的received状态，更新rewards
          this.rewards = this.rewards.map(item => {
            const found = res.data.find(d => d.renshu === item.renshu);
            return found ? { ...item, received: found.received } : item;
          });
        }
      } catch (error) {
        console.error('获取奖励列表失败:', error);
      }
    },
    async receiveReward(renshu, received) {
      if (received) return;
      if (this.userInfo.if_real === 0) {
        this.showAuthDialog = true;
        return;
      }
      try {
        const res = await getShareccb(renshu);
        alert(res.msg);
        this.getShareccbList();
      } catch (e) {
        // 接口异常时不弹窗
      }
    },
    goToAuth() {
      this.$router.push('/user-center');
    }
  },
  created() {
    this.getUserInfo();
    this.getShareccbList();
  }
}
</script>

<style scoped>
.usremei-container {
  min-height: 100vh;
  background-image: url('../assets/dabag.png');
  background-size: cover;
  background-position: center;
  padding-top: 60px;
  padding-bottom: 80px;
  box-sizing: border-box;
  overflow-y: auto;
  position: relative;
}

.invite-header {
  background: #ffeddf;
  color: #f11859;
  font-size: 24px;
  font-weight: bold;
  text-align: center;
  margin: 0 auto 30px auto;
  padding: 20px 0 10px 0;
  width: 90%;
  box-shadow: 0 4px 16px rgba(208,2,27,0.08);
  letter-spacing: 2px;
}

.invite-list {
  width: 90%;
  margin: 0 auto;
}

.invite-item {
  background: #ffeddf;
  margin-bottom: 22px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 20px;
  box-shadow: 0 2px 8px rgba(208,2,27,0.06);
}

.invite-info {
  display: flex;
  flex-direction: column;
  width:70%;
  align-items: center;
}

.invite-title {
  font-size: 14px;
  color: #222;
  font-weight: 600;
  margin-bottom: 6px;
}

.invite-reward {
  font-size: 14px;
  color: #000;
}

.btn {
  min-width: 70px;
  height: 32px;
  border: none;
  border-radius: 16px;
  font-size: 15px;
  font-weight: 500;
  padding: 0 18px;
  cursor: default;
  background: #d0021b;
  color: #fff;
}

.btn.received {
  background: #bdbdbd;
  color: #fff;
}

.btn.not-reached {
  background: #d0021b;
  color: #fff;
}

.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);
  }
}
</style> 