<template>
  <view class="page-withdrawals">
    <!-- 导航栏 -->
    <view class="navbar">
      <view class="navbar-left" @tap="goBack">
        <u-icon name="arrow-left" :size="20" color="#1A1A1A" />
      </view>
      <text class="navbar-title">提现记录</text>
      <view class="navbar-right"></view>
    </view>

    <!-- 筛选栏 -->
    <view class="filter-bar">
      <view
        v-for="status in statusList"
        :key="status.value"
        :class="['filter-item', { active: filterStatus === status.value }]"
        @tap="filterStatus = status.value; fetchWithdrawals()"
      >
        <text class="filter-text">{{ status.label }}</text>
      </view>
    </view>

    <!-- 提现列表 -->
    <scroll-view
      v-if="!loading"
      scroll-y
      class="withdrawals-list"
      @scrolltolower="loadMore"
    >
      <view v-if="withdrawals.length > 0" class="list-content">
        <view
          v-for="item in withdrawals"
          :key="item.id"
          class="withdrawal-item"
          @tap="goDetail(item.id)"
        >
          <!-- 顶部：金额和状态 -->
          <view class="item-top">
            <view class="amount-section">
              <text class="amount-label">提现金额</text>
              <text class="amount-value">¥{{ formatMoney(item.amount) }}</text>
            </view>
            <view class="top-right">
              <view :class="['status-badge', `status-${item.status}`]">
                <text class="status-text">{{ getStatusLabel(item.status) }}</text>
              </view>
              <u-icon name="arrow-right" :size="16" color="#CCCCCC" />
            </view>
          </view>

          <!-- 中间：账户信息 -->
          <view class="item-middle">
            <view v-if="item.accountType" class="info-row">
              <text class="info-label">提现方式</text>
              <view class="info-value-row">
                <u-icon v-if="getAccountIcon(item.accountType)" :name="getAccountIcon(item.accountType)" :size="16" color="#FF6B35" />
                <text class="info-value">{{ getAccountLabel(item.accountType) }}</text>
              </view>
            </view>
            <view v-if="item.accountName || item.accountNumber" class="info-row">
              <text class="info-label">账户信息</text>
              <text class="info-value">{{ item.accountName }} {{ maskAccount(item.accountNumber) }}</text>
            </view>
            <view class="info-row">
              <text class="info-label">申请时间</text>
              <text class="info-value">{{ formatTime(item.createdAt) }}</text>
            </view>
          </view>

          <!-- 底部：备注 -->
          <view v-if="item.remark" class="item-bottom">
            <view class="remark-label">
              <u-icon name="info-circle" :size="14" color="#FF6B35" />
              <text class="remark-label-text">备注</text>
            </view>
            <text class="remark-text">{{ item.remark }}</text>
          </view>
        </view>

        <!-- 加载更多 -->
        <view v-if="hasMore" class="load-more">
          <text class="load-more-text">加载更多...</text>
        </view>
        <view v-else-if="withdrawals.length > 0" class="no-more">
          <text class="no-more-text">没有更多了</text>
        </view>
      </view>

      <!-- 空状态 -->
      <view v-else class="empty-state">
        <view class="empty-icon">
          <u-icon name="list" :size="80" color="#D8D8D8" />
        </view>
        <text class="empty-text">暂无提现记录</text>
      </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 } from '@/api/seller/finance';
import { formatDateTime } from '@/utils/date';

const loading = ref(true);
const withdrawals = ref<any[]>([]);
const filterStatus = ref('');
const page = ref(1);
const pageSize = 20;
const hasMore = ref(true);

const statusList = [
  { label: '全部', value: '' },
  { label: '待审核', value: 'pending' },
  { label: '已通过', value: 'approved' },
  { label: '已打款', value: 'paid' },
  { label: '已拒绝', value: 'rejected' },
];

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

async function fetchWithdrawals(reset = true) {
  if (reset) {
    page.value = 1;
    withdrawals.value = [];
    hasMore.value = true;
  }

  loading.value = reset;

  try {
    const params: any = {
      page: page.value,
      pageSize,
    };
    if (filterStatus.value) {
      params.status = filterStatus.value;
    }

    const res = await financeApi.getWithdrawals(params);
    const newData = res.data.list || [];

    if (reset) {
      withdrawals.value = newData;
    } else {
      withdrawals.value = [...withdrawals.value, ...newData];
    }

    hasMore.value = newData.length >= pageSize;
  } catch (e: any) {
    console.error('获取提现记录失败:', e);
    uni.showToast({ title: e.message || '加载失败', icon: 'none' });
  } finally {
    loading.value = false;
  }
}

function loadMore() {
  if (!hasMore.value || loading.value) return;
  page.value++;
  fetchWithdrawals(false);
}

function getStatusLabel(status: string): string {
  const statusMap: Record<string, string> = {
    pending: '待审核',
    approved: '已通过',
    paid: '已打款',
    rejected: '已拒绝',
  };
  return statusMap[status] || status;
}

function getAccountLabel(type: string): string {
  const typeMap: Record<string, string> = {
    alipay: '支付宝',
    wechat: '微信',
    bank: '银行卡',
  };
  return typeMap[type] || type;
}

function getAccountIcon(type: string): string | null {
  const iconMap: Record<string, string> = {
    alipay: 'pay-circle',
    wechat: 'weixin',
    bank: 'credit-card',
  };
  return iconMap[type] || null;
}

function maskAccount(account: string): string {
  if (!account || account.length <= 8) return account;
  const start = account.slice(0, 4);
  const end = account.slice(-4);
  return `${start}****${end}`;
}

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

const formatTime = formatDateTime;

function goDetail(id: number) {
  // 提现详情页面可以后续添加
  console.log('查看提现详情:', id);
}

function goBack() {
  uni.navigateBack();
}
</script>

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

/* 导航栏 */
.navbar {
  background: #ffffff;
  padding: 0 24rpx;
  border-bottom: 1rpx solid #F0F0F0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 88rpx;
}

.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: #1A1A1A;
}

/* 筛选栏 */
.filter-bar {
  background: #ffffff;
  padding: 16rpx 24rpx;
  border-bottom: 1rpx solid #F0F0F0;
  display: flex;
  gap: 12rpx;
  overflow-x: auto;
}

.filter-item {
  flex-shrink: 0;
  padding: 12rpx 24rpx;
  background: #F5F7FA;
  border-radius: 32rpx;
  transition: all 0.3s ease;

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

  &:active {
    opacity: 0.7;
  }
}

.filter-text {
  font-size: 26rpx;
  color: #666;
  white-space: nowrap;

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

/* 提现列表 */
.withdrawals-list {
  flex: 1;
  overflow-y: auto;
}

.list-content {
  padding: 16rpx 0;
}

.withdrawal-item {
  margin: 0 24rpx 16rpx;
  background: #ffffff;
  border-radius: 16rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 16rpx rgba(0, 0, 0, 0.06);
  transition: all 0.3s ease;

  &:active {
    transform: scale(0.98);
    box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.08);
  }
}

/* 顶部区域 */
.item-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 24rpx 24rpx 20rpx;
  background: linear-gradient(135deg, #FFF8F5 0%, #FFFFFF 100%);
  border-bottom: 1rpx solid #F5F5F5;
}

.amount-section {
  display: flex;
  flex-direction: column;
  gap: 8rpx;
}

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

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

.top-right {
  display: flex;
  align-items: center;
  gap: 8rpx;
}

.status-badge {
  padding: 8rpx 16rpx;
  border-radius: 20rpx;
  font-weight: 500;

  &.status-pending {
    background: linear-gradient(135deg, #FFF7E6 0%, #FFE7BA 100%);
  }

  &.status-approved {
    background: linear-gradient(135deg, #E6F7FF 0%, #BAE7FF 100%);
  }

  &.status-paid {
    background: linear-gradient(135deg, #F6FFED 0%, #D9F7BE 100%);
  }

  &.status-rejected {
    background: linear-gradient(135deg, #FFF1F0 0%, #FFCCC7 100%);
  }
}

.status-text {
  font-size: 24rpx;

  .status-pending & {
    color: #FA8C16;
  }

  .status-approved & {
    color: #1890FF;
  }

  .status-paid & {
    color: #52C41A;
  }

  .status-rejected & {
    color: #FF4D4F;
  }
}

/* 中间区域 */
.item-middle {
  padding: 20rpx 24rpx;
  display: flex;
  flex-direction: column;
  gap: 16rpx;
}

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

.info-label {
  font-size: 26rpx;
  color: #666;
  min-width: 140rpx;
}

.info-value-row {
  display: flex;
  align-items: center;
  gap: 6rpx;
}

.info-value {
  font-size: 26rpx;
  color: #1A1A1A;
  font-weight: 500;
}

/* 底部区域 */
.item-bottom {
  padding: 16rpx 24rpx 20rpx;
  background: #FFFBF5;
  border-top: 1rpx solid #FFE7BA;
}

.remark-label {
  display: flex;
  align-items: center;
  gap: 6rpx;
  margin-bottom: 8rpx;
}

.remark-label-text {
  font-size: 24rpx;
  color: #FF6B35;
  font-weight: 500;
}

.remark-text {
  font-size: 24rpx;
  color: #666;
  line-height: 1.6;
  padding-left: 20rpx;
}

/* 加载更多 */
.load-more,
.no-more {
  padding: 32rpx;
  text-align: center;
}

.load-more-text,
.no-more-text {
  font-size: 24rpx;
  color: #999;
}

/* 空状态 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 120rpx 0;
  gap: 24rpx;
}

.empty-icon {
  opacity: 0.3;
}

.empty-text {
  font-size: 28rpx;
  color: #999;
}

/* 加载状态 */
.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>
