import request from '@/utils/request';
import { withCreditUserScope } from '@/api/credit/utils/data-scope';

import type { ApiResult, PageResult } from '@/api';
import type { CreditNearbyCompany, CreditNearbyCompanyParam } from './model';

/**
 * 分页查询附近企业
 */
export async function pageCreditNearbyCompany(
  params: CreditNearbyCompanyParam
) {
  const res = await request.get<ApiResult<PageResult<CreditNearbyCompany>>>(
    '/credit/credit-nearby-company/page',
    { params: withCreditUserScope(params) }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询附近企业列表
 */
export async function listCreditNearbyCompany(
  params?: CreditNearbyCompanyParam
) {
  const res = await request.get<ApiResult<CreditNearbyCompany[]>>(
    '/credit/credit-nearby-company',
    { params: withCreditUserScope(params) }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加附近企业
 */
export async function addCreditNearbyCompany(data: CreditNearbyCompany) {
  const res = await request.post<ApiResult<unknown>>(
    '/credit/credit-nearby-company',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改附近企业
 */
export async function updateCreditNearbyCompany(data: CreditNearbyCompany) {
  const res = await request.put<ApiResult<unknown>>(
    '/credit/credit-nearby-company',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除附近企业
 */
export async function removeCreditNearbyCompany(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/credit/credit-nearby-company/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除附近企业
 */
export async function removeBatchCreditNearbyCompany(
  data: (number | undefined)[]
) {
  const res = await request.delete<ApiResult<unknown>>(
    '/credit/credit-nearby-company/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询附近企业
 */
export async function getCreditNearbyCompany(id: number) {
  const res = await request.get<ApiResult<CreditNearbyCompany>>(
    '/credit/credit-nearby-company/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 导入附近企业
 */
export async function importCreditNearbyCompany(
  file: File,
  companyId?: number
) {
  const formData = new FormData();
  formData.append('file', file);
  if (companyId != null) {
    formData.append('companyId', String(companyId));
  }
  const res = await request.post<ApiResult<unknown>>(
    '/credit/credit-nearby-company/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));
}
