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

import type { ApiResult, PageResult } from '@/api';
import type { CreditCaseFiling, CreditCaseFilingParam } from './model';

/**
 * 分页查询司法大数据
 */
export async function pageCreditCaseFiling(params: CreditCaseFilingParam) {
  const res = await request.get<ApiResult<PageResult<CreditCaseFiling>>>(
    '/credit/credit-case-filing/page', withCreditUserScope(params));
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询司法大数据列表
 */
export async function listCreditCaseFiling(params?: CreditCaseFilingParam) {
  const res = await request.get<ApiResult<CreditCaseFiling[]>>(
    '/credit/credit-case-filing', withCreditUserScope(params));
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加司法大数据
 */
export async function addCreditCaseFiling(data: CreditCaseFiling) {
  const res = await request.post<ApiResult<unknown>>(
    '/credit/credit-case-filing',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改司法大数据
 */
export async function updateCreditCaseFiling(data: CreditCaseFiling) {
  const res = await request.put<ApiResult<unknown>>(
    '/credit/credit-case-filing',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除司法大数据
 */
export async function removeCreditCaseFiling(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/credit/credit-case-filing/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除司法大数据
 */
export async function removeBatchCreditCaseFiling(
  data: (number | undefined)[]
) {
  const res = await request.del<ApiResult<unknown>>(
    '/credit/credit-case-filing/batch',
    data);
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询司法大数据
 */
export async function getCreditCaseFiling(id: number) {
  const res = await request.get<ApiResult<CreditCaseFiling>>(
    '/credit/credit-case-filing/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 导入司法大数据
 */
export async function importCreditCaseFiling(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-case-filing/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 importCreditCaseFilingHistory(
  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-case-filing/import/history',
    formData,
    {
      header: {
        'Content-Type': 'multipart/form-data'
      }
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}
