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

/**
 * 分页查询材料库资料库表
 */
export async function pagePwlProjectLibrary(params: PwlProjectLibraryParam) {
  const res = await request.get<ApiResult<PageResult<PwlProjectLibrary>>>(
    MODULES_API_URL + '/pwl/pwl-project-library/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询材料库资料库表列表
 */
export async function listPwlProjectLibrary(params?: PwlProjectLibraryParam) {
  const res = await request.get<ApiResult<PwlProjectLibrary[]>>(
    MODULES_API_URL + '/pwl/pwl-project-library',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加材料库资料库表
 */
export async function addPwlProjectLibrary(data: PwlProjectLibrary) {
  const res = await request.post<ApiResult<unknown>>(
    MODULES_API_URL + '/pwl/pwl-project-library',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改材料库资料库表
 */
export async function updatePwlProjectLibrary(data: PwlProjectLibrary) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/pwl/pwl-project-library',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除材料库资料库表
 */
export async function removePwlProjectLibrary(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/pwl/pwl-project-library/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除材料库资料库表
 */
export async function removeBatchPwlProjectLibrary(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/pwl/pwl-project-library/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询材料库资料库表
 */
export async function getPwlProjectLibrary(id: number) {
  const res = await request.get<ApiResult<PwlProjectLibrary>>(
    MODULES_API_URL + '/pwl/pwl-project-library/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
