import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { BookingCashier, BookingCashierParam } from './model';
import { MODULES_API_URL } from '@/config/setting';
import type { CashierParam, CashierVo } from "@/api/shop/cashier/model";

/**
 * 分页查询收银
 */
export async function pageBookingCashier(params: BookingCashierParam) {
  const res = await request.get<ApiResult<PageResult<BookingCashier>>>(
    MODULES_API_URL + '/booking/booking-cashier/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询收银列表
 */
export async function listBookingCashier(params?: BookingCashierParam) {
  const res = await request.get<ApiResult<BookingCashier[]>>(
    MODULES_API_URL + '/booking/booking-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 addBookingCashier(data: BookingCashier) {
  const res = await request.post<ApiResult<unknown>>(
    MODULES_API_URL + '/booking/booking-cashier',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改收银
 */
export async function updateBookingCashier(data: BookingCashier) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/booking/booking-cashier',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

/**
 * 批量删除收银
 */
export async function removeBatchBookingCashier(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/booking/booking-cashier/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询收银
 */
export async function getBookingCashier(id: number) {
  const res = await request.get<ApiResult<BookingCashier>>(
    MODULES_API_URL + '/booking/booking-cashier/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询收银列表
 */
export async function listByGroupId(params?: CashierParam) {
  const res = await request.get<ApiResult<CashierVo>>(
    MODULES_API_URL + '/booking/booking-cashier/listByGroupId',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
