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

import type { ApiResult, PageResult } from '@/api';
import type { CreditXgxf, CreditXgxfParam } from './model';

/**
 * 分页查询限制高消费
 */
export async function pageCreditXgxf(params: CreditXgxfParam) {
  const res = await request.get<ApiResult<PageResult<CreditXgxf>>>(
    '/credit/credit-xgxf/page', withCreditUserScope(params));
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询限制高消费列表
 */
export async function listCreditXgxf(params?: CreditXgxfParam) {
  const res = await request.get<ApiResult<CreditXgxf[]>>(
    '/credit/credit-xgxf', withCreditUserScope(params));
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加限制高消费
 */
export async function addCreditXgxf(data: CreditXgxf) {
  const res = await request.post<ApiResult<unknown>>(
    '/credit/credit-xgxf',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改限制高消费
 */
export async function updateCreditXgxf(data: CreditXgxf) {
  const res = await request.put<ApiResult<unknown>>(
    '/credit/credit-xgxf',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除限制高消费
 */
export async function removeCreditXgxf(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/credit/credit-xgxf/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除限制高消费
 */
export async function removeBatchCreditXgxf(data: (number | undefined)[]) {
  const res = await request.del<ApiResult<unknown>>(
    '/credit/credit-xgxf/batch',
    data);
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询限制高消费
 */
export async function getCreditXgxf(id: number) {
  const res = await request.get<ApiResult<CreditXgxf>>(
    '/credit/credit-xgxf/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 导入限制高消费
 */
export async function importCreditXgxf(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-xgxf/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 importCreditXgxfHistory(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-xgxf/import/history',
    formData,
    {
      header: {
        'Content-Type': 'multipart/form-data'
      }
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}
