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

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

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

/**
 * 添加分销商申请记录表
 */
export async function addShopDealerApply(data: ShopDealerApply) {
  try {
    const res = await request.post<ApiResult<unknown>>(
      '/shop/shop-dealer-apply',
      data
    );
    if (res.code === 0) {
      return res.message || '提交成功';
    }
    // 直接抛出包含服务器错误信息的错误
    const error = new Error(res.message || '提交失败');
    (error as any).code = res.code;
    (error as any).data = res.data;
    throw error;
  } catch (error: any) {
    // 如果已经是我们处理过的错误，直接抛出
    if (error.message && error.code !== undefined) {
      throw error;
    }

    // 处理网络错误或其他异常
    console.error('添加分销商申请失败:', error);

    // 尝试从响应中提取错误信息
    if (error.response?.data) {
      const responseData = error.response.data;
      if (responseData.message) {
        const newError = new Error(responseData.message);
        (newError as any).code = responseData.code;
        throw newError;
      }
    }

    // 默认错误处理
    throw new Error(error.message || '网络错误，请重试');
  }
}

/**
 * 修改分销商申请记录表
 */
export async function updateShopDealerApply(data: ShopDealerApply) {
  try {
    const res = await request.put<ApiResult<unknown>>(
      '/shop/shop-dealer-apply',
      data
    );
    if (res.code === 0) {
      return res.message || '修改成功';
    }
    // 直接抛出包含服务器错误信息的错误
    const error = new Error(res.message || '修改失败');
    (error as any).code = res.code;
    (error as any).data = res.data;
    throw error;
  } catch (error: any) {
    // 如果已经是我们处理过的错误，直接抛出
    if (error.message && error.code !== undefined) {
      throw error;
    }

    // 处理网络错误或其他异常
    console.error('修改分销商申请失败:', error);

    // 尝试从响应中提取错误信息
    if (error.response?.data) {
      const responseData = error.response.data;
      if (responseData.message) {
        const newError = new Error(responseData.message);
        (newError as any).code = responseData.code;
        throw newError;
      }
    }

    // 默认错误处理
    throw new Error(error.message || '网络错误，请重试');
  }
}

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

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

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