import { get, post, postMultipart } from '#/api/http'

import { BasicService } from '../basic'

/**
 * 试卷发布对象类型枚举
 */
export enum TargetTypeEnum {
  CLASS = 2,
  STUDENT = 1,
}

/**
 * 试卷用户发布DTO
 */
export interface SchoolPaperUserPublishDto {
  /**
   * 试卷发布对象类型枚举
   */
  targetType: TargetTypeEnum

  /**
   * 试卷发布对象ID列表
   */
  typeIds: number[]
}

/**
 * 试卷发布DTO
 */
export interface SchoolPaperPublishDto {
  /**
   * 结束时间
   */
  endTime: Date

  /**
   * 名称
   */
  name: string

  /**
   * 试卷id
   */
  paperId: number

  /**
   * 备注
   */
  remark: string

  /**
   * 开始时间
   */
  startTime: Date

  /**
   * 用户列表
   */
  userList: SchoolPaperUserPublishDto[]
}

class PaperService extends BasicService {
  async get(id: any) {
    return await get(`/school/paper/get/${id}`)
  }

  async publish(data: SchoolPaperPublishDto) {
    return await post<any>('/school/paper/publish', data)
  }

  async updateStatus(id: any, status: any) {
    const url = '/school/paper/updateStatus'
    const entity = { id, status }
    const config = (await this.willUpdateSwitchKeyValues(url, entity)) as any
    return await post(url, entity, config)
  }

  protected findPageUrl(): string {
    return '/school/paper/list'
  }

  protected removeUrl(): string {
    throw new Error('Method not implemented.')
  }

  protected saveOrUpdateUrl(): string {
    return '/school/paper/saveOrUpdate'
  }

  // 删除试卷
  async delete(id: any) {
    return await post<any>(`/school/paper/delete/${id}`)
  }

  // 上传PDF试题
  async uploadPdf(paperId: any, file: File) {
    return await postMultipart<any>('/school/paperReleaseRecord/uploadPdf', {
      paperId,
      file,
    })
  }

  protected override switchKeyValues() {
    return [
      {
        key: 'status',
        trueValue: 1,
        falseValue: 0,
      },
    ]
  }
}

export const paperService = new PaperService()
