<template>
  <view class="page">
    <scroll-view
      scroll-y
      @scrolltolower="loadMore"
      refresher-enabled
      :refresher-triggered="isRefreshing"
      @refresherrefresh="onRefresh"
      v-if="zjxxList.length > 0"
      class="warehouse-list"
    >
      <view
        v-for="(zjxxItem, index) in zjxxList"
        :key="index"
        class="warehouse-item"
        @click="goPage(zjxxItem)"
      >
        <view class="warehouse-details">
          <view class="detail-row">
            <text class="label"
              >业务时间：<text class="blackValue">{{
                zjxxItem.jcsj || "--"
              }}</text></text
            >
            <text class="label"
              >粮食品种：<text class="blackValue">{{
                getDictLabelSync(DICT_TYPE.SYSTEM_LSPZ, zjxxItem.lspzmc) || "--"
              }}</text></text
            >
          </view>
          <view class="detail-row">
            <text class="label"
              >检测类型：<text class="blackValue">{{
                zjxxItem.jclx || "--"
              }}</text></text
            >
            <text class="label"
              >是否合格：<text class="blackValue">{{
                zjxxItem.sfhg == 0 ? "不合格" : "合格" || "--"
              }}</text></text
            >
          </view>
        </view>
      </view>

      <!-- 添加加载更多提示 -->
      <view class="loading-more" v-if="zjxxList.length > 0">
        <text v-if="isLoading">加载中...</text>
        <text v-else-if="!hasMore">没有更多数据了</text>
      </view>
    </scroll-view>

    <view v-else class="no-data">暂无数据</view>
  </view>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { getDictOptions, DICT_TYPE, getDictLabelSync } from "@/utils/dict.js";
import { getZjxxByCfId } from "@/api/grainDepositor";
const props = defineProps({
  caId: {
    type: String || Number,
  },
});

// 添加下拉刷新和上拉加载状态
const isRefreshing = ref(false);
const isLoading = ref(false);
const page = ref(1);
const pageSize = ref(10);
const hasMore = ref(true);
const zjxxList = ref([]);
// 请求数据
const getZjxxList = async (isLoadMore = false) => {
  try {
    const res = await getZjxxByCfId({
      caId: props.caId,
      page: page.value,
      pageSize: pageSize.value,
    });

    if (res.code === 0 && res.data !== null) {
      // 判断是否还有更多数据
      hasMore.value = res.data.records.length === pageSize.value;

      // 如果是加载更多，则追加数据，否则替换数据
      if (isLoadMore) {
        zjxxList.value = [...zjxxList.value, ...res.data.records];
      } else {
        zjxxList.value = res.data.records;
      }
    }
  } catch (error) {
    console.error("获取质检信息失败：", error);
    uni.showToast({
      title: "获取数据失败",
      icon: "none",
    });
  }
};

onMounted(async () => {
  await getZjxxList();
});

// 下拉刷新
const onRefresh = async () => {
  try {
    isRefreshing.value = true;
    page.value = 1;
    await getZjxxList();
  } finally {
    isRefreshing.value = false;
  }
};

// 上拉加载更多
const loadMore = async () => {
  if (!hasMore.value || isLoading.value) return;

  try {
    isLoading.value = true;
    page.value++;
    await getZjxxList(true);
  } finally {
    isLoading.value = false;
  }
};

const goPage = async (item) => {
  const serializedData = JSON.stringify(item);
  const encodedData = encodeURIComponent(serializedData);
  uni.navigateTo({
    url: `/pages/infoDetails/zj/detail?getData=${encodedData}`,
  });
};
</script>

<style lang="scss" scoped>
.page {
  position: relative; // 确保伪元素相对于 .page 定位
}

.page::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 200rpx; // 设置渐变的高度
  background: linear-gradient(180deg, #cfddfc, #eff2f5);
  z-index: -1; // 确保它在内容之下
}

.warehouse-list {
  padding: 40rpx 20rpx 120rpx 40rpx;
  box-sizing: border-box;
  height: 100vh;

  .warehouse-item {
    border-left: 2px solid #3872e3;
    background: linear-gradient(180deg, #ffffff, #f5f8ff);
    border-radius: 10px;
    padding: 10rpx 0 10rpx 20rpx;
    margin-bottom: 20rpx;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    box-sizing: border-box;

    .warehouse-details {
      padding-top: 20rpx;
      font-size: 30rpx;
      color: #747476;

      .detail-row {
        margin-bottom: 10px;
        text.label {
          width: 50%;
          display: inline-flex;
          .blackValue {
            color: #000000;
          }
        }
      }
    }
  }
}
.button-container {
  position: fixed;
  bottom: 0;
  background-color: #ffffff;
  width: 100vw;
  padding: 40rpx 0;

  .button-pad {
    padding: 0 40rpx;
    display: flex;
    justify-content: space-between;
  }

  .select-all,
  .confirm {
    width: 44%;
    height: 70rpx;
    line-height: 70rpx;
    border-radius: 10rpx;
    background-color: #1e5fdf;
    color: #fff;
    font-size: 28rpx;
    text-align: center;
  }
}

.warehouse-info {
  padding-top: 20rpx;
}

.navbar {
  display: flex;
  height: 40px;
  padding: 0 5px;
  position: relative;
  z-index: 10;

  .nav-item {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    font-size: 35rpx;
    position: relative;
    color: #334b68;

    &.current {
      color: #0f2239;
      &:after {
        content: "";
        position: absolute;
        left: 50%;
        bottom: 0;
        transform: translateX(-50%);
        width: 60%;
        height: 0;
        border-bottom: 4px solid #789fec;
      }
    }
  }
}

.swiper-box {
  height: calc(100vh - 80rpx);
}

.list-scroll-content {
  height: calc(100vh - 200rpx);
}
.info-scroll-content {
  height: calc(100vh - 120rpx);
}

.no-data {
  text-align: center;
  color: #999;
  font-size: 30rpx;
  margin-top: 20rpx;
}

.loading-more {
  text-align: center;
  padding: 20rpx 0;
  color: #999;
  font-size: 24rpx;
}
</style>
