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

/**
 * 分页查询分销商提现明细表
 */
export async function pageDealerWithdraw(params: DealerWithdrawParam) {
  const res = await request.get<ApiResult<PageResult<DealerWithdraw>>>(
    MODULES_API_URL + '/shop/dealer-withdraw/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询分销商提现明细表列表
 */
export async function listDealerWithdraw(params?: DealerWithdrawParam) {
  const res = await request.get<ApiResult<DealerWithdraw[]>>(
    MODULES_API_URL + '/shop/dealer-withdraw',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加分销商提现明细表
 */
export async function addDealerWithdraw(data: DealerWithdraw) {
  const res = await request.post<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/dealer-withdraw',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改分销商提现明细表
 */
export async function updateDealerWithdraw(data: DealerWithdraw) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/dealer-withdraw',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除分销商提现明细表
 */
export async function removeDealerWithdraw(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/dealer-withdraw/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除分销商提现明细表
 */
export async function removeBatchDealerWithdraw(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/shop/dealer-withdraw/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询分销商提现明细表
 */
export async function getDealerWithdraw(id: number) {
  const res = await request.get<ApiResult<DealerWithdraw>>(
    MODULES_API_URL + '/shop/dealer-withdraw/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
