import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { EquipmentRecord, EquipmentRecordParam } from './model';
/**
 * 分页查询前世今生
 */
export async function pageEquipmentRecord(params: EquipmentRecordParam) {
  const res = await request.get<ApiResult<PageResult<EquipmentRecord>>>(
    '/apps/equipment-record/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询前世今生列表
 */
export async function listEquipmentRecord(params?: EquipmentRecordParam) {
  const res = await request.get<ApiResult<EquipmentRecord[]>>(
    '/apps/equipment-record',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加前世今生
 */
export async function addEquipmentRecord(data: EquipmentRecord) {
  const merchantCode = localStorage.getItem('merchantCode');
  if (merchantCode !== 'null') {
    data.merchantCode = String(merchantCode);
  }
  const res = await request.post<ApiResult<unknown>>(
    '/apps/equipment-record',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改前世今生
 */
export async function updateEquipmentRecord(data: EquipmentRecord) {
  const res = await request.put<ApiResult<unknown>>(
    '/apps/equipment-record',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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