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

/**
 * 分页查询宿舍楼层
 */
export async function pageDormitoryFloor(params: DormitoryFloorParam) {
  const res = await request.get<ApiResult<PageResult<DormitoryFloor>>>(
    MODULES_API_URL + '/dormitory/dormitory-floor/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

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

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

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

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