<script setup>
import { onMounted, ref } from "vue";
import {
  apiPublicBannerList,
  apiPublicBannerMark,
  bannerHistoryList,
} from "@/api/hzyb/report";
import { List } from "vant";
import { useRoute, useRouter, onBeforeRouteUpdate } from "vue-router";
const route = useRoute();
const router = useRouter();
const bannerDataList = ref([]);

// banner 获取列表
async function getBannerList() {
  const res = await apiPublicBannerList();
  if (res.code == 200) {
    // 加载状态结束
    bannerDataList.value = res.data;
  }
}

const loading = ref(false);
const finished = ref(false);

function surveyHistoryLoad() {
  if (loading.value) return;
  page.value++;
  getSurveyHistoryList();
}
const page = ref(1);
const limit = ref(10);
const bannerHistoryDataList = ref([]);

// 获取已结束的调研
async function getSurveyHistoryList() {
  loading.value = true;
  const res = await bannerHistoryList({
    page: page.value,
    limit: limit.value,
  });
  loading.value = false;
  if (res.code === 200) {
    let arr = res.data.list || [];
    bannerHistoryDataList.value = [...bannerHistoryDataList.value, ...arr];
    if (res.data.paging.is_end) {
      finished.value = true;
    }
  }
}

// banner 点击事件
async function bannerSwiperHandler(item) {
  const res = await apiPublicBannerMark({
    first_source: 1, //一级来源 1小程序移动 2小程序pc 3研报官网
    second_source: 2, //二级来源 1首页 2研报详情页
    id: item.id,
  });
  if (res.code === 200) {
    wx.miniProgram.navigateTo({
      url:
        "/pages-report/disseminatePage/disseminatePage?imgHb=" +
        encodeURIComponent(item.jump_url_mobile) +
        "&title=" +
        item.remark +
        "&id=" +
        item.id +
        "&enable=" +
        item.enable,
    });
  }
}

onMounted(() => {
  localStorage.setItem("hzyb-token", route.query.token);
  getBannerList();
  getSurveyHistoryList();
  document.title = "调研列表";
});
</script>

<template>
  <div class="container survey-history-content">
    <div class="state-content">进行中</div>
    <div
      class="activity-content"
      v-for="item in bannerDataList"
      :key="item.id"
      @click="bannerSwiperHandler(item)"
    >
      <img :src="item.image_url_mobile" alt="" />
    </div>
    <div class="state-content" style="margin-top: 20px">已结束</div>
    <List
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="surveyHistoryLoad"
    >
      <div
        class="activity-content"
        v-for="item in bannerHistoryDataList"
        :key="item.id"
        @click="bannerSwiperHandler(item)"
      >
        <img :src="item.image_url_mobile" alt="" />
      </div>
    </List>
  </div>
</template>

<style lang="scss" scoped>
.survey-history-content {
  padding: 35px;
  .state-content {
    position: relative;
    font-size: 28px;
    font-weight: 500;
    padding-left: 15px;
    &::after {
      content: "";
      position: absolute;
      top: 8px;
      left: 0;
      width: 6px;
      height: 27px;
      background-color: #e3b377;
    }
  }
  .activity-content {
    margin-top: 20px;
    width: 100%;
    img {
      width: 100%;
      height: auto;
    }
  }
}
</style>
