import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import { BatchLogoutApprovalForm, LogoutRequestForm, LogoutRequestQuery, LogoutRequestVO } from './types';

/**
 * 分页查询用户注销申请列表
 * @param query 查询参数
 */
export const listLogoutRequests = (query: LogoutRequestQuery): AxiosPromise<LogoutRequestVO[]> => {
  return request({
    url: '/system/user/approval/logout/list',
    method: 'get',
    params: query
  });
};

/**
 * 获取用户注销申请详细信息
 * @param id 申请ID
 */
export const getLogoutRequest = (id: number): AxiosPromise<LogoutRequestVO> => {
  return request({
    url: `/system/user/approval/logout/${id}`,
    method: 'get'
  });
};

/**
 * 申请用户注销
 * @param data 申请数据
 */
export const applyLogout = (data: LogoutRequestForm) => {
  return request({
    url: '/system/user/approval/logout/apply',
    method: 'post',
    data: data
  });
};

/**
 * 批量审批用户注销申请（支持单个或多个）
 * @param data 批量审批数据
 */
export const batchApproveLogoutRequest = (data: BatchLogoutApprovalForm) => {
  return request({
    url: '/system/user/approval/logout/batchApprove',
    method: 'post',
    data: data
  });
};

/**
 * 删除用户注销申请（仅限已处理的申请）
 * @param ids 申请ID数组
 */
export const deleteLogoutRequest = (ids: number | number[]) => {
  const idArray = Array.isArray(ids) ? ids : [ids];
  return request({
    url: `/system/user/approval/logout/${idArray.join(',')}`,
    method: 'delete'
  });
};

/**
 * 检查用户是否有待审核的注销申请
 * @param userId 用户ID
 */
export const checkPendingLogoutRequest = (userId: number): AxiosPromise<boolean> => {
  return request({
    url: `/system/user/approval/logout/check/${userId}`,
    method: 'get'
  });
};

export default {
  listLogoutRequests,
  getLogoutRequest,
  applyLogout,
  batchApproveLogoutRequest,
  deleteLogoutRequest,
  checkPendingLogoutRequest
};
