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

import type { ApiResult, PageResult } from '@/api';
import type { CreditBreachOfTrust, CreditBreachOfTrustParam } from './model';

/**
 * 分页查询失信被执行人
 */
export async function pageCreditBreachOfTrust(
  params: CreditBreachOfTrustParam
) {
  const res = await request.get<ApiResult<PageResult<CreditBreachOfTrust>>>(
    '/credit/credit-breach-of-trust/page', withCreditUserScope(params));
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询失信被执行人列表
 */
export async function listCreditBreachOfTrust(
  params?: CreditBreachOfTrustParam
) {
  const res = await request.get<ApiResult<CreditBreachOfTrust[]>>(
    '/credit/credit-breach-of-trust', withCreditUserScope(params));
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加失信被执行人
 */
export async function addCreditBreachOfTrust(data: CreditBreachOfTrust) {
  const res = await request.post<ApiResult<unknown>>(
    '/credit/credit-breach-of-trust',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改失信被执行人
 */
export async function updateCreditBreachOfTrust(data: CreditBreachOfTrust) {
  const res = await request.put<ApiResult<unknown>>(
    '/credit/credit-breach-of-trust',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除失信被执行人
 */
export async function removeCreditBreachOfTrust(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/credit/credit-breach-of-trust/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除失信被执行人
 */
export async function removeBatchCreditBreachOfTrust(
  data: (number | undefined)[]
) {
  const res = await request.del<ApiResult<unknown>>(
    '/credit/credit-breach-of-trust/batch',
    data);
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询失信被执行人
 */
export async function getCreditBreachOfTrust(id: number) {
  const res = await request.get<ApiResult<CreditBreachOfTrust>>(
    '/credit/credit-breach-of-trust/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 导入失信被执行人
 */
export async function importCreditBreachOfTrust(
  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-breach-of-trust/import',
    formData,
    {
      header: {
        'Content-Type': 'multipart/form-data'
      }
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 导入历史失信被执行人
 */
export async function importCreditBreachOfTrustHistory(
  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-breach-of-trust/import/history',
    formData,
    {
      header: {
        'Content-Type': 'multipart/form-data'
      }
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}
