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

/**
 * 分页查询订单
 */
export async function pageOrder(params: OrderParam) {
  const res = await request.get<ApiResult<PageResult<Order>>>(
    '/system/order/page',
    {
      params
    }
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询订单列表
 */
export async function listOrder(params?: OrderParam) {
  const res = await request.get<ApiResult<Order[]>>(
    '/system/order',
    {
      params
    }
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加订单
 */
export async function addOrder(data: Order) {
  const res = await request.post<ApiResult<unknown>>(
    '/system/order',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改订单
 */
export async function updateOrder(data: Order) {
  const res = await request.put<ApiResult<unknown>>(
    '/system/order',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除订单
 */
export async function removeOrder(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/system/order/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

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

/**
 * 根据id查询订单
 */
export async function getOrder(id: number) {
  const res = await request.get<ApiResult<Order>>(
    '/system/order/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
