import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type {
  UserPlanPrice,
  UserPlanPriceParam
} from '@/api/love/plan-price/model';
/**
 * 分页查询价格
 */
export async function pageUserPlanPrice(params: UserPlanPriceParam) {
  const res = await request.get<ApiResult<PageResult<UserPlanPrice>>>(
    '/love/user-plan-price/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询价格列表
 */
export async function listUserPlanPrice(params?: UserPlanPriceParam) {
  const res = await request.get<ApiResult<UserPlanPrice[]>>(
    '/love/user-plan-price',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加价格
 */
export async function addUserPlanPrice(data: UserPlanPrice) {
  const res = await request.post<ApiResult<unknown>>(
    '/love/user-plan-price',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改价格
 */
export async function updateUserPlanPrice(data: UserPlanPrice) {
  const res = await request.put<ApiResult<unknown>>(
    '/love/user-plan-price',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 绑定价格
 */
export async function bindUserPlanPrice(data: UserPlanPrice) {
  const res = await request.put<ApiResult<unknown>>(
    '/love/user-plan-price/bind',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

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