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

import type { ApiResult, PageResult } from '@/api';
import type { CreditCompany, CreditCompanyParam } from './model';

/**
 * 分页查询企业
 */
export async function pageCreditCompany(params: CreditCompanyParam) {
  const res = await request.get<ApiResult<PageResult<CreditCompany>>>(
    '/credit/credit-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 listCreditCompany(params?: CreditCompanyParam) {
  const res = await request.get<ApiResult<CreditCompany[]>>(
    '/credit/credit-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 addCreditCompany(data: CreditCompany) {
  const res = await request.post<ApiResult<unknown>>(
    '/credit/credit-company',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

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

/**
 * 根据id查询企业
 */
export async function getCreditCompany(id: number) {
  const res = await request.get<ApiResult<CreditCompany>>(
    '/credit/credit-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 importCreditCompany(file: File) {
  const formData = new FormData();
  formData.append('file', file);
  const res = await request.post<ApiResult<unknown>>(
    '/credit/credit-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));
}

/**
 * 根据企业匹配名称查询关联信息
 */
export async function getCompanyRelatedInfo(params: CreditCompanyParam) {
  const res = await request.get<ApiResult<CreditCompany>>(
    '/credit/credit-company/related',
    { params: withCreditUserScope(params) }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
