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

/**
 * 分页查询自提订单联系方式
 */
export async function pageOrderExtract(params: OrderExtractParam) {
  const res = await request.get<ApiResult<PageResult<OrderExtract>>>(
    MODULES_API_URL + '/shop/order-extract/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询自提订单联系方式列表
 */
export async function listOrderExtract(params?: OrderExtractParam) {
  const res = await request.get<ApiResult<OrderExtract[]>>(
    MODULES_API_URL + '/shop/order-extract',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加自提订单联系方式
 */
export async function addOrderExtract(data: OrderExtract) {
  const res = await request.post<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/order-extract',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改自提订单联系方式
 */
export async function updateOrderExtract(data: OrderExtract) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/order-extract',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除自提订单联系方式
 */
export async function removeOrderExtract(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/order-extract/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除自提订单联系方式
 */
export async function removeBatchOrderExtract(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/order-extract/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询自提订单联系方式
 */
export async function getOrderExtract(id: number) {
  const res = await request.get<ApiResult<OrderExtract>>(
    MODULES_API_URL + '/shop/order-extract/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
