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

import type { ApiResult, PageResult } from '@/api';
import type { CreditGqdj, CreditGqdjParam } from './model';

/**
 * 分页查询股权冻结
 */
export async function pageCreditGqdj(params: CreditGqdjParam) {
  const res = await request.get<ApiResult<PageResult<CreditGqdj>>>(
    '/credit/credit-gqdj/page', withCreditUserScope(params));
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询股权冻结列表
 */
export async function listCreditGqdj(params?: CreditGqdjParam) {
  const res = await request.get<ApiResult<CreditGqdj[]>>(
    '/credit/credit-gqdj', withCreditUserScope(params));
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加股权冻结
 */
export async function addCreditGqdj(data: CreditGqdj) {
  const res = await request.post<ApiResult<unknown>>(
    '/credit/credit-gqdj',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改股权冻结
 */
export async function updateCreditGqdj(data: CreditGqdj) {
  const res = await request.put<ApiResult<unknown>>(
    '/credit/credit-gqdj',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除股权冻结
 */
export async function removeCreditGqdj(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/credit/credit-gqdj/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除股权冻结
 */
export async function removeBatchCreditGqdj(data: (number | undefined)[]) {
  const res = await request.del<ApiResult<unknown>>(
    '/credit/credit-gqdj/batch',
    data);
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询股权冻结
 */
export async function getCreditGqdj(id: number) {
  const res = await request.get<ApiResult<CreditGqdj>>(
    '/credit/credit-gqdj/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 导入股权冻结
 */
export async function importCreditGqdj(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-gqdj/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 importCreditGqdjHistory(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-gqdj/import/history',
    formData,
    {
      header: {
        'Content-Type': 'multipart/form-data'
      }
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}
