<template>
  <view class="page">
    <NavBar title="定时折扣" :showBack="true" />
    <view class="search-bar"><input class="search-input" v-model="keyword" placeholder="搜索活动名称" placeholder-class="search-ph" @confirm="handleSearch" /></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-name">{{ item.activityName }}</text><view class="card-meta"><text class="meta-type">{{ discountTypeMap[item.discountType] || '折扣' }}</text><text class="meta-time">{{ item.discountStartTime }} ~ {{ item.discountEndTime }}</text></view></view>
            <view class="card-status" :class="statusClass(item.status)"><text>{{ TimedDiscountStatusText[item.status] || '未知' }}</text></view>
          </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 class="bottom-btn" @click="goDetail(0)"><text>新建定时折扣</text></view>
  </view>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue';
import NavBar from '@/components/NavBar.vue';
import { getTimedDiscountList } from '@/api/timedDiscount';
import { TimedDiscountStatusText } from '@/utils/constants';
const list = ref<any[]>([]); const loading = ref(false); const hasMore = ref(true); const page = ref(1); const keyword = ref('');
const discountTypeMap: Record<number,string> = { 1: '定时折扣', 2: '夜间折扣', 3: '节假日折扣' };
const statusClass = (status: number) => status === 1 ? 'on' : status === 2 ? 'paused' : 'off';
const loadData = async () => { loading.value = true; try { const params: any = { page: page.value, pageSize: 10 }; if (keyword.value) params.activityName = keyword.value; const res = await getTimedDiscountList(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 (e) { console.error('加载定时折扣列表失败', e); } loading.value = false; };
const loadMore = () => { if (loading.value || !hasMore.value) return; page.value++; loadData(); };
const handleSearch = () => { page.value = 1; hasMore.value = true; list.value = []; loadData(); };
const goDetail = (id: number) => { uni.navigateTo({ url: '/pages/marketing/timed-discount-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); }
.search-bar { padding: $spacing-2 $spacing-3; background: $bg-color-card; }
.search-input { width: 100%; height: 68rpx; background: $bg-color-page; border-radius: 40rpx; padding: 0 24rpx; font-size: $font-size-base; box-sizing: border-box; }
.search-ph { color: $text-color-muted; font-size: 26rpx; }
.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-name { font-size: 30rpx; font-weight: 600; color: $text-color-primary; display: block; margin-bottom: 8rpx; }
.card-meta { display: flex; flex-direction: column; gap: 4rpx; }
.meta-type { font-size: 24rpx; color: $primary-color-dark; font-weight: 500; }
.meta-time { font-size: 22rpx; color: $text-color-muted; }
.card-status { padding: 6rpx 16rpx; border-radius: 10rpx; font-size: 22rpx; font-weight: 500; flex-shrink: 0; }
.card-status.on { background: $success-color-bg; color: $success-color; }
.card-status.paused { background: $warning-color-bg; color: $warning-color; }
.card-status.off { background: $bg-color-page; 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; }
.bottom-btn { background: $primary-color; margin: 20rpx 24rpx; padding: 28rpx 0; border-radius: $radius-xl; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.bottom-btn text { font-size: $font-size-lg; font-weight: 600; color: $text-color-primary; }
</style>
