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

/**
 * 分页查询被执行人
 */
export async function pageCreditJudgmentDebtorHistory(
  params: CreditJudgmentDebtorHistoryParam
) {
  const res = await request.get<
    ApiResult<PageResult<CreditJudgmentDebtorHistory>>
  >('/credit/credit-judgment-debtor-history/page', {
    params
  });
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询被执行人列表
 */
export async function listCreditJudgmentDebtorHistory(
  params?: CreditJudgmentDebtorHistoryParam
) {
  const res = await request.get<ApiResult<CreditJudgmentDebtorHistory[]>>(
    '/credit/credit-judgment-debtor-history',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加被执行人
 */
export async function addCreditJudgmentDebtorHistory(
  data: CreditJudgmentDebtorHistory
) {
  const res = await request.post<ApiResult<unknown>>(
    '/credit/credit-judgment-debtor-history',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改被执行人
 */
export async function updateCreditJudgmentDebtorHistory(
  data: CreditJudgmentDebtorHistory
) {
  const res = await request.put<ApiResult<unknown>>(
    '/credit/credit-judgment-debtor-history',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除被执行人
 */
export async function removeCreditJudgmentDebtorHistory(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/credit/credit-judgment-debtor-history/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

/**
 * 根据id查询被执行人
 */
export async function getCreditJudgmentDebtorHistory(id: number) {
  const res = await request.get<ApiResult<CreditJudgmentDebtorHistory>>(
    '/credit/credit-judgment-debtor-history/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 导入历史被执行人
 */
export async function importCreditJudgmentDebtorHistory(
  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-judgment-debtor-history/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));
}
