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>>>(
    '/oa/order/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

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

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

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

/**
 * 修改用户状态
 */
export async function updateOrderStatus(orderId?: number, status?: number) {
  const res = await request.put<ApiResult<unknown>>('/oa/order/status', {
    orderId,
    status
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询订单
 */
export async function getOrder(id: number) {
  const res = await request.get<ApiResult<Order>>('/oa/order/' + id);
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  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>>('/oa/order/existence', {
    params: { field, value, id }
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
