<template>
  <view class="red_packet_list">
    <view v-for="(packet, index) in redpackets" :key="packet.id" class="red_packet_item" :class="{ disabled: packet.status !== 0 }" @click="handleClick(packet)">
      <img src="https://kailin-mp.oss-cn-shenzhen.aliyuncs.com/static/icon/packet_list_backerground.png" alt="" class="redpacket_background" />
      <view class="packet_header">
        <view class="packet_image">福</view>
        <view class="packet_info">
          <h3 class="packet_title">{{ packet.active_name }}</h3>
          <p class="packet_date">{{ common.timestampToString(packet.start_time) }} - {{ common.timestampToString(packet.end_time) }}</p>
        </view>
      </view>
    </view>
    <Empty v-if="redpackets.length == 0 && !isReqing" text="----- 还没有红包啦 -----" />
    <view class="to_bottom" v-if="isLast && redpackets.length"> -----到底啦-----</view>
  </view>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { onReachBottom, onPullDownRefresh } from "@dcloudio/uni-app";
import Empty from "@/components/Empty/Empty.vue";
import common from "@/utils/common";
import http from "@/utils/request";

const redpackets = ref([]);
const page = ref(1);
const pageSize = ref(10);
const isLast = ref(false);
const isReqing = ref(false);

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

const handleClick = (packet) => {
  if (packet.status !== 0) return;
  uni.navigateTo({
    url: `/pages/redpacket/index?packet_id=${packet.custom_redpacket_id}`,
  });
};

const _getPacketlist = () => {
  http.request("api/redpacket/get_list", { page: page.value, limit: pageSize.value }).then((callback) => {
    if (callback.code == "success") {
      if (callback.data.last_page <= page.value) isLast.value = true;
      const packetData = callback.data.data || [];
      redpackets.value = [...redpackets.value, ...packetData];
    }
  });
};

onReachBottom(() => {
  // 如果页码是0，避免第一页重复
  if (page.value < 1) return;

  // 最后一页不请求
  if (isLast.value) return;

  isReqing.value = true;

  page.value = page.value + 1;

  _getPacketlist();
});

onPullDownRefresh(() => {
  page.value = 1;
  redpackets.value = [];
  isReqing.value = true;
  isLast.value = false;
  _getPacketlist();
  uni.stopPullDownRefresh();
});
</script>

<style lang="less" scoped>
.red_packet_list {
  display: flex;
  flex-direction: column;
  gap: 20rpx;
  padding: 40rpx;
}

.red_packet_item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #c30a0a;
  border-radius: 20rpx;
  padding: 20rpx;
  box-shadow: 0 4rpx 8rpx rgba(0, 0, 0, 0.1);
  cursor: pointer;
  position: relative;
  height: 140rpx;
  overflow: hidden;
  &.disabled {
    cursor: not-allowed;
    opacity: 0.3;
  }
  .redpacket_background {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 120px;
    z-index: 1;
  }

  .packet_header {
    display: flex;
    align-items: center;
    z-index: 10;
    .packet_image {
      width: 100rpx;
      height: 100rpx;
      border-radius: 50%;
      margin-right: 20rpx;
      background-color: #fede9f;
      color: #af3636;
      font-size: 48rpx;
      font-weight: bold;
      text-align: center;
      line-height: 100rpx;
    }

    .packet_info {
      display: flex;
      flex-direction: column;

      .packet_title {
        font-size: 32rpx;
        font-weight: bold;
        margin: 0;
        color: #fff;
      }

      .packet_date {
        font-size: 24rpx;
        color: #fff;
        margin: 0;
        margin-top: 10rpx;
      }
    }
  }

  .packet_amount {
    font-size: 36rpx;
    font-weight: bold;
    color: #e74c3c;
  }
}
</style>
