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

/**
 * 分页查询收银
 */
export async function pageCashier(params: CashierParam) {
  const res = await request.get<ApiResult<PageResult<Cashier>>>(
    MODULES_API_URL + '/shop/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<CashierVo>>(
    MODULES_API_URL + '/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 listByGroupId(params?: CashierParam) {
  const res = await request.get<ApiResult<CashierVo[]>>(
    MODULES_API_URL + '/shop/cashier/listByGroupId',
    {
      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>>(
    MODULES_API_URL + '/shop/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>>(
    MODULES_API_URL + '/shop/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>>(
    MODULES_API_URL + '/shop/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>>(
    MODULES_API_URL + '/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 getCashier(id: number) {
  const res = await request.get<ApiResult<Cashier>>(
    MODULES_API_URL + '/shop/cashier/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 挂单
 * @param data
 */
export async function packCashier(data: Cashier[]) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/cashier/packCashier',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 减少数量
 * @param data
 */
export async function subCartNum(data: Cashier) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/cashier/subCartNum',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 增加数量
 * @param data
 */
export async function addCartNum(data: Cashier) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/cashier/addCartNum',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 取单
 */
export async function getByGroup(groupId: number) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/cashier/getByGroup/' + groupId
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}


/**
 * 删除整单
 */
export async function removeByGroup(groupId: number) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/cashier/removeByGroup/' + groupId
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
