<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-header">
            <view class="header-left">
              <view class="account-info">
                <u-icon :name="getAccountIcon(item.accountType)" :size="20" color="#FF6B35" />
                <text class="account-text">{{ getAccountLabel(item.accountType) }}</text>
              </view>
              <view :class="['status-badge', `status-${item.status}`]">
                <text class="status-text">{{ getStatusLabel(item.status) }}</text>
              </view>
            </view>
            <u-icon name="arrow-right" :size="16" color="#CCCCCC" />
          </view>

          <view class="item-amount">
            <text class="amount-value">¥{{ formatMoney(item.amount) }}</text>
          </view>

          <view class="item-info">
            <text class="info-text">{{ item.accountName }} {{ maskAccount(item.accountNumber) }}</text>
            <text class="info-time">{{ formatTime(item.createdAt) }}</text>
          </view>

          <view v-if="item.remark" class="item-remark">
            <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';

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: 'processing' },
  { label: '已完成', value: 'completed' },
  { 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.items || [];

    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: '待审核',
    processing: '处理中',
    completed: '已完成',
    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 {
  const iconMap: Record<string, string> = {
    alipay: 'pay-circle',
    wechat: 'weixin',
    bank: 'credit-card',
  };
  return iconMap[type] || 'help-circle';
}

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

function formatTime(time: string): string {
  if (!time) return '';
  const date = new Date(time);
  return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`;
}

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;
  padding: 24rpx;
  background: #ffffff;
  border-radius: 16rpx;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
  transition: all 0.3s ease;

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

.item-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16rpx;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12rpx;
  flex: 1;
}

.account-info {
  display: flex;
  align-items: center;
  gap: 8rpx;
}

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

.status-badge {
  padding: 4rpx 12rpx;
  border-radius: 12rpx;

  &.status-pending {
    background: #FFF7E6;
  }

  &.status-processing {
    background: #E6F7FF;
  }

  &.status-completed {
    background: #F6FFED;
  }

  &.status-rejected {
    background: #FFF1F0;
  }
}

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

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

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

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

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

.item-amount {
  margin-bottom: 12rpx;
}

.amount-value {
  font-size: 40rpx;
  font-weight: 700;
  color: #1A1A1A;
}

.item-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 12rpx;
  border-top: 1rpx solid #F0F0F0;
}

.info-text {
  font-size: 24rpx;
  color: #999;
}

.info-time {
  font-size: 24rpx;
  color: #999;
}

.item-remark {
  margin-top: 12rpx;
  padding: 12rpx;
  background: #FFF7E6;
  border-radius: 8rpx;
}

.remark-text {
  font-size: 24rpx;
  color: #FAAD14;
  line-height: 1.5;
}

/* 加载更多 */
.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>
