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

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

/**
 * 添加设备
 */
export async function addBcEquipment(data: BcEquipment) {
  const merchantCode = localStorage.getItem('merchantCode');
  console.log(merchantCode);
  if (merchantCode !== null && merchantCode != '') {
    console.log(merchantCode);
    data.merchantCode = String(merchantCode);
  }
  const res = await request.post<ApiResult<unknown>>(
    '/apps/bc-equipment',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

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

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

/**
 * 发送企业微信消息推送
 */
export async function addSend(data: BcEquipment) {
  const res = await request.post<ApiResult<unknown>>(
    '/apps/bc-equipment/addSend',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
