<template>
  <div class="information-details gradient-background-circles">
    <MobileHeader title="Information details" />

    <div class="content">
      <!-- 加载状态 -->
      <div v-if="loading" class="loading-state">
        <div class="loading-spinner"></div>
        <p>Loading...</p>
      </div>

      <!-- 错误状态 -->
      <div v-else-if="error" class="error-state">
        <div class="error-icon">⚠️</div>
        <p>{{ error }}</p>
        <button @click="fetchData" class="retry-btn">Retry</button>
      </div>

      <!-- 交易记录列表 -->
      <div v-else class="transaction-list">
        <div 
          v-for="transaction in transactions" 
          :key="transaction.id"
          class="transaction-item"
        >
          <div class="transaction-content">
            <div class="transaction-info">
              <h3>{{ transaction.eventDescribe }}</h3>
              <p class="transaction-time">{{ transaction.createTime }}</p>
            </div>
            <div class="transaction-amount" :class="transaction.amount > 0 ? 'positive' : 'negative'">
              {{ transaction.amount > 0 ? '+' : '' }}{{ Math.abs(transaction.amount) }}
            </div>
          </div>
        </div>
      </div>

      <!-- 空状态 -->
      <div v-if="!loading && !error && transactions.length === 0" class="empty-state">
        <div class="empty-icon">📄</div>
        <p>No transaction records</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import MobileHeader from '../components/MobileHeader.vue'
import { getWalletDetails, formatDateTime, formatEventDescribe } from '../api/wallet.js'

const transactions = ref([])
const loading = ref(false)
const error = ref(null)

// 获取数据
const fetchData = async () => {
  loading.value = true
  error.value = null
  
  try {
    const response = await getWalletDetails()
    
    // 处理API返回的数据
    if (response.body && Array.isArray(response.body)) {
      transactions.value = response.body.map(item => ({
        id: item.id,
        eventDescribe: formatEventDescribe(item.eventDescribe),
        createTime: formatDateTime(item.createTime),
        amount: item.type === 0 ? -Math.abs(item.amount) : item.amount, // type=0表示支出，显示为负数
        balance: item.balance,
        event: item.event
      }))
    } else {
      transactions.value = []
    }
  } catch (err) {
    console.error('Failed to fetch wallet details:', err)
    error.value = err.message || 'Failed to load data'
  } finally {
    loading.value = false
  }
}

// 页面加载时获取数据
onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.information-details {
  font-family: -apple-system, BlinkMacSystemFont, sans-serif;
}

.content {
  padding: 16px;
  position: relative;
  z-index: 2;
}

/* 交易记录列表 */
.transaction-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.transaction-item {
  background-color: white;
  padding: 16px;
  border-radius: 16px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.transaction-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.transaction-info {
  flex: 1;
}

.transaction-info h3 {
  margin: 0 0 4px 0;
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.transaction-time {
  margin: 0;
  font-size: 14px;
  color: #666;
}

.transaction-amount {
  font-size: 18px;
  font-weight: 600;
  min-width: 40px;
  text-align: right;
}

.transaction-amount.positive {
  color: #10B981;
}

.transaction-amount.negative {
  color: #EF4444;
}

/* 加载状态 */
.loading-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: #666;
}

.loading-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid #f3f3f3;
  border-top: 3px solid #8B5CF6;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.loading-state p {
  margin: 0;
  font-size: 14px;
}

/* 错误状态 */
.error-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: #666;
  text-align: center;
}

.error-icon {
  font-size: 48px;
  margin-bottom: 16px;
}

.error-state p {
  margin: 0 0 20px 0;
  font-size: 14px;
  line-height: 1.5;
}

.retry-btn {
  padding: 10px 24px;
  background-color: #8B5CF6;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.retry-btn:active {
  background-color: #7C3AED;
}

/* 空状态 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: #666;
  text-align: center;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 16px;
}

.empty-state p {
  margin: 0;
  font-size: 16px;
}
</style>
