import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api/index';
import type { ShopCashier, ShopCashierParam } from './model';
import { MODULES_API_URL } from '@/config/setting';

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

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

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

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

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

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