import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type {
  ProjectOrder,
  ProjectOrderParam
} from '@/api/tower/project/order/model/index';

/**
 * 分页查询合同预定设备清单
 */
export async function pageProjectOrder(params: ProjectOrderParam) {
  const res = await request.get<ApiResult<PageResult<ProjectOrder>>>(
    '/tower/tower-project-order/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询合同预定设备清单列表
 */
export async function listProjectOrder(params?: ProjectOrderParam) {
  const res = await request.get<ApiResult<ProjectOrder[]>>(
    '/tower/tower-project-order',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询合同预定设备清单
 */
export async function getProjectOrder(id: number) {
  const res = await request.get<ApiResult<ProjectOrder>>(
    '/tower/tower-project-order/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加合同预定设备清单
 */
export async function addProjectOrder(data: ProjectOrder) {
  const res = await request.post<ApiResult<unknown>>(
    '/tower/tower-project-order',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改合同预定设备清单
 */
export async function updateProjectOrder(data: ProjectOrder) {
  const res = await request.put<ApiResult<unknown>>(
    '/tower/tower-project-order',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除合同预定设备清单
 */
export async function removeProjectOrder(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/tower/tower-project-order/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除合同预定设备清单
 */
export async function removeBatchProjectOrder(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/tower/tower-project-order/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 检查IP是否存在
 */
export async function checkExistence(
  field: string,
  value: string,
  id?: number
) {
  const res = await request.get<ApiResult<unknown>>(
    '/tower/tower-project-order/existence',
    {
      params: { field, value, id }
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
