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

/**
 * 分页查询提现记录
 */
export async function pageMerchantWithdraws(params: MerchantWithdrawParam) {
  const res = await request.get<ApiResult<PageResult<MerchantWithdraw>>>(
    '/shop/merchant-withdraw/page',
    { params }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询提现记录列表
 */
export async function listMerchantWithdraws(params?: MerchantWithdrawParam) {
  const res = await request.get<ApiResult<MerchantWithdraw[]>>(
    '/shop/merchant-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 addMerchantWithdraw(data: MerchantWithdraw) {
  const merchantCode = localStorage.getItem('merchantCode');
  if (merchantCode !== 'null') {
    data.merchantCode = String(merchantCode);
  }
  const res = await request.post<ApiResult<unknown>>(
    '/shop/merchant-withdraw',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改提现记录
 */
export async function updateMerchantWithdraw(data: MerchantWithdraw) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/merchant-withdraw',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除提现记录
 */
export async function removeMerchantWithdraw(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/shop/merchant-withdraw/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除提现记录
 */
export async function removeMerchantWithdraws(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/shop/merchant-withdraw/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
