<template>
  <view class="course-container">
    <!-- 搜索 -->
    <uni-search-bar
      placeholder="班级名称"
      bgColor="#ffffff"
      clearButton="auto"
      cancelButton="none"
      @confirm="search"
      @clear="clear"
    />
    <view class="tag">
      <text
        v-for="(item, i) in tagList"
        :key="i"
        class="tag-item"
        :class="item.type === 'primary' ? 'tag-active' : ''"
        @click="changeTagType(i)"
        >{{ item.name }}
      </text>
    </view>
    <!-- 内容 -->
    <view v-if="courseList.length">
      <text class="fund">为你找到{{ listQuery.total }}个的课程</text>
      <block v-for="(item, index) in courseList" :key="index">
        <attendanceDetailsItem :courseItem="item" @init="init" />
      </block>
      <view 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 { getCourse } from "@/api/course";
import { useStudentStore } from "@/stores/modules/studentStore";
import attendanceDetailsItem from "./components/attendanceDetailsItem.vue";
import useQqMaps from "@/utils/QqMaps";

const globalStore = useStudentStore();

const tagList = ref([
  { type: "primary", name: "全部" },
  { type: "info", name: "正常" },
  { type: "info", name: "缺勤" },
  { type: "info", name: "请假" },
]);

const status = ref("loadmore");

const listQuery = reactive({
  Phone: globalStore.infoData.FMOBILE,
  type: 2,
  pageIndex: 1,
  pageSize: 10,
  total: 0,
  classId: "",
  Name: "",
  KqType: 0, // 0 全部，1 正常，2 缺勤，3 请假
});

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

const { initQqMaps } = useQqMaps();

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

const clear = () => {
  listQuery.Name = "";
  init();
};

const changeTagType = (index: number) => {
  if (index === listQuery.KqType) return;
  tagList.value.map((item: any, i: number) => {
    if (index === i) item.type = "primary";
    else item.type = "info";
  });
  listQuery.KqType = index;
  courseList.value = [];
  listQuery.pageIndex = 1;
  listQuery.pageSize = 10;
  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 getCourse(listQuery);
  if (res.code == 200) {
    if (res.total > 10) status.value = "loadmore";
    else status.value = "nomore";
    courseList.value = [...courseList.value, ...res.data];
    listQuery.total = res.total;
  }
};
onLoad(async (options: any) => {
  listQuery.classId = options.classId;
  await initQqMaps();
  await init();
});
</script>

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

  .tag {
    margin-bottom: 48rpx;

    .tag-item {
      margin-right: 24rpx;
      border-radius: 24rpx;
      font-size: 24rpx;
      padding: 8rpx 24rpx;
      font-weight: 400;
      color: #aeaeae;
      background: #ffffff;
    }

    .tag-active {
      background: #ddf3ff;
      color: #05a8ff;
    }
  }
}

:deep(uni-modal) {
  z-index: 99999 !important;
}
</style>
