import request from "../request";
import Mcok from "mockjs";

interface topicQuery {
  //分类练习
  classIssue: string;
  //当前页码
  pageNum: string;
  //每页数据量
  pageSize: string;
}

/**
 * 获取题目列表
 * @returns
 */
export async function getTopicList(params: any, isVip: Boolean = true) {
  const getUserAnswer = (type: string) => {
    switch (type) {
      case "判断题":
        return null;
      case "单选题":
        return null;
      case "多选题":
        return [];
    }
  };

  const getType = (answer: Array<string>) => {
    if (answer.length === 1) {
      if (["√", "×"].includes(answer[0])) {
        return "判断题";
      } else {
        return "单选题";
      }
    } else {
      return "多选题";
    }
  };

  const res = await request({
    url: isVip
      ? "/student/qustion/info/list"
      : "/student/qustion/info/normalList",
    params,
  });

  const data = {
    total: res.data.total,
    list: res.data.rows.map((item: any) => {
      return {
        ...item,
        explain: item.issue,
        opts: item.opts.split("-"),
        image: item.image,
        type: getType(item.answer.split("-")),
        answer: item.answer,
        userAnswer: getUserAnswer(getType(item.answer.split("-"))),
        isTrue: null,
      };
    }),
  };

  return data;
}

/**
 * 获取题目分类
 */
export async function getTopicClass(path: string, params: any) {
  return request({
    url: `/student/qustion/info/${path}`,
    params,
  });
}

/**
 * 根据ids批量获取问题详情
 */
export const getQuestionInfoByIds = async (
  params: Test.QuestionInfoByIdsParams
) => {
  const res = await request({
    url: "/student/qustion/info/getQuestionInfoByIds",
    params: {
      ...params,
      ids: params.ids.toString(),
    },
  });
  return <Test.QuestionInfoByIdsRes>res.data;
};
