<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">
      <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 v-if="showSuccessDialog" class="custom-dialog-mask">
      <div class="custom-dialog success-dialog">
        <div class="dialog-header">
          <div class="dialog-title">签到成功</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">
          <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-header">
          <div class="dialog-title">温馨提示</div>
        </div>
        <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-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-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="showErrorDialog" class="custom-dialog-mask">
      <div class="custom-dialog error-dialog">
        <div class="dialog-header">
          <div class="dialog-title">签到失败</div>
          <div class="dialog-close" @click="showErrorDialog = false">
            <i class="close-icon"></i>
          </div>
        </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-content">{{ errorMessage }}</div>
        <button class="dialog-btn error-btn" @click="showErrorDialog = false">确定</button>
      </div>
    </div>

    <!-- Toast 组件 -->
    <Toast ref="toast" />
  </div>
</template>

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

export default {
  name: 'Signin',
  components: {
    Toast
  },
  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: 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);
}

.sign_content_box {
  background: #fff;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.sign_content {
  padding: 30px 20px;
}

.sign_content_title {
  font-size: 16px;
  color: #374151;
  margin-bottom: 16px;
  line-height: 1.6;
}

.sign_content_title_span {
  color: #2f9580 !important;
  font-size: 18px;
  padding: 4px 8px;
  font-weight: 700;
  background: rgba(47, 149, 128, 0.1);
  border-radius: 8px;
  display: inline-block;
  margin: 0 3px;
}

.sign_content_title_span.days-count {
  font-size: 24px;
  font-weight: 900;
  background: rgba(47, 149, 128, 0.15);
  padding: 6px 12px;
  border-radius: 12px;
}

.sign-btn {
  width: 100%;
  height: 50px;
  line-height: 50px;
  background: #2f9580;
  color: #fff;
  text-align: center;
  border-radius: 12px;
  margin: 30px auto 0;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 16px;
  font-weight: 600;
  box-shadow: 0 4px 12px rgba(47, 149, 128, 0.3);
}

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

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

.sign-btn.signed {
  background: #9ca3af;
  cursor: not-allowed;
  box-shadow: none;
}

.sign-btn.signed:hover {
  background: #9ca3af;
  transform: none;
  box-shadow: none;
}

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

.divider-line {
  flex: 1;
  height: 1px;
  background: rgba(156, 163, 175, 0.3);
}

.divider-text {
  color: #6b7280;
  font-size: 14px;
  margin: 0 15px;
}

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

.invite-btn {
  width: 100%;
  height: 48px;
  line-height: 48px;
  background: #4fb3a1;
  color: #fff;
  text-align: center;
  border-radius: 12px;
  cursor: pointer;
  font-size: 15px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.invite-btn:hover {
  background: #3a9b8a;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(79, 179, 161, 0.3);
}

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

.bottom-gap {
  height: 20px;
}

.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,
.warning-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;
}

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

.warning-btn {
  background: #FAAD14;
}

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

.error-btn {
  background: #ff4d4f;
}

.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;
  }
  
  .sign_content {
    padding: 24px 16px;
  }
  
  .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> 