import request from '@/utils/request';
import type {ApiResult, PageResult} from '@/api/index';
import type {HjmFence, HjmFenceParam} from './model';

/**
 * 分页查询电子围栏
 */
export async function pageHjmFence(params: HjmFenceParam) {
  const res = await request.get<ApiResult<PageResult<HjmFence>>>(
    '/hjm/hjm-fence/page',
    {
      params
    }
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询电子围栏列表
 */
export async function listHjmFence(params?: HjmFenceParam) {
  const res = await request.get<ApiResult<HjmFence[]>>(
    '/hjm/hjm-fence',
    {
      params
    }
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加电子围栏
 */
export async function addHjmFence(data: HjmFence) {
  const res = await request.post<ApiResult<unknown>>(
    '/hjm/hjm-fence',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改电子围栏
 */
export async function updateHjmFence(data: HjmFence) {
  const res = await request.put<ApiResult<unknown>>(
    '/hjm/hjm-fence',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除电子围栏
 */
export async function removeHjmFence(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/hjm/hjm-fence/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除电子围栏
 */
export async function removeBatchHjmFence(data: (number | undefined)[]) {
  const res = await request.del<ApiResult<unknown>>(
    '/hjm/hjm-fence/batch',
    {
      data
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询电子围栏
 */
export async function getHjmFence(id: number) {
  const res = await request.get<ApiResult<HjmFence>>(
    '/hjm/hjm-fence/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
