import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { Teacher, TeacherParam } from './model';
import { MODULES_API_URL } from '@/config/setting';

/**
 * 分页查询老师管理
 */
export async function pageTeacher(params: TeacherParam) {
  const res = await request.get<ApiResult<PageResult<Teacher>>>(
    MODULES_API_URL + '/booking/teacher/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询老师管理列表
 */
export async function listTeacher(params?: TeacherParam) {
  const res = await request.get<ApiResult<Teacher[]>>(
    MODULES_API_URL + '/booking/teacher',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加老师管理
 */
export async function addTeacher(data: Teacher) {
  const res = await request.post<ApiResult<unknown>>(
    MODULES_API_URL + '/booking/teacher',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改老师管理
 */
export async function updateTeacher(data: Teacher) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/booking/teacher',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除老师管理
 */
export async function removeTeacher(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/booking/teacher/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除老师管理
 */
export async function removeBatchTeacher(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/booking/teacher/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询老师管理
 */
export async function getTeacher(id: number) {
  const res = await request.get<ApiResult<Teacher>>(
    MODULES_API_URL + '/booking/teacher/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
