<template>
  <div class="information-details gradient-background-circles">
    <!-- 使用 GeneralHeader 替换 MobileHeader -->
    <GeneralHeader
      :showLanguageList="true"
      :title="t('details')"
      color="black"
      style="box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); z-index: 999"
    />

    <div class="content">
      <!-- 加载状态 -->
      <div v-if="loading" class="loading-state">
        <LoadingContent spinner-margin="0 auto 16px">{{ t('loading') }}...</LoadingContent>
      </div>

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

      <!-- 交易记录列表 -->
      <div v-else class="transaction-list">
        <div v-for="transaction in transactions" :key="transaction.id" class="transaction-item">
          <!-- 提现 -->
          <div v-if="transaction.event == 'WITHDRAW'" class="transaction-content">
            <!-- 交易信息 -->
            <div class="transaction-info">
              <div style="margin-bottom: 4px; font-weight: 700">
                {{ transaction.eventDescribe }}
              </div>
              <p style="font-size: 1em; font-weight: 500; color: rgba(0, 0, 0, 0.4)">
                {{ transaction.createTime }}
              </p>
            </div>

            <!-- 交易金额 -->
            <div
              style="margin: 0 8px; display: flex; flex-direction: column; align-items: flex-end"
            >
              <div style="font-size: 1.2em; font-weight: 500; margin-bottom: 4px; direction: ltr">
                ${{ Math.abs(transaction.amount) }}
              </div>
              <div
                style="
                  font-weight: 510;
                  background-clip: text;
                  -webkit-background-clip: text;
                  -webkit-text-fill-color: transparent;
                "
                :style="{
                  backgroundImage:
                    transaction.withdrawStatus == 'Under review'
                      ? 'linear-gradient(248deg, #FFE675 5.66%, #FFC53D 42.49%)'
                      : transaction.withdrawStatus == 'Approved'
                        ? 'linear-gradient(248deg, #75FF98 5.66%, #3DFF54 42.49%)'
                        : 'linear-gradient(248deg, #FF7578 5.66%, #FF3D40 42.49%)',
                }"
              >
                {{ t(transaction.withdrawStatus) }}
              </div>
            </div>
          </div>

          <!-- 转账和兑换 -->
          <div v-else class="transaction-content">
            <!-- 交易信息 -->
            <div class="transaction-info">
              <div style="margin-bottom: 4px; font-weight: 700">
                {{ transaction.eventDescribe }}
              </div>
              <p style="font-size: 1em; font-weight: 500; color: rgba(0, 0, 0, 0.4)">
                {{ transaction.createTime }}
              </p>
            </div>

            <!-- 交易金额 -->
            <div style="font-size: 1.2em; font-weight: 500; margin: 0 8px; direction: ltr">
              {{ 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>{{ t('no_transaction_records') }}</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useI18n } from 'vue-i18n'
import { getWalletDetails } from '@/api/wallet.js'
import { formatUTCTimeClean } from '@/utils/utcFormat.js'
import { setDocumentDirection } from '@/locales/i18n'

import GeneralHeader from '@/components/GeneralHeader.vue'

const { t, locale } = useI18n()

// 监听语言变化并设置文档方向
locale.value && setDocumentDirection(locale.value)

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) => {
        // 根据 event 字段判断是加还是减
        let amount = item.amount
        let withdrawStatus = '' // 提现状态

        // 根据事件类型判断显示
        switch (item.event) {
          case 'RECEIVE_TRANSFER':
            // 接收转账 - 显示为正数（绿色）
            amount = Math.abs(item.amount)
            break
          case 'TRANSFER':
            // 转账支出 - 显示为负数（红色）
            amount = -Math.abs(item.amount)
            break
          case 'WITHDRAW':
            // 其他情况根据 type 字段处理
            // type: 0 表示收入，1 表示支出
            if (item.type === 0) {
              amount = Math.abs(item.amount)
            } else {
              amount = -Math.abs(item.amount)
            }

            if (item.details.latestApprovalStatus == 'SUBMIT') {
              withdrawStatus = 'under_review'
            } else if (item.details.latestApprovalStatus == 'PASS') {
              withdrawStatus = 'approved'
            } else if (item.details.latestApprovalStatus == 'NOT_PASS') {
              withdrawStatus = 'rejection'
            }
            break
          default:
            break
        }

        return {
          id: item.id,
          eventDescribe: item.eventDescribe,
          createTime: formatUTCTimeClean(item.createTime),
          amount: amount,
          balance: item.balance,
          event: item.event,
          withdrawStatus: withdrawStatus, // 提现状态
          // 添加用户信息字段
          userAvatar: item.details?.userAvatar || null,
          userNickname: item.details?.userNickname || null,
          account: item.details?.account || null,
        }
      })
    } else {
      transactions.value = []
    }
  } catch (err) {
    console.error('Failed to fetch wallet details:', err)
  } finally {
    loading.value = false
  }
}

// 头像占位符
const getAvatarPlaceholder = (nickname) => {
  if (!nickname) return 'U'
  return nickname.charAt(0).toUpperCase()
}

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

<style scoped>
* {
  color: rgba(0, 0, 0, 0.8);
}

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

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

.transaction-item {
  background-color: white;
  padding: 12px;
  border-radius: 12px;
  box-shadow: 0 0 4px 0 rgba(0, 0, 0, 0.25);
}

.transaction-content {
  display: flex;
  align-items: center;
  gap: 12px;
}

/* 用户头像 */
.user-avatar-container {
  flex-shrink: 0;
}

.user-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  object-fit: cover;
}

.avatar-placeholder {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background-color: #8b5cf6;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 18px;
  font-weight: 600;
}

.transaction-info {
  flex: 1;
  min-width: 0; /* 为了支持text overflow */
}

/* 用户信息 */
.user-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-bottom: 4px;
}

.user-nickname {
  font-size: 14px;
  font-weight: 500;
  color: #333;
}

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

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

/* 响应式设计 */
@media (max-width: 480px) {
  .transaction-content {
    gap: 10px;
  }

  .user-avatar,
  .avatar-placeholder {
    width: 40px;
    height: 40px;
    font-size: 16px;
  }

  .user-nickname {
    font-size: 13px;
  }
}

.user-account {
  font-size: 0.9em;
}

.transaction-time {
  font-size: 1em;
}

.transaction-amount {
  font-size: 1.2em;
}

@media screen and (max-width: 360px) {
  * {
    font-size: 10px;
  }
}

@media screen and (min-width: 360px) {
  * {
    font-size: 12px;
  }
}

@media screen and (min-width: 768px) {
  * {
    font-size: 24px;
  }
}

/* RTL支持 */
[dir='rtl'] .transaction-content {
  /* flex-direction: row-reverse; */
}

[dir='rtl'] .transaction-info {
  text-align: right;
}
</style>
