import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api/index';
import { Cart, CartParam, CartVo } from './model';
import { MODULES_API_URL } from '@/config/setting';

/**
 * 分页查询购物车
 */
export async function pageCart(params: CartParam) {
  const res = await request.get<ApiResult<PageResult<Cart>>>(
    MODULES_API_URL + '/shop/cart/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询购物车列表
 */
export async function listCart(params?: CartParam) {
  const res = await request.get<ApiResult<CartVo>>(
    MODULES_API_URL + '/shop/cart',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加购物车
 */
export async function addCart(data: Cart) {
  const res = await request.post<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/cart',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改购物车
 */
export async function updateCart(data: Cart) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/cart',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除购物车
 */
export async function removeCart(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/cart/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除购物车
 */
export async function removeBatchCart(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/cart/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询购物车
 */
export async function getCart(id: number) {
  const res = await request.get<ApiResult<Cart>>(
    MODULES_API_URL + '/shop/cart/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 挂单
 * @param data
 */
export async function packCart(data: Cart) {
  const res = await request.post<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/cart/packCart',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 减少数量
 * @param data
 */
export async function subCartNum(data: Cart) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/cart/subCartNum',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 增加数量
 * @param data
 */
export async function addCartNum(data: Cart) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/cart/addCartNum',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
