<template>
  <ie-page ref="iePageRef">
    <ie-navbar :title="pageTitle" />
    <view class="px-30 py-16 bg-back">
      <view class="flex items-center justify-end gap-x-4" @click="handleViewHistory">
        <uv-icon name="clock" size="16" color="#31A0FC"></uv-icon>
        <text class="text-28 text-primary">查看记录</text>
        <uv-icon name="arrow-right" size="16" color="#31A0FC"></uv-icon>
      </view>
    </view>
    <view class="px-40">
      <knowledgeTree :tree-data="treeData" @start-practice="handleStartPractice" />
    </view>
  </ie-page>
</template>

<script lang="ts" setup>
import IePage from '@/components/ie-page/ie-page.vue';
import { useTransferPage } from '@/hooks/useTransferPage';
import { getTextbooksKnowledgeList } from '@/api/modules/study';
import knowledgeTree from '@/pagesStudy/components/knowledge-tree.vue';
import * as Study from '@/types/study';
import { EnumPaperType } from '@/common/enum';
import { useUserStore } from '@/store/userStore';
const { prevData, transferTo } = useTransferPage();

const userStore = useUserStore();
const iePageRef = ref<InstanceType<typeof IePage>>();
const pageTitle = computed(() => {
  return '教材同步练习';
});

const treeData = ref<Study.KnowledgeNode[]>([]);

const handleViewHistory = () => {
  transferTo('/pagesStudy/pages/textbooks-practice-history/textbooks-practice-history', {
    data: {}
  });
}
const loadKnowledgeList = async () => {
  try {
    uni.$ie.showLoading();
    const { data } = await getTextbooksKnowledgeList();
    treeData.value = data as Study.KnowledgeNode[];
  } catch (error) {
    console.log(error);
  } finally {
    uni.$ie.hideLoading();
  }
}

const handleStartPractice = async (node: Study.KnowledgeNode) => {
  const isVip = await userStore.checkVip();
  if (isVip) {
    transferTo('/pagesStudy/pages/exam-start/exam-start', {
      data: {
        name: '教材同步练习-' + node.name,
        paperType: EnumPaperType.COURSE,
        practiceInfo: {
          name: node.name,
          relateId: node.id
        },
      }
    });
  } else {
    iePageRef.value?.showVipPopup();
  }
}

onShow(() => {
  loadKnowledgeList();
});
</script>

<style></style>
