<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="overview-card">
        <view class="card-header">
          <text class="card-title">数据概览</text>
        </view>
        <view class="overview-tabs">
          <view
            v-for="tab in ['today', 'week', 'month']"
            :key="tab"
            :class="['tab-item', { active: activeTab === tab }]"
            @tap="activeTab = tab"
          >
            <text class="tab-text">{{ tabLabels[tab] }}</text>
          </view>
        </view>
        <view class="overview-stats">
          <view class="stat-item">
            <text class="stat-value">{{ overview[activeTab]?.orderCount || 0 }}</text>
            <text class="stat-label">订单数</text>
          </view>
          <view class="stat-divider"></view>
          <view class="stat-item">
            <text class="stat-value">¥{{ formatMoney(overview[activeTab]?.totalIncome || 0) }}</text>
            <text class="stat-label">订单金额</text>
          </view>
          <view class="stat-divider"></view>
          <view class="stat-item">
            <text class="stat-value highlight">¥{{ formatMoney(overview[activeTab]?.actualIncome || 0) }}</text>
            <text class="stat-label">实际收入</text>
          </view>
        </view>
      </view>

      <!-- 快捷入口 -->
      <view class="quick-actions">
        <view class="action-item" @tap="goSettlements">
          <view class="action-icon">
            <u-icon name="file-text" :size="24" color="#FF6B35" />
          </view>
          <text class="action-text">结算记录</text>
        </view>
        <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="goTransactions">
          <view class="action-icon">
            <u-icon name="order" :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 { financeApi, statisticsApi } from '@/api/seller/finance';

const loading = ref(true);
const wallet = ref<any>({
  balance: 0,
  availableBalance: 0,
  frozenBalance: 0,
});
const overview = ref<any>({
  today: null,
  week: null,
  month: null,
});
const activeTab = ref('today');

const tabLabels: Record<string, string> = {
  today: '今日',
  week: '本周',
  month: '本月',
};

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

async function fetchData() {
  loading.value = true;
  try {
    const [walletRes, overviewRes] = await Promise.all([
      financeApi.getWallet(),
      statisticsApi.getOverview(),
    ]);
    wallet.value = walletRes.data;
    overview.value = overviewRes.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() {
  uni.navigateTo({ url: '/pages/seller/withdraw' });
}

function goSettlements() {
  uni.navigateTo({ url: '/pages/seller/settlements' });
}

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

function goTransactions() {
  uni.navigateTo({ url: '/pages/seller/transactions' });
}
</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;
}

/* 数据概览卡片 */
.overview-card {
  margin: 0 24rpx 16rpx;
  padding: 24rpx;
  background: #ffffff;
  border-radius: 20rpx;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.06);
}

.card-header {
  margin-bottom: 20rpx;
}

.card-title {
  font-size: 30rpx;
  font-weight: 600;
  color: #1A1A1A;
}

.overview-tabs {
  display: flex;
  gap: 16rpx;
  margin-bottom: 24rpx;
}

.tab-item {
  flex: 1;
  padding: 16rpx;
  text-align: center;
  background: #F5F7FA;
  border-radius: 12rpx;
  transition: all 0.3s ease;

  &.active {
    background: linear-gradient(135deg, #FFF5F0 0%, #FFE8DD 100%);
  }
}

.tab-text {
  font-size: 26rpx;
  color: #666;

  .active & {
    color: #FF6B35;
    font-weight: 600;
  }
}

.overview-stats {
  display: flex;
  align-items: center;
  padding: 24rpx 0;
  background: linear-gradient(135deg, #FFFBF8 0%, #FFF5F0 100%);
  border-radius: 16rpx;
}

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

.stat-value {
  font-size: 32rpx;
  font-weight: 700;
  color: #1A1A1A;

  &.highlight {
    color: #FF6B35;
  }
}

.stat-label {
  font-size: 24rpx;
  color: #999;
}

.stat-divider {
  width: 1rpx;
  height: 60rpx;
  background: #FFE8DD;
}

/* 快捷入口 */
.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>
