import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { OaAppUrl, OaAppUrlParam } from './model';
import { MODULES_API_URL } from '@/config/setting';

/**
 * 分页查询项目域名
 */
export async function pageOaAppUrl(params: OaAppUrlParam) {
  const res = await request.get<ApiResult<PageResult<OaAppUrl>>>(
    MODULES_API_URL + '/oa/oa-app-url/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询项目域名列表
 */
export async function listOaAppUrl(params?: OaAppUrlParam) {
  const res = await request.get<ApiResult<OaAppUrl[]>>(
    MODULES_API_URL + '/oa/oa-app-url',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加项目域名
 */
export async function addOaAppUrl(data: OaAppUrl) {
  const res = await request.post<ApiResult<unknown>>(
    MODULES_API_URL + '/oa/oa-app-url',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改项目域名
 */
export async function updateOaAppUrl(data: OaAppUrl) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/oa/oa-app-url',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除项目域名
 */
export async function removeOaAppUrl(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/oa/oa-app-url/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除项目域名
 */
export async function removeBatchOaAppUrl(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/oa/oa-app-url/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询项目域名
 */
export async function getOaAppUrl(id: number) {
  const res = await request.get<ApiResult<OaAppUrl>>(
    MODULES_API_URL + '/oa/oa-app-url/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
