<template>
  <ie-page bg-color="#F6F8FA">
    <ie-navbar title="" bg-color="transparent" :placeholder="false" />
    <view class="relative">
      <ie-image :is-oss="true" src="/study-bg9.png" custom-class="w-full h-[544rpx] absolute top-0 left-0 z-0" />
      <ie-image :is-oss="true" src="/study-bg10.png" :customClass="`w-312 h-198 absolute right-32 z-1 ${appStore.isH5 ? 'top-75' : 'top-145'}`"
        mode="aspectFill" />
      <view class="relative z-2 mx-30">
        <view :class="`ml-55 ${appStore.isH5 ? 'pt-126' : 'pt-196'}`">
          <ie-image :is-oss="true" src="/study-title5.png" customClass="w-264 h-60" />
        </view>
        <view v-if="universityInfo" class="mt-65 bg-white rounded-15 px-40 pt-50 pb-40">
          <view class="text-36 text-fore-title text-center font-bold">{{ universityInfo.universityName }}</view>
          <view class="mt-26 text-32 text-fore-title text-center font-bold">模考须知</view>
          <view class="mt-66">
            <view class="flex items-center justify-between">
              <text class="text-28 text-fore-light">所考专业</text>
              <text class="text-28 text-fore-title">{{ universityInfo.majorName }}</text>
            </view>
            <view class="mt-40 flex items-center justify-between">
              <text class="text-28 text-fore-light">考生类型</text>
              <view class="text-28 text-fore-title">
                <ie-dict :dict-name="EnumDictName.EXAM_TYPE" :dict-value="userStore.getExamType || '--'" />
              </view>
            </view>
            <view class="mt-40 flex items-center justify-between">
              <text class="text-28 text-fore-light">考试类型</text>
              <text class="text-28 text-fore-title">{{ subjectInfo.subject }}</text>
            </view>
            <view class="mt-40 flex items-center justify-between">
              <text class="text-28 text-fore-light">考试标准</text>
              <text class="text-28 text-fore-title">{{ paperInfo.time }}分钟，满分{{ paperInfo.score }}分</text>
            </view>
            <view class="mt-40 flex items-center justify-between">
              <text class="text-28 text-fore-light">考试题型</text>
              <text class="text-28 text-fore-title">{{ questionTypes || '--' }}</text>
            </view>
          </view>
          <view class="mt-36 rounded-15 bg-back px-40 py-36 flex">
            <view class="flex-1 text-center">
              <view class="text-40 text-fore-title font-bold">{{ questionCount }}</view>
              <view class="mt-10 text-28 text-fore-light">题量</view>
            </view>
            <view class="flex-1 text-center">
              <view class="text-40 text-fore-title font-bold">{{ paperInfo.score }}</view>
              <view class="mt-10 text-28 text-fore-light">总分</view>
            </view>
          </view>
          <ie-button type="primary" custom-class="mt-44" :disabled="!isReady" @click="handleStartTest">开始考试</ie-button>
        </view>
      </view>
    </view>
  </ie-page>
</template>

<script lang="ts" setup>
import { useTransferPage } from '@/hooks/useTransferPage';
import { getOpenExaminee, getPaper } from '@/api/modules/study';
import { useUserStore } from '@/store/userStore';
import { useAppStore } from '@/store/appStore';
import { EnumDictName, EnumPaperType } from '@/common/enum';
import { Study } from '@/types';

const { prevData, transferTo } = useTransferPage();
const userStore = useUserStore();
const appStore = useAppStore();

const isReady = ref(false);
const examineeId = ref();
const paperInfo = ref<Study.ExamineePaperInfo>({
  score: 0,
  time: 0,
  types: []
});
const questionTypes = computed(() => {
  return paperInfo.value.types.reduce((acc: string, curr: { type: string }, index: number) => {
    return acc + curr.type + (index < paperInfo.value.types.length - 1 ? "、" : "");
  }, "");
});
const questionCount = computed(() => {
  return paperInfo.value.types.reduce((acc: number, curr: { count: number }) => {
    return acc + curr.count;
  }, 0);
});
const questions = ref<Study.ExamineeQuestion[]>([]);
const universityInfo = computed(() => {
  return prevData.value.universityInfo;
});
const subjectInfo = computed(() => {
  return prevData.value.subjectInfo;
});
const handleStartTest = () => {
  transferTo('/pagesStudy/pages/exam-start/exam-start', {
    data: {
      paperType: EnumPaperType.SIMULATED,
      name: '模拟考试-' + subjectInfo.value.subject,
      simulationInfo: {
        examineeId: examineeId.value
      }
    }
  });
}
const loadData = async () => {
  uni.$ie.showLoading('AI智能出卷中...');
  const { data } = await getOpenExaminee({
    paperType: 'Simulated',
    subjectId: subjectInfo.value.subjectId,
    directed: true
  });
  if (!data) {
    uni.$ie.hideLoading();
    return;
  }
  examineeId.value = data.examineeId;
  paperInfo.value = {
    ...data.paperInfo,
    time: Math.round(data.paperInfo.time / 60)
  }
  const { data: paperData } = await getPaper({ type: 'Simulated', id: data.paperId });
  if (paperData) {
    questions.value = paperData.questions;
    isReady.value = true;
  }
  setTimeout(() => {
    uni.$ie.hideLoading();
  }, 300);
}
onLoad(() => {
  loadData();
});
</script>

<style lang="scss" scoped></style>
