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

/**
 * 查询故障电池列表
 */
export async function listEquipmentFault(params?: EquipmentFaultParam) {
  const res = await request.get<ApiResult<EquipmentFault[]>>(
    '/apps/equipment-fault',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

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

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