<template>
  <!-- <headers title='考勤详情' /> -->
  <div class="deatils-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="detailList.length">
      <text class="fund">为您找到{{ listQuery.total }}位同学</text>

      <block v-for="(item, index) in detailList" :key="index">
        <detailsItem :detailItem="item" @init="refresh" />
      </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>
  </div>
</template>

<script setup lang="ts">
import { getStateList } from "@/api/course";
import detailsItem from "./components/detailsItem.vue";

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

const detailList = ref([] as any);

const listQuery = reactive({
  courseId: "",
  pageIndex: 1,
  pageSize: 10,
  total: 0,
  Name: "",
  KqType: 0, // 0 全部，1 正常，2 缺勤，3 请假,4 补签
});

const status = ref("loadmore");

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

const changeTagType = (index: number) => {
  if (index === listQuery.KqType) return;
  listQuery.KqType = index;
  tagList.value.map((item: any, i: number) => {
    if (index === i) item.type = "primary";
    else item.type = "info";
  });
  detailList.value = [];
  listQuery.pageIndex = 1;
  listQuery.pageSize = 10;
  init();
};

const search = (val: any) => {
  listQuery.Name = val.value;
  detailList.value = [];
  listQuery.pageIndex = 1;
  listQuery.pageSize = 10;
  init();
};

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

const init = async () => {
  status.value = "loading";
  const { data: res } = await getStateList(listQuery);
  if (res.code == 200) {
    if (res.total > 10) status.value = "loadmore";
    else status.value = "nomore";
    detailList.value = [...detailList.value, ...res.data];
    listQuery.total = res.total;
  }
};

const refresh = () => {
  detailList.value = [];
  init();
};

onLoad((options: any) => {
  listQuery.courseId = options.courseId;
  init();
});
</script>

<style lang="scss" scoped>
.deatils-container {
  padding: 30rpx;

  .tag {
    margin-bottom: 48rpx;

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

    .tag-active {
      background: #ddf3ff;
      color: #05a8ff;
    }
  }
}
</style>
