import request from '@/utils/request';
import type {ApiResult, PageResult} from '@/api/index';
import type {HjmCourses, HjmCoursesParam} from './model';

/**
 * 分页查询课程
 */
export async function pageHjmCourses(params: HjmCoursesParam) {
  const res = await request.get<ApiResult<PageResult<HjmCourses>>>(
    '/hjm/hjm-courses/page',
    {
      params
    }
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询课程列表
 */
export async function listHjmCourses(params?: HjmCoursesParam) {
  const res = await request.get<ApiResult<HjmCourses[]>>(
    '/hjm/hjm-courses',
    {
      params
    }
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加课程
 */
export async function addHjmCourses(data: HjmCourses) {
  const res = await request.post<ApiResult<unknown>>(
    '/hjm/hjm-courses',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改课程
 */
export async function updateHjmCourses(data: HjmCourses) {
  const res = await request.put<ApiResult<unknown>>(
    '/hjm/hjm-courses',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除课程
 */
export async function removeHjmCourses(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/hjm/hjm-courses/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除课程
 */
export async function removeBatchHjmCourses(data: (number | undefined)[]) {
  const res = await request.del<ApiResult<unknown>>(
    '/hjm/hjm-courses/batch',
    {
      data
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询课程
 */
export async function getHjmCourses(id: number) {
  const res = await request.get<ApiResult<HjmCourses>>(
    '/hjm/hjm-courses/' + id
  );
  if (res.code === 0 && res) {
    return res;
  }
  return Promise.reject(new Error(res.message));
}
