<template>
  <ie-page bg-color="#F6F8FA" :fix-height="true">
    <z-paging ref="pagingRef" v-model="historyList" :loading-more-enabled="false" @query="handleQuery">
      <template #top>
        <ie-navbar title="刷题记录" />
      </template>
      <view class="mt-20">
        <view v-for="(item, index) in historyList" :key="index"
          class="bg-white px-40 py-30 flex items-center sibling-border-top">
          <view class="flex-1">
            <view class="text-28">
              <text class=" text-fore-light">知识点：</text>
              <text class="text-fore-title">{{ item.paperName }}</text>
            </view>
            <view class="mt-10 text-28">
              <text class=" text-fore-light">完成时间：</text>
              <text class="text-fore-title">{{ getTime(item.endTime) }}</text>
            </view>
          </view>
          <view class="text-24 text-white bg-primary w-fit px-40 py-12 rounded-full text-center"
            @click="handleViewHistory(item)">
            查看
          </view>
        </view>
      </view>
    </z-paging>
  </ie-page>
</template>
<script lang="ts" setup>
import { useUserStore } from '@/store/userStore';
import { useNavbar } from '@/hooks/useNavbar';
import { useTransferPage } from '@/hooks/useTransferPage';
import { getPracticeHistory } from '@/api/modules/study';
import { Study } from '@/types';
import { Transfer } from '@/types';
import { EnumPaperType } from '@/common/enum';

const { prevData, transferTo } = useTransferPage<{}, Transfer.PracticeResultPageOptions>();
const { baseStickyTop } = useNavbar();
const userStore = useUserStore();
const historyList = ref<Study.PracticeHistory[]>([]);
const handleViewHistory = (item: Study.PracticeHistory) => {
  transferTo('/pagesStudy/pages/knowledge-practice-detail/knowledge-practice-detail', {
    data: {
      paperType: EnumPaperType.PRACTICE,
      examineeId: item.examineeId,
      name: item.paperName,
      directed: item.directed === 1,
      questionType: Number(item.questionType)
    }
  });
}

const pagingRef = ref();
const getTime = (time: string) => {
  return uni.$ie.formatTime(time, 'yyyy-mm-dd hh:MM:ss');
}
const handleQuery = (pageNum: number, pageSize: number) => {
  getPracticeHistory({
    pageNum,
    pageSize
  }).then(res => {
    historyList.value = res.rows.map(item => {
      return {
        ...item,
        endTime: uni.$uv.timeFormat(item.endTime, 'yyyy-mm-dd hh:MM:ss')
      } as Study.PracticeHistory;
    });
    pagingRef.value.complete(res.rows)
  }).catch(() => {
    pagingRef.value.complete(false);
  });
}
onLoad(() => {
  console.log(prevData.value)
  // loadData();
});
</script>
<style lang="scss" scoped></style>