import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { OrderRefund, OrderRefundParam } from './model';

/**
 * 分页查询退货单
 */
export async function pageOrderRefund(params: OrderRefundParam) {
  const res = await request.get<ApiResult<PageResult<OrderRefund>>>(
    '/shop/order-refund/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询退货单列表
 */
export async function listOrderRefund(params?: OrderRefundParam) {
  const res = await request.get<ApiResult<OrderRefund[]>>(
    '/shop/order-refund',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询退货单
 */
export async function getOrderRefund(id: number) {
  const res = await request.get<ApiResult<OrderRefund>>(
    '/shop/order-refund/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加退货单
 */
export async function addOrderRefund(data: OrderRefund) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/order-refund',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改退货单
 */
export async function updateOrderRefund(data: OrderRefund) {
  const res = await request.put<ApiResult<unknown>>('/shop/order-refund', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除退货单
 */
export async function removeOrderRefund(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/shop/order-refund/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除退货单
 */
export async function removeBatchOrderRefund(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/shop/order-refund/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 检查IP是否存在
 */
export async function checkExistence(
  field: string,
  value: string,
  id?: number
) {
  const res = await request.get<ApiResult<unknown>>(
    '/shop/order-refund/existence',
    {
      params: { field, value, id }
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
