import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { AppUrl, AppUrlParam } from '@/api/oa/app/url/model';
import { MODULES_API_URL } from '@/config/setting';

/**
 * 分页查询项目域名
 */
export async function pageAppUrl(params: AppUrlParam) {
  const res = await request.get<ApiResult<PageResult<AppUrl>>>(
    MODULES_API_URL + '/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 listAppUrl(params?: AppUrlParam) {
  const res = await request.get<ApiResult<AppUrl[]>>(
    MODULES_API_URL + '/oa/app-url',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

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

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

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