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

/**
 * 分页查询宿舍记录
 */
export async function pageDormitoryRecord(params: DormitoryRecordParam) {
  const res = await request.get<ApiResult<PageResult<DormitoryRecord>>>(
    MODULES_API_URL + '/dormitory/dormitory-record/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

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

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

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

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