<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 v-if="showSuccessDialog" class="custom-dialog-mask">
      <div class="custom-dialog success-dialog">
        <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">
          <p>获得2000红旗资产</p>
          <p>获得2元每日现金</p>
        </div>
        <button class="dialog-btn success-btn" @click="showSuccessDialog = false">确定</button>
      </div>
    </div>

    <!-- 已签到提示弹窗 -->
    <div v-if="showAlreadySignedDialog" class="custom-dialog-mask">
      <div class="custom-dialog warning-dialog">
        <div class="dialog-icon">
          <svg t="1710400708095" class="warning-icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5311">
            <path d="M512 512m-448 0a448 448 0 1 0 896 0 448 448 0 1 0-896 0Z" fill="#FAAD14" p-id="5312"></path>
            <path d="M512 720m-48 0a48 48 0 1 0 96 0 48 48 0 1 0-96 0Z" fill="#FFFFFF" p-id="5313"></path>
            <path d="M512 608c-17.7 0-32-14.3-32-32V320c0-17.7 14.3-32 32-32s32 14.3 32 32v256c0 17.7-14.3 32-32 32z" fill="#FFFFFF" p-id="5314"></path>
          </svg>
        </div>
        <div class="dialog-title warning-title">温馨提示</div>
        <div class="dialog-content">今日已签到，请明天再来</div>
        <button class="dialog-btn warning-btn" @click="showAlreadySignedDialog = false">我知道了</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 v-if="showErrorDialog" class="custom-dialog-mask">
      <div class="custom-dialog error-dialog">
        <div class="dialog-close" @click="showErrorDialog = false">
          <i class="close-icon"></i>
        </div>
        <div class="dialog-icon">
          <svg viewBox="64 64 896 896" data-icon="close-circle" 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 64zm165.4 618.2l-66-.3L512 563.4l-99.3 118.4-66.1.3c-4.4 0-8-3.5-8-8 0-1.9.7-3.7 1.9-5.2l130.1-155L340.5 359a8.32 8.32 0 01-1.9-5.2c0-4.4 3.6-8 8-8l66.1.3L512 464.6l99.3-118.4 66-.3c4.4 0 8 3.5 8 8 0 1.9-.7 3.7-1.9 5.2L553.5 514l130 155c1.2 1.5 1.9 3.3 1.9 5.2 0 4.4-3.6 8-8 8z"></path>
          </svg>
        </div>
        <div class="dialog-title error-title">签到失败</div>
        <div class="dialog-content">{{ errorMessage }}</div>
        <button class="dialog-btn error-btn" @click="showErrorDialog = false">确定</button>
      </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,
      showSuccessDialog: false,
      showAlreadySignedDialog: false,
      showAuthDialog: false,
      showErrorDialog: false,
      errorMessage: ''
    }
  },
  computed: {
    hasSignedToday() {
      if (!this.userInfo.sign_times) return false;
      // 假设 sign_times 是秒级时间戳
      const signDate = new Date(this.userInfo.sign_times * 1000);
      const today = new Date();
      return (
        signDate.getFullYear() === today.getFullYear() &&
        signDate.getMonth() === today.getMonth() &&
        signDate.getDate() === today.getDate()
      );
    }
  },
  methods: {
    async getUserInfo() {
      try {
        const res = await getUserInfo();
        this.userInfo = res.data;
      } catch (error) {
        console.error('获取用户信息失败:', error);
      }
    },
    async handleSignIn() {
      // 先检查是否已签到
      if (this.hasSignedToday) {
        this.showAlreadySignedDialog = true;
        return;
      }

      // 检查是否实名认证
      if (this.userInfo.if_real === 0) {
        this.showAuthDialog = true;
        return;
      }

      try {
        await signIn();
        const today = new Date().toLocaleDateString();
        localStorage.setItem('lastSignDate', today);
        this.lastSignDate = today;
        
        this.showSuccessDialog = true;
        await this.getUserInfo();
      } catch (error) {
        console.error('签到失败:', error);
        this.errorMessage = '签到失败，请重试';
        this.showErrorDialog = true;
      }
    },
    goToAuth() {
      this.$router.push('/user-center');
    },
    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);
}

.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: 280px;
  background: #fff;
  border-radius: 12px;
  padding: 20px;
  text-align: center;
  animation: dialogFadeIn 0.3s ease;
  position: relative;
}

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

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

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

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

.warning-title {
  color: #FAAD14;
}

.dialog-content {
  font-size: 14px;
  color: #666;
  line-height: 1.6;
  margin-bottom: 20px;
}

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

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

.warning-btn {
  background: #FAAD14;
}

.dialog-btn:hover {
  opacity: 0.9;
}

.dialog-btn:active {
  transform: scale(0.98);
}

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

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

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

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

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

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

.auth-dialog .dialog-content {
  margin: 16px 0 24px;
  line-height: 1.8;
}

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

.error-title {
  color: #ff4d4f;
}

.error-btn {
  background: #ff4d4f;
}

.error-btn:hover {
  background: #ff7875;
}

.error-dialog {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}
</style> 