<template>
  <view class="my-attendance-container">
    <!-- 搜索 -->
    <uni-search-bar
      placeholder="班级名称"
      bgColor="#ffffff"
      clearButton="auto"
      cancelButton="none"
      @confirm="search"
      @clear="clear"
    />
    <view v-if="myAttendanceList.length">
      <text class="fund">为你找到{{ listQuery.total }}个的课程</text>

      <block v-for="(item, index) in myAttendanceList" :key="index">
        <myAttendanceItem :myAttendanceItem="item" @init="init" />
      </block>
      <!-- <view v-if="tabsState.current" style="padding: 20rpx">
        <u-loadmore :status="status" icon-type="flower" />
      </view> -->
    </view>
    <u-empty
      v-else
      text="暂无数据~"
      icon-size="506"
      :margin-top="220"
      src="../../static/img/noData.png"
    ></u-empty>
  </view>
</template>

<script setup lang="ts">
import { getMyAttendance } from "@/api/myAttendance";
import { useStudentStore } from "@/stores/modules/studentStore";
import myAttendanceItem from "./components/myAttendanceItem.vue";

const globalStore = useStudentStore();

const status = ref("loadmore");

const listQuery = reactive({
  Phone: globalStore.infoData.FMOBILE,
  pageIndex: 1,
  pageSize: 10,
  Name: "",
  total: 0,
});

const myAttendanceList: any = ref([] as any);

const search = (val: any) => {
  listQuery.Name = val.value;
  myAttendanceList.value = [];
  init();
};

const clear = () => {
  listQuery.Name = "";
  myAttendanceList.value = [];
  init();
};

// 上拉加载数据
onReachBottom(() => {
  // 判断是否还有下一页数据
  if (listQuery.pageIndex * listQuery.pageSize >= listQuery.total)
    return (status.value = "nomore");
  // // 判断是否正在请求其它数据，如果是，则不发起额外的请求
  if (status.value === "loading") return;
  listQuery.pageIndex += 1;
  init();
});

const init = async () => {
  status.value = "loading";
  const { data: res }: any = await getMyAttendance(listQuery);
  if (res.code == 200) {
    if (res.total > 10) status.value = "loadmore";
    else status.value = "nomore";
    myAttendanceList.value = [...myAttendanceList.value, ...res.data];
    listQuery.total = res.total;
    console.log(myAttendanceList.value);
  }
};
init();
</script>

<style lang="scss" scoped>
.my-attendance-container {
  padding: 0 30rpx 30rpx;
  padding-top: 20rpx;
}
</style>
