import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { Profit, ProfitParam } from '@/api/love/profit/model';
/**
 * 分页查询收益明细
 */
export async function pageProfit(params: ProfitParam) {
  const res = await request.get<ApiResult<PageResult<Profit>>>(
    '/love/profit/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询收益明细列表
 */
export async function listProfit(params?: ProfitParam) {
  const res = await request.get<ApiResult<Profit[]>>('/love/profit', {
    params
  });
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加收益明细
 */
export async function addProfit(data: Profit) {
  const res = await request.post<ApiResult<unknown>>('/love/profit', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改收益明细
 */
export async function updateProfit(data: Profit) {
  const res = await request.put<ApiResult<unknown>>('/love/profit', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 绑定收益明细
 */
export async function bindProfit(data: Profit) {
  const res = await request.put<ApiResult<unknown>>('/love/profit/bind', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量添加
 */
export async function addBatchProfit(data: Profit[]) {
  const res = await request.post<ApiResult<unknown>>(
    '/love/profit/batch',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除收益明细
 */
export async function removeProfit(id?: number) {
  const res = await request.delete<ApiResult<unknown>>('/love/profit/' + id);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除收益明细
 */
export async function removeBatchProfit(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>('/love/profit/batch', {
    data
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
