import { http } from '../request/request'
import { storeToRefs } from 'pinia'
import { getCache } from '@/utils'
import db from '@/utils/db'
import { userStore } from '@/store'

// 课表日历
export const getScheduleList = async (data?: Record<string, any>): Promise<any> =>
  await http.get<any>('/phone/inspectorTeacher/getCourseTableById', data)

// 查询指定日期学生数据
export const getCourseDate = async (data?: Record<string, any>): Promise<any> =>
  await http.get<any>(
    `/phone/inspectorTeacher/getCourseTabletStudentVoByTime` +
      `${data.date ? `?courseDate=${data.date}` : ''}`,
  )

// 开始督学
export const startInpector = async (data?: Record<string, any>): Promise<any> =>
  await http.post<any>(`/phone/inspectorTeacher/startInspector?courseDate=${data.courseDate}`)

// 结束督学
export const endInpector = async (data?: Record<string, any>): Promise<any> =>
  await http.post<any>(`/phone/inspectorTeacher/endInspector?courseDate=${data.courseDate}`)

// 督学实况
export const condition = async (data?: Record<string, any>): Promise<any> =>
  await http.post<any>(`/phone/inspectorTeacher/inspector_real?courseDate=${data.courseDate}`)

// 获取学员课表日历
export const getCourseData = async (data?: Record<string, any>): Promise<any> =>
  await http.get<any>(`/phone/inspectorTeacher/getStuCourseTable?userId=${data.id}`)

// 获取学科
export const getSubject = async (data?: Record<string, any>): Promise<any> =>
  await http.get<any>(`/subject/list/${data.gradeId}`)

// 获取学科教材
export const getBooks = async (data?: Record<string, any>): Promise<any> =>
  await http.get<any>(
    `/userTextBook/list?gradeId=${data.gradeId}&subjectId=${data.subjectId}&userId=${data.userId}&current=${data.current}`,
  )

// 获取教材章节
export const getChaper = async (data?: Record<string, any>): Promise<any> =>
  await http.get<any>(`/subjectChapter/tree?textBookId=${data.textBookId}`)

// 获取教材章节知识点
export const getKnowledge = async (data?: Record<string, any>): Promise<any> =>
  await http.get<any>(`/subjectKnowledge/tree?chapterId=${data.chapterId}`)

// 发布计划
export const publishPlan = async (data?: Record<string, any>): Promise<any> =>
  await http.post<any>('/phone/inspectorTeacher/publishPlan', data)

// 获取学员计划
export const getStuPlan = async (data?: Record<string, any>): Promise<any> =>
  await http.get<any>(
    `/phone/inspectorTeacher/getStuPlanByDate?userId=${data.userId}&courseDate=${data.courseDate}`,
  )

// 获取督学师信息
export const getUserInfo = async (data?: Record<string, any>): Promise<any> =>
  await http.get<any>('/phone/inspectorTeacher/getCurrentTeacher')
// 文件上传
export const uploadFile = async (file): Promise<any> => {
  const { token } = storeToRefs(userStore())

  const mergerToken = token.value

  const data = await uni.uploadFile({
    url: `/api/main/sysFileInfo/tenUploadAll`, // ${import.meta.env.VITE_HOST}
    file: file,
    name: file.fileName,
    header: {
      // 'content-type': 'multipart/form-data',
      Authorization: mergerToken ? `Bearer ${mergerToken}` : token,
    },
  })
  return JSON.parse(data.data)
}

// 完善督学师资料 /phone/inspectorTeacher/updateTeacherInfo
export const updateTeacherInfo = async (data?: Record<string, any>): Promise<any> =>
  await http.post<any>('/phone/inspectorTeacher/updateTeacherInfo', data)

// 沟通列表
export const getTalkList = async (data?: Record<string, any>): Promise<any> =>
  await http.get<any>(`/phone/inspectorTeacher/inspectorTalkListPage?current=${data.current}`)

// 沟通详情
export const getTalkDetails = async (data?: Record<string, any>): Promise<any> => {
  const param = data.inspectorTalkRecordId
    ? `?inspectorTalkRecordId=${data.inspectorTalkRecordId}&userId=${data.userId}`
    : `?userId=${data.userId}`
  return await http.get<any>(`/phone/inspectorTeacher/studentInspectorTalkPage${param}`)
}

// 消息存储
export const saveTalk = async (data?: Record<string, any>): Promise<any> =>
  await http.post<any>('/inspectorStudent/sendInspectorTalkRecord', data)

// 验证码校验
export const validCode = async (data?: Record<string, any>): Promise<any> =>
  await http.post<any>(`/sms/validateMessage?phoneNumbers=${data.phone}&code=${data.code}`, data)

// 获取我的未读信息
export const getUnreadCount = async (data?: Record<string, any>): Promise<any> =>
  await http.get<any>('/phone/inspectorTeacher/myUnreadCount')

// 消息标记为已读
export const readMsg = async (data?: Record<string, any>): Promise<any> =>
  await http.post<any>('/inspectorStudent/readTalkRecord', data)

// 督学师是否已完善资料
export const needPerfectInfo = async (data?: Record<string, any>): Promise<any> =>
  await http.get<any>('/phone/inspectorTeacher/is_perfect')

// 学生是否可以进行精准学判断
export const getPrecisionFlag = async (data?: Record<string, any>): Promise<any> =>
  await http.get<any>(
    `/userTrainTitle/airPrecisionFlag?subjectId=${data.subjectId}&userId=${data.userId}`,
  )

export const getKnowledgeFlag = async (knowledgeId: string): Promise<boolean> => {
  return await http.get<any>(`/subjectKnowledge/titleFlag?id=${knowledgeId}`)
}

export const getKnowledgeVideo = async (chapterId: string | number): Promise<any> => {
  return await http.get<any>(`/subjectChapterVideo/list?chapterId=${chapterId}`)
}
