<template>
  <div class="status-card legion-signin">
    <div class="card-header">
      <img
        src="/icons/1733492491706148.png"
        alt="签到图标"
        class="status-icon"
      >
      <div class="status-info">
        <h3>俱乐部签到</h3>
        <p>每日签到状态</p>
      </div>
      <div
        class="status-badge"
        :class="{ active: legionSignin.isSignedIn }"
      >
        <div class="status-dot" />
        <span>{{ legionSignin.isSignedIn ? '已签到' : '待签到' }}</span>
      </div>
    </div>
    <div class="card-content">
      <p
        v-if="legionSignin.clubName"
        class="club-name"
      >
        当前俱乐部<br>
        <strong>{{ legionSignin.clubName }}</strong>
      </p>
      <p
        v-else
        class="description"
      >
        尚未加入任何俱乐部
      </p>
      <button 
        class="action-button"
        :disabled="legionSignin.isSignedIn"
        @click="signInLegion"
      >
        {{ legionSignin.isSignedIn ? '已签到' : '立即签到' }}
      </button>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, watch, onMounted } from 'vue'
import { useTokenStore } from '@/stores/tokenStore'
import { useMessage } from 'naive-ui'

const tokenStore = useTokenStore()
const message = useMessage()

// 响应式数据
const legionSignin = ref({
  isSignedIn: false,
  clubName: ''
})

// 计算属性
const roleInfo = computed(() => {
  return tokenStore.gameData?.roleInfo || null
})

// 更新俱乐部签到状态
const updateLegionSigninStatus = () => {
  if (!roleInfo.value) return

  const role = roleInfo.value.role

  if (role.statisticsTime) {
    const today = new Date()
    today.setHours(0, 0, 0, 0)
    const todayTimestamp = today.getTime() / 1000
    
    legionSignin.value.isSignedIn = 
      role.statisticsTime['legion:sign:in'] > todayTimestamp
  }
}

// 俱乐部签到
const signInLegion = () => {
  if (!tokenStore.selectedToken || legionSignin.value.isSignedIn) return
  
  const tokenId = tokenStore.selectedToken.id
  tokenStore.sendMessage(tokenId, 'legion_signin')
  tokenStore.sendMessage(tokenId, 'role_getroleinfo')
  
  message.info('俱乐部签到')
}

// 监听角色信息变化
watch(roleInfo, (newValue) => {
  if (newValue) {
    updateLegionSigninStatus()
  }
}, { deep: true, immediate: true })

// 生命周期
onMounted(() => {
  updateLegionSigninStatus()
  
  // 获取俱乐部信息
  if (tokenStore.selectedToken) {
    const tokenId = tokenStore.selectedToken.id
    tokenStore.sendMessage(tokenId, 'legion_getinfo')
  }
})
</script>

<style scoped lang="scss">
.status-card {
  background: var(--bg-primary);
  border-radius: var(--border-radius-xl);
  padding: var(--spacing-lg);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08), 0 1px 3px rgba(0, 0, 0, 0.02);
  transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
  position: relative;
  overflow: hidden;
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 4px;
    background: linear-gradient(90deg, var(--primary-color), var(--primary-color-light));
    opacity: 0.8;
    transition: opacity 0.3s ease;
  }
  
  &:hover {
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12), 0 2px 8px rgba(0, 0, 0, 0.04);
    transform: translateY(-4px);
    
    &::before {
      opacity: 1;
    }
  }
  
  &.legion-signin {
    &::before { background: linear-gradient(90deg, #10b981, #34d399); }
    
    &:hover {
      box-shadow: 0 12px 40px rgba(16, 185, 129, 0.2), 0 4px 12px rgba(0, 0, 0, 0.08);
      transform: translateY(-6px);
      
      &::before {
        opacity: 1;
        box-shadow: 0 0 20px rgba(16, 185, 129, 0.4);
      }
    }
    
    .status-icon {
      background: linear-gradient(135deg, rgba(16, 185, 129, 0.15), rgba(52, 211, 153, 0.1));
      box-shadow: 0 4px 12px rgba(16, 185, 129, 0.2);
      
      &:hover {
        transform: scale(1.1);
        box-shadow: 0 8px 24px rgba(16, 185, 129, 0.3);
      }
    }
    
    .status-badge.active {
      background: rgba(16, 185, 129, 0.25);
      color: #10b981;
      box-shadow: 0 0 16px rgba(16, 185, 129, 0.4);
      border: 1px solid rgba(16, 185, 129, 0.3);
      font-weight: 600;
    }
    
    .action-button {
      background: linear-gradient(135deg, #10b981, #34d399);
      box-shadow: 0 4px 12px rgba(16, 185, 129, 0.3);
      
      &:hover:not(:disabled) {
        background: linear-gradient(135deg, #12b981, #36d399);
        box-shadow: 0 6px 16px rgba(16, 185, 129, 0.4);
      }
      
      &:disabled {
        background: linear-gradient(135deg, #059669, #10b981);
        box-shadow: 0 4px 12px rgba(16, 185, 129, 0.4);
        border: 1px solid rgba(16, 185, 129, 0.3);
        font-weight: 700;
        color: #ffffff;
        cursor: not-allowed;
        opacity: 1;
      }
    }
  }
}

.card-header {
  display: flex;
  align-items: flex-start;
  gap: var(--spacing-md);
  margin-bottom: var(--spacing-lg);
  position: relative;
}

.status-icon {
  width: 32px;
  height: 32px;
  object-fit: contain;
  flex-shrink: 0;
  border-radius: var(--border-radius-lg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.05));
  backdrop-filter: blur(10px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  
  &:hover {
    transform: scale(1.05);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.15);
  }
}

.status-info {
  flex: 1;

  h3 {
    font-size: var(--font-size-md);
    font-weight: var(--font-weight-semibold);
    color: var(--text-primary);
    margin: 0 0 var(--spacing-xs) 0;
  }

  p {
    font-size: var(--font-size-sm);
    color: var(--text-secondary);
    margin: 0;
  }
}

.status-badge {
  display: flex;
  align-items: center;
  gap: var(--spacing-xs);
  padding: var(--spacing-xs) var(--spacing-sm);
  border-radius: var(--border-radius-full);
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-medium);
  background: rgba(156, 163, 175, 0.1);
  color: var(--text-secondary);

  &.active {
    background: rgba(34, 197, 94, 0.1);
    color: var(--success-color);
  }
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: currentColor;
}

.card-content {
  .description {
    color: var(--text-secondary);
    font-size: var(--font-size-sm);
    line-height: 1.5;
    margin-bottom: var(--spacing-lg);
  }

  .club-name {
    color: var(--text-secondary);
    font-size: var(--font-size-sm);
    margin-bottom: var(--spacing-lg);

    strong {
      color: var(--text-primary);
      font-weight: var(--font-weight-medium);
    }
  }
}

.action-button {
  width: 100%;
  padding: var(--spacing-sm) var(--spacing-md);
  border: none;
  border-radius: var(--border-radius-medium);
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-medium);
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
  background: linear-gradient(135deg, var(--primary-color), var(--primary-color-light));
  color: white;
  box-shadow: 0 4px 12px rgba(var(--primary-color-rgb, 24, 160, 88), 0.3);
  position: relative;
  overflow: hidden;
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
    transition: left 0.5s;
  }
  
  &:hover:not(:disabled) {
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(var(--primary-color-rgb, 24, 160, 88), 0.4);
    
    &::before {
      left: 100%;
    }
  }
  
  &:active {
    transform: translateY(0);
    box-shadow: 0 2px 8px rgba(var(--primary-color-rgb, 24, 160, 88), 0.3);
  }
  
  &:disabled {
    background: var(--bg-tertiary);
    color: var(--text-tertiary);
    cursor: not-allowed;
    transform: none;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    
    &::before {
      display: none;
    }
  }
}

@media (max-width: 768px) {
  .status-card {
    padding: var(--spacing-md);
  }

  .card-header {
    flex-direction: column;
    text-align: center;
    gap: var(--spacing-sm);
  }
}
</style>

