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

/**
 * 分页查询应用操作动态
 */
export async function pageAppEvent(params: AppEventParam) {
  const res = await request.get<ApiResult<PageResult<AppEvent>>>(
    '/app/app-event/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询应用操作动态列表
 */
export async function listAppEvent(params?: AppEventParam) {
  const res = await request.get<ApiResult<AppEvent[]>>(
    '/app/app-event',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加应用操作动态
 */
export async function addAppEvent(data: AppEvent) {
  const res = await request.post<ApiResult<unknown>>(
    '/app/app-event',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改应用操作动态
 */
export async function updateAppEvent(data: AppEvent) {
  const res = await request.put<ApiResult<unknown>>(
    '/app/app-event',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除应用操作动态
 */
export async function removeAppEvent(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/app/app-event/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除应用操作动态
 */
export async function removeBatchAppEvent(data: (number | undefined)[]) {
  const res = await request.del<ApiResult<unknown>>(
    '/app/app-event/batch',
    {
      data
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询应用操作动态
 */
export async function getAppEvent(id: number) {
  const res = await request.get<ApiResult<AppEvent>>(
    '/app/app-event/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
