<template>
  <div class="signin-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="sign_bg">
        <div class="sign_content_box">
          <div class="sign_content">
            <!-- 签到信息 -->
            <div class="sign_content_title" style="font-size: 20px;">
              已签到<span class="sign_content_title_span days-count">{{ userInfo.sign_days || 0 }}</span>天
            </div>
            <div class="sign_content_title">
              注册系统自动赠送<span class="sign_content_title_span">10000</span>红旗资产
            </div>
            <div class="sign_content_title">
              实名系统自动赠送<span class="sign_content_title_span">18000</span>红旗资产
            </div>
            <div class="sign_content_title">
              每日签到送<span class="sign_content_title_span">2000</span>红旗资产+<span class="sign_content_title_span">2</span>元每日现金
            </div>
            <!-- <div class="sign_content_title">
              累计签到<span class="sign_content_title_span">15</span>天额外获得<span class="sign_content_title_span">58000</span>红旗资产
            </div>
            <div class="sign_content_title">
              累计签到<span class="sign_content_title_span">30</span>天额外获得<span class="sign_content_title_span">88000</span>红旗资产
            </div> -->

            <!-- 签到按钮 -->
            <div 
              class="sign-btn" 
              :class="{ 'signed': hasSignedToday }"
              @click="handleSignIn"
            >
              {{ hasSignedToday ? '今日已签到' : '签 到' }}
            </div>

            <!-- 分割线 -->
            <div class="divider">
              <div class="divider-line"></div>
              <div class="divider-text">邀请好友获取更多奖励</div>
              <div class="divider-line"></div>
            </div>

            <!-- 邀请按钮 -->
            <div class="invite-section">
              <div class="invite-btn" @click="$router.push('/share')">邀请好友</div>
            </div>

            <!-- 底部间隔 -->
            <div class="bottom-gap"></div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getUserInfo } from '@/api/home';
import { signIn } from '@/api/profile';

export default {
  name: 'Signin',
  data() {
    return {
      userInfo: {},
      dailyBonus: 2800,
      dailyCoins: 10,
      lastSignDate: null
    }
  },
  computed: {
    hasSignedToday() {
      // 如果没有最后签到日期，说明还没签到过
      if (!this.lastSignDate) return false;
      
      const today = new Date().toLocaleDateString();
      return today === this.lastSignDate;
    }
  },
  methods: {
    async getUserInfo() {
      try {
        const res = await getUserInfo();
        this.userInfo = res.data;
        // 获取本地存储的最后签到日期
        this.lastSignDate = localStorage.getItem('lastSignDate');
      } catch (error) {
        console.error('获取用户信息失败:', error);
      }
    },
    async handleSignIn() {
      // 如果今天已经签到，就不再调用接口
      if (this.hasSignedToday) {
        alert('今日已签到，请明天再来');
        return;
      }

      try {
        await signIn();
        // 保存签到日期到本地存储
        const today = new Date().toLocaleDateString();
        localStorage.setItem('lastSignDate', today);
        this.lastSignDate = today;
        
        alert('签到成功');
        // 刷新用户信息以更新签到天数
        await this.getUserInfo();
      } catch (error) {
        console.error('签到失败:', error);
        alert('签到失败，请重试');
      }
    },
    handleInvite() {
      // 邀请处理逻辑
    }
  },
  created() {
    this.getUserInfo();
  }
}
</script>

<style scoped>
.signin-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: 100vh;
}

.sign_bg {
  padding: 20px;
  padding-top: 100px;
}

.sign_content_box {
  background: #fff;
  border-radius: 12px;
  overflow: hidden;
}

.sign_content {
  padding: 20px;
}

.sign_content_title {
  font-size: 16px;
  color: #333;
  margin-bottom: 15px;
  /* text-align: center; */
  line-height: 1.5;
}

.sign_content_title_span {
  color: #ff5722 !important;
  font-size: 18px;
  padding-left: 10px;
  padding-right: 10px;
  font-weight: 700;
  background: rgba(111, 101, 243, .10980392156862745);
  border-radius: 10px;
  display: inline-block;
  margin: 0 3px;
}

/* 特殊处理已签到天数的样式，保持原来的大小 */
.sign_content_title_span.days-count {
  font-size: 24px;
  font-weight: 900;
}

/* 确保抢福利文本和数字样式一致 */
.sign_content_title_span.reward-text {
  font-size: 18px;
}

.sign-btn {
  width: 80%;
  height: 30px;
  line-height: 30px;
  background: #ed4b39;
  color: #fff;
  text-align: center;
  border-radius: 4px;
  margin: 30px auto 0;
  cursor: pointer;
  transition: all 0.3s;
}

.sign-btn.signed {
  background: #999;
  cursor: not-allowed;
}

.divider {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 30px 0;
}

.divider-line {
  width: 100px;
  height: 1px;
  background: rgba(129, 134, 167, 0.35);
}

.divider-text {
  color: rgb(129, 134, 167);
  font-size: 13px;
  margin: 0 15px;
}

.invite-section {
  margin-top: 29px;
}

.invite-btn {
  width: 80%;
  height: 30px;
  line-height: 30px;
  background: #4a90e2;
  color: #fff;
  text-align: center;
  border-radius: 4px;
  margin: 20px auto 0;
  cursor: pointer;
}

.bottom-gap {
  height: 40px;
}

/* 按钮点击效果 */
.sign-btn:active,
.invite-btn:active {
  opacity: 0.9;
  transform: scale(0.98);
}
</style> 