import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type {
  UserPlanEquity,
  UserPlanEquityParam
} from '@/api/love/plan-equity/model';
/**
 * 分页查询会员权益
 */
export async function pageUserPlanEquity(params: UserPlanEquityParam) {
  const res = await request.get<ApiResult<PageResult<UserPlanEquity>>>(
    '/love/user-plan-equity/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询会员权益列表
 */
export async function listUserPlanEquity(params?: UserPlanEquityParam) {
  const res = await request.get<ApiResult<UserPlanEquity[]>>(
    '/love/user-plan-equity',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加会员权益
 */
export async function addUserPlanEquity(data: UserPlanEquity) {
  const res = await request.post<ApiResult<unknown>>(
    '/love/user-plan-equity',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改会员权益
 */
export async function updateUserPlanEquity(data: UserPlanEquity) {
  const res = await request.put<ApiResult<unknown>>(
    '/love/user-plan-equity',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 绑定会员权益
 */
export async function bindUserPlanEquity(data: UserPlanEquity) {
  const res = await request.put<ApiResult<unknown>>(
    '/love/user-plan-equity/bind',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

/**
 * 删除会员权益
 */
export async function removeUserPlanEquity(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/love/user-plan-equity/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除会员权益
 */
export async function removeBatchUserPlanEquity(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/love/user-plan-equity/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
