import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { OaLink, OaLinkParam } from './model';

/**
 * 分页查询常用链接
 */
export async function pageOaLink(params: OaLinkParam) {
  const res = await request.get<ApiResult<PageResult<OaLink>>>(
    '/oa/oa-link/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询常用链接列表
 */
export async function listOaLink(params?: OaLinkParam) {
  const res = await request.get<ApiResult<OaLink[]>>(
    '/oa/oa-link',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加常用链接
 */
export async function addOaLink(data: OaLink) {
  const res = await request.post<ApiResult<unknown>>(
    '/oa/oa-link',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改常用链接
 */
export async function updateOaLink(data: OaLink) {
  const res = await request.put<ApiResult<unknown>>(
    '/oa/oa-link',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除常用链接
 */
export async function removeOaLink(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/oa/oa-link/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除常用链接
 */
export async function removeBatchOaLink(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/oa/oa-link/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询常用链接
 */
export async function getOaLink(id: number) {
  const res = await request.get<ApiResult<OaLink>>(
    '/oa/oa-link/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
