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

/**
 * 分页查询订单
 */
export async function pageOrder(params: OrderParam) {
  const res = await request.get<ApiResult<PageResult<Order>>>(
    '/shop/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[]>>('/shop/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 listOrderPay(params?: OrderParam) {
  const res = await request.get<ApiResult<Order[]>>('/shop/order-pay', {
    params
  });
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询交费记录
 */
export async function pageOrderPay(params?: OrderParam) {
  const res = await request.get<ApiResult<OrderPay[]>>('/shop/order-pay/page', {
    params
  });
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

//续租订单
export async function addOrderPay(data: OrderPay) {
  const res = await request.post<ApiResult<unknown>>('/shop/order-pay/renew', data);
  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<App>>('/shop/order/' + id);
  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>>('/shop/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>>('/shop/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>>('/shop/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>>('/shop/order/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/existence', {
    params: { field, value, id }
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 更改电池
 */
export async function batteryChange(data) {
  const res = await request.post<ApiResult<unknown>>(
    '/open/equipment/change',
    data
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 强制退租
 */
export async function rentingOut(data: Order) {
  const res = await request.post<ApiResult<unknown>>(
    '/open/equipment/rentingOut',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
