<template>
  <view class="page-settlements">
    <!-- 导航栏 -->
    <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; fetchSettlements()"
      >
        <text class="filter-text">{{ status.label }}</text>
      </view>
    </view>

    <!-- 结算列表 -->
    <scroll-view
      v-if="!loading"
      scroll-y
      class="settlements-list"
      @scrolltolower="loadMore"
    >
      <view v-if="settlements.length > 0" class="list-content">
        <view
          v-for="item in settlements"
          :key="item.id"
          class="settlement-item"
          @tap="goDetail(item.id)"
        >
          <view class="item-header">
            <view class="header-left">
              <text class="period-text">{{ formatPeriod(item.startDate, item.endDate) }}</text>
              <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-stats">
            <view class="stat-item">
              <text class="stat-label">订单数</text>
              <text class="stat-value">{{ item.orderCount }}</text>
            </view>
            <view class="stat-item">
              <text class="stat-label">订单总额</text>
              <text class="stat-value">¥{{ formatMoney(item.totalAmount) }}</text>
            </view>
            <view class="stat-item">
              <text class="stat-label">服务费</text>
              <text class="stat-value fee">-¥{{ formatMoney(item.serviceFee) }}</text>
            </view>
          </view>

          <view class="item-footer">
            <text class="footer-label">结算金额</text>
            <text class="footer-amount">¥{{ formatMoney(item.settlementAmount) }}</text>
          </view>
        </view>

        <!-- 加载更多 -->
        <view v-if="hasMore" class="load-more">
          <text class="load-more-text">加载更多...</text>
        </view>
        <view v-else-if="settlements.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="file-text" :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 settlements = 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: 'completed' },
];

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

async function fetchSettlements(reset = true) {
  if (reset) {
    page.value = 1;
    settlements.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.getSettlements(params);
    const newData = res.data.items || [];

    if (reset) {
      settlements.value = newData;
    } else {
      settlements.value = [...settlements.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++;
  fetchSettlements(false);
}

function getStatusLabel(status: string): string {
  const statusMap: Record<string, string> = {
    pending: '待结算',
    completed: '已结算',
  };
  return statusMap[status] || status;
}

function formatPeriod(startDate: string, endDate: string): string {
  if (!startDate || !endDate) return '';
  const start = new Date(startDate);
  const end = new Date(endDate);
  return `${start.getMonth() + 1}/${start.getDate()} - ${end.getMonth() + 1}/${end.getDate()}`;
}

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

function goDetail(id: number) {
  uni.navigateTo({ url: `/pages/seller/settlement-detail?id=${id}` });
}

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

<style lang="scss" scoped>
.page-settlements {
  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: 16rpx;
}

.filter-item {
  flex: 1;
  padding: 12rpx 24rpx;
  text-align: center;
  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;

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

/* 结算列表 */
.settlements-list {
  flex: 1;
  overflow-y: auto;
}

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

.settlement-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: 20rpx;
}

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

.period-text {
  font-size: 28rpx;
  font-weight: 600;
  color: #1A1A1A;
}

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

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

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

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

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

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

.item-stats {
  display: flex;
  padding: 20rpx 0;
  border-top: 1rpx solid #F0F0F0;
  border-bottom: 1rpx solid #F0F0F0;
  margin-bottom: 20rpx;
}

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

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

.stat-value {
  font-size: 28rpx;
  font-weight: 600;
  color: #1A1A1A;

  &.fee {
    color: #FF6B35;
  }
}

.item-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.footer-label {
  font-size: 26rpx;
  color: #666;
}

.footer-amount {
  font-size: 32rpx;
  font-weight: 700;
  color: #FF6B35;
}

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