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

/**
 * 分页查询发票
 */
export async function pageUserInvoice(params: UserInvoiceParam) {
  const res = await request.get<ApiResult<PageResult<UserInvoice>>>(
    MODULES_API_URL + '/booking/user-invoice/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询发票列表
 */
export async function listUserInvoice(params?: UserInvoiceParam) {
  const res = await request.get<ApiResult<UserInvoice[]>>(
    MODULES_API_URL + '/booking/user-invoice',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加发票
 */
export async function addUserInvoice(data: UserInvoice) {
  const res = await request.post<ApiResult<unknown>>(
    MODULES_API_URL + '/booking/user-invoice',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改发票
 */
export async function updateUserInvoice(data: UserInvoice) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/booking/user-invoice',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除发票
 */
export async function removeUserInvoice(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/booking/user-invoice/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除发票
 */
export async function removeBatchUserInvoice(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/booking/user-invoice/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询发票
 */
export async function getUserInvoice(id: number) {
  const res = await request.get<ApiResult<UserInvoice>>(
    MODULES_API_URL + '/booking/user-invoice/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
