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

/**
 * 分页查询分销商用户记录表
 */
export async function pageShopDealerUser(params: ShopDealerUserParam) {
  const res = await request.get<ApiResult<PageResult<ShopDealerUser>>>(
    '/shop/shop-dealer-user/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询分销商用户记录表列表
 */
export async function listShopDealerUser(params?: ShopDealerUserParam) {
  const res = await request.get<ApiResult<ShopDealerUser[]>>(
    '/shop/shop-dealer-user',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加分销商用户记录表
 */
export async function addShopDealerUser(data: ShopDealerUser) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-dealer-user',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改分销商用户记录表
 */
export async function updateShopDealerUser(data: ShopDealerUser) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/shop-dealer-user',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除分销商用户记录表
 */
export async function removeShopDealerUser(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/shop/shop-dealer-user/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除分销商用户记录表
 */
export async function removeBatchShopDealerUser(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/shop/shop-dealer-user/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询分销商用户记录表
 */
export async function getShopDealerUser(id: number) {
  const res = await request.get<ApiResult<ShopDealerUser>>(
    '/shop/shop-dealer-user/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 导入分销商用户
 */
export async function importShopDealerUsers(file: File) {
  const formData = new FormData();
  formData.append('file', file);
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-dealer-user/import',
    formData,
    {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 导出分销商用户
 */
export async function exportShopDealerUsers(params?: ShopDealerUserParam) {
  const res = await request.get<Blob>('/shop/shop-dealer-user/export', {
    params,
    responseType: 'blob'
  });
  return res.data;
}
