import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { ShopStoreRider, ShopStoreRiderParam } from './model';

/**
 * 分页查询配送员
 */
export async function pageShopStoreRider(params: ShopStoreRiderParam) {
  const res = await request.get<ApiResult<PageResult<ShopStoreRider>>>(
    '/shop/shop-store-rider/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询配送员列表
 */
export async function listShopStoreRider(params?: ShopStoreRiderParam) {
  const res = await request.get<ApiResult<ShopStoreRider[]>>(
    '/shop/shop-store-rider',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加配送员
 */
export async function addShopStoreRider(data: ShopStoreRider) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-store-rider',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改配送员
 */
export async function updateShopStoreRider(data: ShopStoreRider) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/shop-store-rider',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除配送员
 */
export async function removeShopStoreRider(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/shop/shop-store-rider/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除配送员
 */
export async function removeBatchShopStoreRider(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/shop/shop-store-rider/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询配送员
 */
export async function getShopStoreRider(id: number) {
  const res = await request.get<ApiResult<ShopStoreRider>>(
    '/shop/shop-store-rider/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
