import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { Project, ProjectParam } from '@/api/tower/project/model';

/**
 * 项目数据
 */
export async function towerProjectData() {
  const res = await request.get('/tower/tower-project/data');
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
/**
 * 分页查询仓库
 */
export async function pageProject(params: ProjectParam) {
  const res = await request.get<ApiResult<PageResult<Project>>>(
    '/tower/tower-project/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询仓库列表
 */
export async function listProject(params?: ProjectParam) {
  const res = await request.get<ApiResult<Project[]>>('/tower/tower-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 addProject(data: Project) {
  const res = await request.post<ApiResult<unknown>>(
    '/tower/tower-project',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
export async function addBatchProject(data: Project[]) {
  const res = await request.post<ApiResult<unknown>>(
    '/tower/tower-project/batch',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改仓库
 */
export async function updateProject(data: Project) {
  const res = await request.put<ApiResult<unknown>>(
    '/tower/tower-project',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 绑定仓库
 */
export async function bindProject(data: Project) {
  const res = await request.put<ApiResult<unknown>>(
    '/tower/tower-project/bind',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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