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

/**
 * 分页查询分销商银行卡
 */
export async function pageShopDealerBank(params: ShopDealerBankParam) {
  const res = await request.get<ApiResult<PageResult<ShopDealerBank>>>(
    '/shop/shop-dealer-bank/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询分销商银行卡列表
 */
export async function listShopDealerBank(params?: ShopDealerBankParam) {
  const res = await request.get<ApiResult<ShopDealerBank[]>>(
    '/shop/shop-dealer-bank',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加分销商银行卡
 */
export async function addShopDealerBank(data: ShopDealerBank) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-dealer-bank',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改分销商银行卡
 */
export async function updateShopDealerBank(data: ShopDealerBank) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/shop-dealer-bank',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除分销商银行卡
 */
export async function removeShopDealerBank(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/shop/shop-dealer-bank/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除分销商银行卡
 */
export async function removeBatchShopDealerBank(data: (number | undefined)[]) {
  const res = await request.del<ApiResult<unknown>>(
    '/shop/shop-dealer-bank/batch',
    {
      data
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询分销商银行卡
 */
export async function getShopDealerBank(id: number) {
  const res = await request.get<ApiResult<ShopDealerBank>>(
    '/shop/shop-dealer-bank/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
