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

import type { ApiResult, PageResult } from '@/api';
import type {
  CreditAdministrativeLicense,
  CreditAdministrativeLicenseParam
} from './model';

/**
 * 分页查询行政许可
 */
export async function pageCreditAdministrativeLicense(
  params: CreditAdministrativeLicenseParam
) {
  const res = await request.get<
    ApiResult<PageResult<CreditAdministrativeLicense>>
  >('/credit/credit-administrative-license/page', { params: withCreditUserScope(params) });
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询行政许可列表
 */
export async function listCreditAdministrativeLicense(
  params?: CreditAdministrativeLicenseParam
) {
  const res = await request.get<ApiResult<CreditAdministrativeLicense[]>>(
    '/credit/credit-administrative-license',
    { 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 addCreditAdministrativeLicense(
  data: CreditAdministrativeLicense
) {
  const res = await request.post<ApiResult<unknown>>(
    '/credit/credit-administrative-license',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改行政许可
 */
export async function updateCreditAdministrativeLicense(
  data: CreditAdministrativeLicense
) {
  const res = await request.put<ApiResult<unknown>>(
    '/credit/credit-administrative-license',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除行政许可
 */
export async function removeCreditAdministrativeLicense(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/credit/credit-administrative-license/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除行政许可
 */
export async function removeBatchCreditAdministrativeLicense(
  data: (number | undefined)[]
) {
  const res = await request.delete<ApiResult<unknown>>(
    '/credit/credit-administrative-license/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询行政许可
 */
export async function getCreditAdministrativeLicense(id: number) {
  const res = await request.get<ApiResult<CreditAdministrativeLicense>>(
    '/credit/credit-administrative-license/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 导入行政许可
 */
export async function importCreditAdministrativeLicense(
  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-administrative-license/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 importCreditAdministrativeLicenseHistory(
  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-administrative-license/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));
}
