<template>
  <view class="page-wallet">
    <!-- 自定义导航栏 -->
    <view class="custom-navbar">
      <view class="navbar-content">
        <view class="navbar-left" @tap="goBack">
          <u-icon name="arrow-left" :size="20" color="#ffffff" />
        </view>
        <text class="navbar-title">我的钱包</text>
        <view class="navbar-right"></view>
      </view>
    </view>

    <scroll-view v-if="!loading" scroll-y class="wallet-content">
      <!-- 余额卡片 -->
      <view class="balance-card">
        <view class="balance-header">
          <text class="balance-label">账户余额（元）</text>
          <view class="balance-tips" @tap="showBalanceTips">
            <u-icon name="question-circle" :size="16" color="rgba(255,255,255,0.8)" />
          </view>
        </view>
        <view class="balance-amount">
          <text class="amount-symbol">¥</text>
          <text class="amount-value">{{ formatMoney(wallet.balance) }}</text>
        </view>
        <view class="balance-detail">
          <view class="detail-item">
            <text class="detail-label">可用余额</text>
            <text class="detail-value">¥{{ formatMoney(wallet.availableBalance) }}</text>
          </view>
          <view class="detail-divider"></view>
          <view class="detail-item">
            <text class="detail-label">冻结金额</text>
            <text class="detail-value">¥{{ formatMoney(wallet.frozenBalance) }}</text>
          </view>
        </view>
        <view class="withdraw-btn" @tap="goWithdraw">
          <u-icon name="rmb-circle" :size="18" color="#ffffff" />
          <text class="btn-text">立即提现</text>
        </view>
      </view>

      <!-- 余额说明 -->
      <view class="info-card">
        <view class="info-header">
          <u-icon name="info-circle" :size="16" color="#FF6B35" />
          <text class="info-title">余额说明</text>
        </view>
        <view class="info-list">
          <text class="info-item">• 余额来源：邀请好友成功下单后获得的返现奖励</text>
          <text class="info-item">• 可用余额：可以提现到微信或支付宝的金额</text>
          <text class="info-item">• 冻结金额：提现申请中或待结算的金额</text>
          <text class="info-item">• 提现到账：1-3个工作日内到账</text>
        </view>
      </view>

      <!-- 快捷入口 -->
      <view class="quick-actions">
        <view class="action-item" @tap="goWithdrawals">
          <view class="action-icon">
            <u-icon name="list" :size="24" color="#FF6B35" />
          </view>
          <text class="action-text">提现记录</text>
        </view>
        <view class="action-item" @tap="goInvite">
          <view class="action-icon">
            <u-icon name="share" :size="24" color="#FF6B35" />
          </view>
          <text class="action-text">邀请好友</text>
        </view>
      </view>

      <!-- 底部占位 -->
      <view class="bottom-placeholder"></view>
    </scroll-view>

    <!-- 加载状态 -->
    <view v-else class="loading-state">
      <view class="loading-spinner">
        <view class="spinner-dot"></view>
        <view class="spinner-dot"></view>
        <view class="spinner-dot"></view>
      </view>
      <text class="loading-text">加载中...</text>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { walletApi } from '@/api/user/wallet';

const loading = ref(true);
const wallet = ref<any>({
  balance: 0,
  availableBalance: 0,
  frozenBalance: 0,
});

onMounted(() => {
  fetchWallet();
});

async function fetchWallet() {
  loading.value = true;
  try {
    const res = await walletApi.getWallet();
    wallet.value = res.data;
  } catch (e: any) {
    console.error('获取钱包信息失败:', e);
    uni.showToast({ title: e.message || '加载失败', icon: 'none' });
  } finally {
    loading.value = false;
  }
}

function formatMoney(value: number | string): string {
  const num = Number(value || 0);
  return num.toFixed(2);
}

function goBack() {
  uni.navigateBack();
}

function showBalanceTips() {
  uni.showModal({
    title: '余额说明',
    content: '可用余额：可以提现的金额\n冻结金额：提现申请中或待结算的金额',
    showCancel: false,
  });
}

function goWithdraw() {
  if (wallet.value.availableBalance <= 0) {
    uni.showToast({ title: '暂无可提现余额', icon: 'none' });
    return;
  }
  uni.navigateTo({ url: '/pages/wallet/withdraw' });
}

function goWithdrawals() {
  uni.navigateTo({ url: '/pages/wallet/withdrawals' });
}

function goInvite() {
  uni.navigateTo({ url: '/pages/invite/index' });
}
</script>

<style lang="scss" scoped>
.page-wallet {
  min-height: 100vh;
  background: #F5F7FA;
  display: flex;
  flex-direction: column;
}

/* 自定义导航栏 */
.custom-navbar {
  background: linear-gradient(135deg, #FF8C5A 0%, #FF6B35 100%);
  padding: 0 24rpx;
}

.navbar-content {
  height: 88rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.navbar-left,
.navbar-right {
  width: 80rpx;
  display: flex;
  align-items: center;
}

.navbar-left {
  justify-content: flex-start;
  padding: 12rpx;
  margin-left: -12rpx;
  transition: opacity 0.3s ease;

  &:active {
    opacity: 0.6;
  }
}

.navbar-title {
  flex: 1;
  text-align: center;
  font-size: 32rpx;
  font-weight: 600;
  color: #ffffff;
}

/* 钱包内容 */
.wallet-content {
  flex: 1;
  overflow-y: auto;
}

/* 余额卡片 */
.balance-card {
  margin: 24rpx 24rpx 16rpx;
  padding: 40rpx 32rpx;
  background: linear-gradient(135deg, #FF8C5A 0%, #FF6B35 100%);
  border-radius: 24rpx;
  box-shadow: 0 8rpx 24rpx rgba(255, 107, 53, 0.3);
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    top: -60rpx;
    right: -60rpx;
    width: 200rpx;
    height: 200rpx;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 50%;
  }
}

.balance-header {
  display: flex;
  align-items: center;
  gap: 8rpx;
  margin-bottom: 16rpx;
  position: relative;
  z-index: 1;
}

.balance-label {
  font-size: 26rpx;
  color: rgba(255, 255, 255, 0.9);
}

.balance-tips {
  padding: 8rpx;
  margin: -8rpx;
}

.balance-amount {
  display: flex;
  align-items: baseline;
  gap: 8rpx;
  margin-bottom: 24rpx;
  position: relative;
  z-index: 1;
}

.amount-symbol {
  font-size: 40rpx;
  font-weight: 600;
  color: #ffffff;
}

.amount-value {
  font-size: 64rpx;
  font-weight: 700;
  color: #ffffff;
  line-height: 1;
}

.balance-detail {
  display: flex;
  align-items: center;
  padding: 24rpx 0;
  border-top: 1rpx solid rgba(255, 255, 255, 0.2);
  border-bottom: 1rpx solid rgba(255, 255, 255, 0.2);
  margin-bottom: 24rpx;
  position: relative;
  z-index: 1;
}

.detail-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8rpx;
}

.detail-label {
  font-size: 24rpx;
  color: rgba(255, 255, 255, 0.8);
}

.detail-value {
  font-size: 28rpx;
  font-weight: 600;
  color: #ffffff;
}

.detail-divider {
  width: 1rpx;
  height: 40rpx;
  background: rgba(255, 255, 255, 0.2);
}

.withdraw-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8rpx;
  padding: 24rpx;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 48rpx;
  backdrop-filter: blur(10rpx);
  transition: all 0.3s ease;
  position: relative;
  z-index: 1;

  &:active {
    transform: scale(0.96);
    background: rgba(255, 255, 255, 0.3);
  }
}

.btn-text {
  font-size: 28rpx;
  font-weight: 600;
  color: #ffffff;
}

/* 信息卡片 */
.info-card {
  margin: 0 24rpx 16rpx;
  padding: 24rpx;
  background: #FFF7E6;
  border-radius: 16rpx;
}

.info-header {
  display: flex;
  align-items: center;
  gap: 8rpx;
  margin-bottom: 16rpx;
}

.info-title {
  font-size: 26rpx;
  font-weight: 600;
  color: #FF6B35;
}

.info-list {
  display: flex;
  flex-direction: column;
  gap: 8rpx;
}

.info-item {
  font-size: 24rpx;
  color: #999;
  line-height: 1.6;
}

/* 快捷入口 */
.quick-actions {
  margin: 0 24rpx 16rpx;
  padding: 24rpx;
  background: #ffffff;
  border-radius: 20rpx;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.06);
  display: flex;
  justify-content: space-around;
}

.action-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12rpx;
  padding: 16rpx;
  transition: all 0.3s ease;

  &:active {
    opacity: 0.7;
    transform: scale(0.95);
  }
}

.action-icon {
  width: 80rpx;
  height: 80rpx;
  background: linear-gradient(135deg, #FFF5F0 0%, #FFE8DD 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.action-text {
  font-size: 24rpx;
  color: #666;
}

/* 底部占位 */
.bottom-placeholder {
  height: 40rpx;
}

/* 加载状态 */
.loading-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16rpx;
  padding: 120rpx 0;
}

.loading-spinner {
  display: flex;
  align-items: center;
  gap: 12rpx;
}

.spinner-dot {
  width: 12rpx;
  height: 12rpx;
  background: #FF6B35;
  border-radius: 50%;
  animation: bounce 1.4s infinite ease-in-out both;

  &:nth-child(1) {
    animation-delay: -0.32s;
  }

  &:nth-child(2) {
    animation-delay: -0.16s;
  }
}

@keyframes bounce {
  0%, 80%, 100% {
    transform: scale(0.6);
    opacity: 0.5;
  }
  40% {
    transform: scale(1);
    opacity: 1;
  }
}

.loading-text {
  font-size: 24rpx;
  color: #999;
}
</style>
