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

/**
 * 分页查询宿舍床铺
 */
export async function pageDormitoryBed(params: DormitoryBedParam) {
  const res = await request.get<ApiResult<PageResult<DormitoryBed>>>(
    MODULES_API_URL + '/dormitory/dormitory-bed/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询宿舍床铺列表
 */
export async function listDormitoryBed(params?: DormitoryBedParam) {
  const res = await request.get<ApiResult<DormitoryBed[]>>(
    MODULES_API_URL + '/dormitory/dormitory-bed',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加宿舍床铺
 */
export async function addDormitoryBed(data: DormitoryBed) {
  const res = await request.post<ApiResult<unknown>>(
    MODULES_API_URL + '/dormitory/dormitory-bed',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改宿舍床铺
 */
export async function updateDormitoryBed(data: DormitoryBed) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/dormitory/dormitory-bed',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除宿舍床铺
 */
export async function removeDormitoryBed(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/dormitory/dormitory-bed/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除宿舍床铺
 */
export async function removeBatchDormitoryBed(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/dormitory/dormitory-bed/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询宿舍床铺
 */
export async function getDormitoryBed(id: number) {
  const res = await request.get<ApiResult<DormitoryBed>>(
    MODULES_API_URL + '/dormitory/dormitory-bed/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
