import $req from '../request'
import { storeToRefs } from 'pinia'
import { user } from '@/store'
import { getCache } from '@/utils'
import db from '@/utils/db'
// 课表日历
export const getScheduleList = (data?: Record<string, any>): Promise<any> =>
  $req({
    method: 'get',
    url: '/phone/inspectorTeacher/getCourseTableById',
    data,
  })

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

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

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

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

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

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

// 获取教材章节
export const getChaper = (data?: Record<string, any>): Promise<any> =>
  $req({
    method: 'get',
    url: `/subjectChapter/tree?textBookId=${data.textBookId}`,
  })
// 获取教材章节知识点
export const getKnowledge = (data?: Record<string, any>): Promise<any> =>
  $req({
    method: 'get',
    url: `/subjectKnowledge/tree?chapterId=${data.chapterId}`,
  })

// 发布计划
export const publishPlan = (data?: Record<string, any>): Promise<any> =>
  $req({
    method: 'post',
    url: `/phone/inspectorTeacher/publishPlan`,
    data,
  })
// 获取学员计划
export const getStuPlan = (data?: Record<string, any>): Promise<any> =>
  $req({
    method: 'get',
    url: `/phone/inspectorTeacher/getStuPlanByDate?userId=${data.userId}&courseDate=${data.courseDate}`,
  })

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

  const mergerToken = token.value || db.get('token')

  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 = (data?: Record<string, any>): Promise<any> =>
  $req({
    method: 'post',
    url: `/phone/inspectorTeacher/updateTeacherInfo`,
    data,
  })

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

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

// 消息存储
export const saveTalk = (data?: Record<string, any>): Promise<any> =>
  $req({
    method: 'post',
    url: `/inspectorStudent/sendInspectorTalkRecord`,
    data,
  })

// 验证码校验
export const validCode = (data?: Record<string, any>): Promise<any> =>
  $req({
    method: 'post',
    url: `/sms/validateMessage?phoneNumbers=${data.phone}&code=${data.code}`,
    data,
  })
// 获取我的未读信息
export const getUnreadCount = (data?: Record<string, any>): Promise<any> =>
  $req({
    method: 'get',
    url: `/phone/inspectorTeacher/myUnreadCount`,
  })

// 消息标记为已读
export const readMsg = (data?: Record<string, any>): Promise<any> =>
  $req({
    method: 'post',
    url: `/inspectorStudent/readTalkRecord`,
    data,
  })

// 督学师是否已完善资料
export const needPerfectInfo = (data?: Record<string, any>): Promise<any> =>
  $req({
    method: 'get',
    url: `/phone/inspectorTeacher/is_perfect`,
  })
// 学生是否可以进行精准学判断
export const getPrecisionFlag = (data?: Record<string, any>): Promise<any> =>
  $req({
    method: 'get',
    url: `/userTrainTitle/airPrecisionFlag?subjectId=${data.subjectId}&userId=${data.userId}`,
  })

export const getKnowledgeFlag = (knowledgeId: string): Promise<boolean> => {
  return $req({
    method: 'get',
    url: `/subjectKnowledge/titleFlag?id=${knowledgeId}`,
  })
}

export const getKnowledgeVideo = (chapterId: string | number): Promise<any> => {
  return $req({
    method: 'get',
    url: `/subjectChapterVideo/list?chapterId=${chapterId}`,
  })
}
