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

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

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

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

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

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