<template>
  <div class="container">
    <div class="searchBox">
      <div class="search">
        <span class="search-icon"></span>
        <input placeholder="请输入地点、活动名称等关键字" v-model="keyword" />
      </div>
    </div>
    
    <div class="body">
      <van-list 
        v-model:loading="loading" 
        :finished="finished" 
        finished-text="没有更多了" 
        @load="onLoad"
      >
        <ActItem 
          v-for="item in actList" 
          :key="item.id" 
          :data="item"
          @goDetails="goDetails"
        />
      </van-list>
    </div>
    
    <button class="fBtn" @click="goMyApply">我的报名</button>
  </div>
</template>

<script>
import axios from "axios";
import ActItem from "../components/Home/ActItem.vue";

export default {
  components: {
    ActItem,
  },
  data() {
    return {
      actList: [],
      loading: false,
      finished: false,
      currentPage: 1,
      keyword: "",
    };
  },
  methods: {
    fetchActList(currentPage = 1) {
      const that = this;
      const payload = {
        currentPage,
        pageSize: 10,
        keyword: this.keyword,
      };

      this.loading = true;
      axios
        .get("/api/actList", { params: payload })
        .then(function (response) {
          const { error, data = {} } = response.data;
          if (error === 0) {
            const currentPage = data.current;
            const list = data.list;

            if (currentPage === 1) {
              that.actList = list;
            } else {
              that.actList.push(...list);
            }

            that.currentPage = currentPage;
            that.finished = data.pageCount === currentPage;
          }
        })
        .finally(function () {
          that.loading = false;
        });
    },
    onLoad() {
      this.fetchActList(this.currentPage + 1);
    },
    goMyApply() {
      this.$router.push("/myApply");
    },
    goDetails(id) {
      this.$router.push("/actDetails?id=" + id);
    },
  },
  watch: {
    keyword() {
      this.currentPage = 1;
      this.fetchActList(1);
    },
  },
  mounted() {
    this.fetchActList(1);
  },
};
</script>

<style scoped>
.searchBox {
  padding: 15px;
  background: white;
}

.search {
  display: flex;
  align-items: center;
  padding: 10px;
  background: #f5f5f5;
  border-radius: 20px;
}

.search input {
  flex: 1;
  border: none;
  background: transparent;
  outline: none;
  margin-left: 10px;
}

.body {
  padding: 10px;
}

.fBtn {
  position: fixed;
  bottom: 20px;
  right: 20px;
  padding: 12px 24px;
  background: #ff5d23;
  color: white;
  border: none;
  border-radius: 25px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.2);
  cursor: pointer;
}
</style>
