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

/**
 * 分页查询可见性
 */
export async function pageUserVisibility(params: UserVisibilityParam) {
  const res = await request.get<ApiResult<PageResult<UserVisibility>>>(
    '/oa/user-visibility/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询可见性列表
 */
export async function listUserVisibility(params?: UserVisibilityParam) {
  const res = await request.get<ApiResult<UserVisibility[]>>(
    '/oa/user-visibility',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加可见性
 */
export async function addUserVisibility(data: UserVisibility) {
  const res = await request.post<ApiResult<unknown>>(
    '/oa/user-visibility',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改可见性
 */
export async function updateUserVisibility(data: UserVisibility) {
  const res = await request.put<ApiResult<unknown>>(
    '/oa/user-visibility',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除可见性
 */
export async function removeUserVisibility(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/oa/user-visibility/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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