import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { EquipmentAlarm, EquipmentAlarmParam } from './model';
/**
 * 分页查询电池报警
 */
export async function pageEquipmentAlarm(params: EquipmentAlarmParam) {
  const res = await request.get<ApiResult<PageResult<EquipmentAlarm>>>(
    '/apps/equipment-alarm/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询电池报警列表
 */
export async function listEquipmentAlarm(params?: EquipmentAlarmParam) {
  const res = await request.get<ApiResult<EquipmentAlarm[]>>(
    '/apps/equipment-alarm',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加电池报警
 */
export async function addEquipmentAlarm(data: EquipmentAlarm) {
  const merchantCode = localStorage.getItem('merchantCode');
  if (merchantCode !== 'null') {
    data.merchantCode = String(merchantCode);
  }
  const res = await request.post<ApiResult<unknown>>(
    '/apps/equipment-alarm',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改电池报警
 */
export async function updateEquipmentAlarm(data: EquipmentAlarm) {
  const res = await request.put<ApiResult<unknown>>(
    '/apps/equipment-alarm',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除电池报警
 */
export async function removeEquipmentAlarm(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/apps/equipment-alarm/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除电池报警
 */
export async function removeBatchEquipmentAlarm(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/apps/equipment-alarm/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>>(
    '/apps/equipment-alarm/existence',
    {
      params: { field, value, id }
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
