import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import { TrainingVO, TrainingQuery, TrainingForm, TrainingUploadResult, TrainingParticipantVO, TrainingLearnRecordVO } from './types';

/**
 * 查询培训列表
 * @param query
 * @returns {*}
 */
export const listTraining = (query: TrainingQuery): AxiosPromise<TrainingVO[]> => {
  return request({
    url: '/main/training/list',
    method: 'get',
    params: query
  });
};

/**
 * 查询线下培训列表
 * @param query
 * @returns {*}
 */
export const listOfflineTraining = (query: TrainingQuery): AxiosPromise<TrainingVO[]> => {
  return request({
    url: '/main/training/offline/list',
    method: 'get',
    params: query
  });
};

/**
 * 查询培训详细
 * @param id
 * @returns {*}
 */
export const getTraining = (id: string | number): AxiosPromise<TrainingVO> => {
  return request({
    url: '/main/training/' + id,
    method: 'get'
  });
};

/**
 * 查询线下培训详细
 * @param id
 * @returns {*}
 */
export const getOfflineTraining = (id: string | number): AxiosPromise<TrainingVO> => {
  return request({
    url: '/main/training/offline/' + id,
    method: 'get'
  });
};

export const listTrainingLearnRecords = (id: string | number, query: PageQuery): AxiosPromise<TrainingLearnRecordVO[]> => {
  return request({
    url: '/main/training/' + id + '/learn-records',
    method: 'get',
    params: query
  });
};

export const listOfflineTrainingParticipants = (id: string | number, query: PageQuery): AxiosPromise<TrainingParticipantVO[]> => {
  return request({
    url: '/main/training/offline/' + id + '/participants',
    method: 'get',
    params: query
  });
};

export const exportTrainingLearnRecords = (id: string | number, studentIds: Array<string | number>) => {
  return request({
    url: '/main/training/' + id + '/learn-records/export',
    method: 'post',
    data: studentIds,
    responseType: 'blob'
  });
};

export const exportOfflineTrainingParticipants = (id: string | number, studentIds: Array<string | number>) => {
  return request({
    url: '/main/training/offline/' + id + '/participants/export',
    method: 'post',
    data: studentIds,
    responseType: 'blob'
  });
};

/**
 * 新增培训
 * @param data
 * @returns {*}
 */
export const addTraining = (data: TrainingForm) => {
  return request({
    url: '/main/training',
    method: 'post',
    data: data
  });
};

/**
 * 培训文件上传
 * @param file
 * @returns {*}
 */
export const uploadTrainingFile = (file: File): AxiosPromise<TrainingUploadResult> => {
  const formData = new FormData();
  formData.append('file', file);
  return request({
    url: '/main/training/upload',
    method: 'post',
    data: formData
  });
};

/**
 * 修改培训
 * @param data
 * @returns {*}
 */
export const updateTraining = (data: TrainingForm) => {
  return request({
    url: '/main/training',
    method: 'put',
    data: data
  });
};

/**
 * 删除培训
 * @param id
 * @returns {*}
 */
export const delTraining = (id: string | number | Array<string | number>) => {
  return request({
    url: '/main/training/' + id,
    method: 'delete'
  });
};

/**
 * 删除线下培训
 * @param id
 * @returns {*}
 */
export const delOfflineTraining = (id: string | number) => {
  return request({
    url: '/main/training/offline/' + id,
    method: 'delete'
  });
};

/**
 * 更新培训状态（上架/下架）
 * @param id
 * @param status
 * @returns {*}
 */
export const updateTrainingStatus = (id: string | number, status: number) => {
  return request({
    url: '/main/training/updateStatus',
    method: 'put',
    data: { id, status }
  });
};

/**
 * 更新线下培训状态（上架/下架）
 * @param id
 * @param status
 * @returns {*}
 */
export const updateOfflineTrainingStatus = (id: string | number, status: number) => {
  return request({
    url: '/main/training/offline/updateStatus',
    method: 'put',
    data: { id, status }
  });
};

/**
 * 查询学员培训学习记录列表
 * @param studentId 学员ID
 */
export const listStudentTrainingRecords = (studentId: string | number): AxiosPromise<TrainingLearnRecordVO[]> => {
  return request({
    url: '/main/training-learn-record/list',
    method: 'get',
    params: { studentId }
  });
};
