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

import type { ApiResult, PageResult } from '@/api';
import type { CreditFinalVersion, CreditFinalVersionParam } from './model';

/**
 * 分页查询终本案件
 */
export async function pageCreditFinalVersion(params: CreditFinalVersionParam) {
  const res = await request.get<ApiResult<PageResult<CreditFinalVersion>>>(
    '/credit/credit-final-version/page', withCreditUserScope(params));
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询终本案件列表
 */
export async function listCreditFinalVersion(params?: CreditFinalVersionParam) {
  const res = await request.get<ApiResult<CreditFinalVersion[]>>(
    '/credit/credit-final-version', withCreditUserScope(params));
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加终本案件
 */
export async function addCreditFinalVersion(data: CreditFinalVersion) {
  const res = await request.post<ApiResult<unknown>>(
    '/credit/credit-final-version',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改终本案件
 */
export async function updateCreditFinalVersion(data: CreditFinalVersion) {
  const res = await request.put<ApiResult<unknown>>(
    '/credit/credit-final-version',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除终本案件
 */
export async function removeCreditFinalVersion(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/credit/credit-final-version/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除终本案件
 */
export async function removeBatchCreditFinalVersion(
  data: (number | undefined)[]
) {
  const res = await request.del<ApiResult<unknown>>(
    '/credit/credit-final-version/batch',
    data);
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询终本案件
 */
export async function getCreditFinalVersion(id: number) {
  const res = await request.get<ApiResult<CreditFinalVersion>>(
    '/credit/credit-final-version/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 导入终本案件
 */
export async function importCreditFinalVersion(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-final-version/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 importCreditFinalVersionHistory(
  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-final-version/import/history',
    formData,
    {
      header: {
        'Content-Type': 'multipart/form-data'
      }
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}
