<template>
  <view class="page">
    <NavBar title="退款管理" :showBack="true" />
    <view class="tab-bar">
      <view v-for="tab in tabs" :key="tab.key" class="tab-item" :class="{ active: currentTab === tab.key }" @click="changeTab(tab.key)">
        <text>{{ tab.label }}</text>
        <view class="tab-line" v-if="currentTab === tab.key"></view>
      </view>
    </view>
    <scroll-view class="list-scroll" scroll-y @scrolltolower="loadMore">
      <view class="card-set">
        <view class="card" v-for="item in list" :key="item.id" @click="goDetail(item.id)">
          <view class="card-head">
            <view class="card-left">
              <text class="card-no">{{ item.orderNo }}</text>
              <text class="card-time">{{ item.createTime }}</text>
            </view>
            <view class="card-amount">{{ formatMoney(item.refundAmount || 0) }}</view>
            <view class="card-status" :class="statusClass(item.status)">
              <text>{{ RefundStatusText[item.status] || '未知' }}</text>
            </view>
          </view>
          <view class="card-foot">
            <text class="foot-reason">原因: {{ item.reason || '-' }}</text>
          </view>
        </view>
      </view>
      <view class="load-tip" v-if="loading"><view class="dot-row"><view class="pulse-dot"></view><view class="pulse-dot"></view><view class="pulse-dot"></view></view></view>
      <view class="end-tip" v-if="!hasMore && list.length > 0"><text>没有更多了</text></view>
      <view class="empty-state" v-if="!loading && list.length === 0"><text class="empty-text">暂无退款数据</text></view>
    </scroll-view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import NavBar from '@/components/NavBar.vue';
import { getRefundList } from '@/api/refund';
import { RefundStatusText, DEFAULT_PAGE_SIZE } from '@/utils/constants';
import { formatMoney } from '@/utils/common';

const tabs = [
  { key: '', label: '全部' },
  { key: '0', label: '待审核' },
  { key: '1', label: '已通过' },
  { key: '2', label: '已驳回' }
];
const currentTab = ref('');
const list = ref<any[]>([]);
const loading = ref(false);
const hasMore = ref(true);
const page = ref(1);
const pageSize = DEFAULT_PAGE_SIZE;

const statusClass = (status: number) => {
  if (status === 0) return 'pending';
  if (status === 1) return 'approved';
  return 'rejected';
};

const loadData = async () => {
  loading.value = true;
  try {
    const params: any = { page: page.value, pageSize };
    if (currentTab.value) params.status = Number(currentTab.value);
    const res = await getRefundList(params);
    if (page.value === 1) list.value = res.list || [];
    else list.value = [...list.value, ...(res.list || [])];
    hasMore.value = list.value.length < (res.total || 0);
  } catch { }
  loading.value = false;
};

const loadMore = () => { if (loading.value || !hasMore.value) return; page.value++; loadData(); };
const changeTab = (key: string) => { currentTab.value = key; page.value = 1; hasMore.value = true; list.value = []; loadData(); };
const goDetail = (id: number) => { uni.navigateTo({ url: '/pages/orders/refund-detail?id=' + id }); };
onMounted(() => loadData());
</script>

<style lang="scss" scoped>
.page { height: 100vh; background: $bg-color-page; display: flex; flex-direction: column; padding-bottom: env(safe-area-inset-bottom); }
.tab-bar { display: flex; background: $bg-color-card; border-bottom: 1rpx solid $border-color-light; }
.tab-bar .tab-item { flex: 1; display: flex; flex-direction: column; align-items: center; padding: $spacing-3 0 $spacing-2; position: relative; }
.tab-bar .tab-item text { font-size: $font-size-base; color: $text-color-secondary; }
.tab-bar .tab-item.active text { color: $text-color-primary; font-weight: 600; }
.tab-bar .tab-item .tab-line { position: absolute; bottom: 0; left: 50%; transform: translateX(-50%); width: 60rpx; height: 4rpx; background: $primary-color; border-radius: 2rpx; }
.list-scroll { flex: 1; height: 0; }
.card-set { padding: 20rpx 24rpx; display: flex; flex-direction: column; gap: 16rpx; }
.card { background: $bg-color-card; border-radius: $radius-md; padding: 24rpx; box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.05); }
.card-head { display: flex; justify-content: space-between; align-items: flex-start; }
.card-left { flex: 1; min-width: 0; }
.card-no { font-size: 28rpx; font-weight: 600; color: $text-color-primary; display: block; margin-bottom: 6rpx; }
.card-time { font-size: 24rpx; color: $text-color-muted; display: block; }
.card-amount { font-size: 32rpx; font-weight: 700; color: $primary-color-dark; margin: 0 20rpx; }
.card-status { padding: 4rpx 14rpx; border-radius: 8rpx; font-size: 22rpx; font-weight: 500; flex-shrink: 0; }
.card-status.pending { background: $warning-color-bg; color: $warning-color; }
.card-status.approved { background: $success-color-bg; color: $success-color; }
.card-status.rejected { background: $error-color-bg; color: $error-color; }
.card-foot { margin-top: 14rpx; padding-top: 14rpx; border-top: 1rpx solid $border-color-light; }
.foot-reason { font-size: 24rpx; color: $text-color-muted; }
.load-tip { display: flex; justify-content: center; padding: 36rpx 0; }
.dot-row { display: flex; gap: 8rpx; }
.pulse-dot { width: 9rpx; height: 9rpx; background: $text-color-placeholder; border-radius: 50%; animation: pulse 1.2s ease-in-out infinite; }
.pulse-dot:nth-child(2) { animation-delay: 0.2s; } .pulse-dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes pulse { 0%,80%,100% { transform: scale(0.5); opacity: 0.4; } 40% { transform: scale(1); opacity: 1; } }
.end-tip { text-align: center; padding: 36rpx; font-size: 24rpx; color: $text-color-muted; }
.empty-state { display: flex; align-items: center; justify-content: center; padding: 100rpx 0; }
.empty-text { font-size: 28rpx; color: $text-color-muted; }
</style>
