import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { Cashier, CashierParam } from './model/index';

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

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

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

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

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

/**
 * 批量删除订单
 */
export async function removeBatchCashier(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>('/apps/cashier/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>>('/apps/cashier/existence', {
    params: { field, value, id }
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
