<template>
  <!-- <headers title="我的班级" /> -->
  <view class="class-container">
    <u-tabs
      :list="tabsState.list"
      :is-scroll="false"
      v-model="tabsState.current"
      :active-item-style="{ fontSize: '34rpx', color: '#000000' }"
      font-size="28"
      bar-width="72"
      bar-height="6"
      bg-color="#fbfcff"
      inactive-color="#cecfcf"
      active-color="#05a8ff"
      @change="tabsChange"
    />
    <!-- 搜索 -->
    <uni-search-bar
      placeholder="班级名称"
      bgColor="#ffffff"
      clearButton="auto"
      cancelButton="none"
      @confirm="search"
      @clear="clear"
    />
    <!-- 内容 -->
    <view v-if="evaluateList.length">
      <text class="fund">为您找到{{ listQuery.total }}个班级</text>
      <view v-if="!tabsState.current">
        <block v-for="(item, index) in evaluateList" :key="index">
          <classItem :classItem="item" :current="2" @init="tabsChange" />
        </block>
      </view>
      <view v-else>
        <block v-for="(item, index) in evaluateList" :key="index">
          <courseItem :courseItem="item" :current="2" />
        </block>
      </view>
      <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 headers from "@/components/header/index.vue"
import classItem from "@/pages/class/components/classItem.vue";
import courseItem from "@/pages/course/components/courseItem.vue";
import { getClassList } from "@/api/class";
import { getCourseList } from "@/api/course";
import { useGlobalStore } from "@/store/useStore";

const globalStore = useGlobalStore();

const tabsState = reactive({
  list: [
    {
      name: "培训评价",
    },
    {
      name: "课程评价",
    },
  ],
  current: 0,
});

const evaluateList = ref([] as any);

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

const status = ref("loadmore");

const tabsChange = (index: number) => {
  evaluateList.value = [];
  listQuery.pageIndex = 1;
  listQuery.pageSize = 10;
  if (index) initCourse();
  else initClass();
};

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

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

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

const initClass = async () => {
  status.value = "loading";
  const { data: res } = await getClassList(listQuery);
  if (res.code == 200) {
    if (res.total > 10) status.value = "loadmore";
    else status.value = "nomore";
    evaluateList.value = [...evaluateList.value, ...res.data];
    listQuery.total = res.total;
  }
};
const initCourse = async () => {
  status.value = "loading";
  const { data: res } = await getCourseList(listQuery);
  if (res.code == 200) {
    if (res.total > 10) status.value = "loadmore";
    else status.value = "nomore";
    evaluateList.value = [...evaluateList.value, ...res.data];
    listQuery.total = res.total;
  }
};

tabsChange(tabsState.current);
</script>

<style lang="scss" scoped>
.class-container {
  padding: 0 32rpx 168rpx;
}
</style>
