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

import type { ApiResult, PageResult } from '@/api';
import type { CreditProject, CreditProjectParam } from './model';

/**
 * 分页查询招投标
 */
export async function pageCreditProject(params: CreditProjectParam) {
  const res = await request.get<ApiResult<PageResult<CreditProject>>>(
    '/credit/credit-project/page',
    { params: withCreditUserScope(params) }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询招投标列表
 */
export async function listCreditProject(params?: CreditProjectParam) {
  const res = await request.get<ApiResult<CreditProject[]>>(
    '/credit/credit-project',
    { 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 addCreditProject(data: CreditProject) {
  const res = await request.post<ApiResult<unknown>>(
    '/credit/credit-project',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改招投标
 */
export async function updateCreditProject(data: CreditProject) {
  const res = await request.put<ApiResult<unknown>>(
    '/credit/credit-project',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除招投标
 */
export async function removeCreditProject(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/credit/credit-project/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

/**
 * 根据id查询招投标
 */
export async function getCreditProject(id: number) {
  const res = await request.get<ApiResult<CreditProject>>(
    '/credit/credit-project/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 导入招投标
 */
export async function importCreditProject(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-project/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));
}
