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

import type { ApiResult, PageResult } from '@/api';
import type { CreditDeliveryNotice, CreditDeliveryNoticeParam } from './model';

/**
 * 分页查询送达公告司法大数据
 */
export async function pageCreditDeliveryNotice(
  params: CreditDeliveryNoticeParam
) {
  const res = await request.get<ApiResult<PageResult<CreditDeliveryNotice>>>(
    '/credit/credit-delivery-notice/page', withCreditUserScope(params));
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询送达公告司法大数据列表
 */
export async function listCreditDeliveryNotice(
  params?: CreditDeliveryNoticeParam
) {
  const res = await request.get<ApiResult<CreditDeliveryNotice[]>>(
    '/credit/credit-delivery-notice', withCreditUserScope(params));
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加送达公告司法大数据
 */
export async function addCreditDeliveryNotice(data: CreditDeliveryNotice) {
  const res = await request.post<ApiResult<unknown>>(
    '/credit/credit-delivery-notice',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改送达公告司法大数据
 */
export async function updateCreditDeliveryNotice(data: CreditDeliveryNotice) {
  const res = await request.put<ApiResult<unknown>>(
    '/credit/credit-delivery-notice',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

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

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

/**
 * 根据id查询送达公告司法大数据
 */
export async function getCreditDeliveryNotice(id: number) {
  const res = await request.get<ApiResult<CreditDeliveryNotice>>(
    '/credit/credit-delivery-notice/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

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