import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type {
  BCTemporary,
  BCTemporaryParam
} from '@/api/apps/bc/temporary/model';
import { OPEN_API_URL } from '@/config/setting';
/**
 * 分页查询设备
 */
export async function pageBCTemporary(params: BCTemporaryParam) {
  const res = await request.get<ApiResult<PageResult<BCTemporary>>>(
    OPEN_API_URL + '/apps/bc-temporary/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询设备列表
 */
export async function listBCTemporary(params?: BCTemporaryParam) {
  const res = await request.get<ApiResult<BCTemporary[]>>(
    OPEN_API_URL + '/apps/bc-temporary',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加设备
 */
export async function addBCTemporary(data: BCTemporary) {
  const res = await request.post<ApiResult<unknown>>(
    OPEN_API_URL + '/apps/bc-temporary',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改设备
 */
export async function updateBCTemporary(data: BCTemporary) {
  const res = await request.put<ApiResult<unknown>>(
    OPEN_API_URL + '/apps/bc-temporary',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 绑定设备
 */
export async function bindBCTemporary(data: BCTemporary) {
  const res = await request.put<ApiResult<unknown>>(
    OPEN_API_URL + '/apps/bc-temporary/bind',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除设备
 */
export async function removeBCTemporary(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    OPEN_API_URL + '/apps/bc-temporary/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除设备
 */
export async function removeBatchBCTemporary(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    OPEN_API_URL + '/apps/bc-temporary/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
