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

/**
 * 分页查询审批管理
 */
export async function pageDormitoryApply(params: DormitoryApplyParam) {
  const res = await request.get<ApiResult<PageResult<DormitoryApply>>>(
    MODULES_API_URL + '/dormitory/dormitory-apply/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询审批管理列表
 */
export async function listDormitoryApply(params?: DormitoryApplyParam) {
  const res = await request.get<ApiResult<DormitoryApply[]>>(
    MODULES_API_URL + '/dormitory/dormitory-apply',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加审批管理
 */
export async function addDormitoryApply(data: DormitoryApply) {
  const res = await request.post<ApiResult<unknown>>(
    MODULES_API_URL + '/dormitory/dormitory-apply',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改审批管理
 */
export async function updateDormitoryApply(data: DormitoryApply) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/dormitory/dormitory-apply',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除审批管理
 */
export async function removeDormitoryApply(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/dormitory/dormitory-apply/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

/**
 * 根据id查询审批管理
 */
export async function getDormitoryApply(id: number) {
  const res = await request.get<ApiResult<DormitoryApply>>(
    MODULES_API_URL + '/dormitory/dormitory-apply/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
