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',
    { params: withCreditUserScope(params) }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

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

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

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