import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { PwlProject, PwlProjectParam } from './model';
import { MODULES_API_URL } from '@/config/setting';

/**
 * 分页查询卫兰的项目项目系统
 */
export async function pagePwlProject(params: PwlProjectParam) {
  const res = await request.get<ApiResult<PageResult<PwlProject>>>(
    MODULES_API_URL + '/pwl/pwl-project/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询卫兰的项目项目系统列表
 */
export async function listPwlProject(params?: PwlProjectParam) {
  const res = await request.get<ApiResult<PwlProject[]>>(
    MODULES_API_URL + '/pwl/pwl-project',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加卫兰的项目项目系统
 */
export async function addPwlProject(data: PwlProject) {
  const res = await request.post<ApiResult<unknown>>(
    MODULES_API_URL + '/pwl/pwl-project',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改卫兰的项目项目系统
 */
export async function updatePwlProject(data: PwlProject) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/pwl/pwl-project',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除卫兰的项目项目系统
 */
export async function removePwlProject(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/pwl/pwl-project/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除卫兰的项目项目系统
 */
export async function removeBatchPwlProject(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/pwl/pwl-project/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询卫兰的项目项目系统
 */
export async function getPwlProject(id: number) {
  const res = await request.get<ApiResult<PwlProject>>(
    MODULES_API_URL + '/pwl/pwl-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 pwlProjectCount() {
  const res = await request.get('/pwl/pwl-project/');
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}


/**
 * 文章批量导入
 */
export async function importPwlProject(file: File) {
  const formData = new FormData();
  formData.append('file', file);
  const res = await request.post<ApiResult<unknown>>(
    MODULES_API_URL + '/pwl/pwl-project/import',
    formData
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
