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

/**
 * 分页查询场地时段
 */
export async function pagePeriod(params: PeriodParam) {
  const res = await request.get<ApiResult<PageResult<Period>>>(
    MODULES_API_URL + '/booking/period/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询场地时段列表
 */
export async function listPeriod(params?: PeriodParam) {
  const res = await request.get<ApiResult<Period[]>>(
    MODULES_API_URL + '/booking/period',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加场地时段
 */
export async function addPeriod(data: Period) {
  const res = await request.post<ApiResult<unknown>>(
    MODULES_API_URL + '/booking/period',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改场地时段
 */
export async function updatePeriod(data: Period) {
  const res = await request.put<ApiResult<unknown>>(
    MODULES_API_URL + '/booking/period',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除场地时段
 */
export async function removePeriod(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/booking/period/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除场地时段
 */
export async function removeBatchPeriod(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    MODULES_API_URL + '/booking/period/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询场地时段
 */
export async function getPeriod(id: number) {
  const res = await request.get<ApiResult<Period>>(
    MODULES_API_URL + '/booking/period/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
