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

import type { ApiResult, PageResult } from '@/api';
import type { CreditBankruptcy, CreditBankruptcyParam } from './model';

/**
 * 分页查询破产重整
 */
export async function pageCreditBankruptcy(params: CreditBankruptcyParam) {
  const res = await request.get<ApiResult<PageResult<CreditBankruptcy>>>(
    '/credit/credit-bankruptcy/page',
    { params: withCreditUserScope(params) }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询破产重整列表
 */
export async function listCreditBankruptcy(params?: CreditBankruptcyParam) {
  const res = await request.get<ApiResult<CreditBankruptcy[]>>(
    '/credit/credit-bankruptcy',
    { 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 addCreditBankruptcy(data: CreditBankruptcy) {
  const res = await request.post<ApiResult<unknown>>(
    '/credit/credit-bankruptcy',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改破产重整
 */
export async function updateCreditBankruptcy(data: CreditBankruptcy) {
  const res = await request.put<ApiResult<unknown>>(
    '/credit/credit-bankruptcy',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

/**
 * 根据id查询破产重整
 */
export async function getCreditBankruptcy(id: number) {
  const res = await request.get<ApiResult<CreditBankruptcy>>(
    '/credit/credit-bankruptcy/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 导入破产重整
 */
export async function importCreditBankruptcy(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-bankruptcy/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 importCreditBankruptcyHistory(
  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-bankruptcy/import/history',
    formData,
    {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
