import request from '@/utils/request';
import { JobInfoForm, JobInfoQuery, JobInfoVO, JobGroupVO, RouteStrategyOption, BlockStrategyOption, GlueTypeOption } from './types';
import { AxiosPromise } from 'axios';

// 查询任务列表
export function listJobInfo(query: JobInfoQuery): AxiosPromise<JobInfoVO[]> {
  return request({
    url: '/xxl-job/jobinfo/pageList',
    method: 'get',
    params: query
  });
}

// 查询任务详细
export function getJobInfo(id: string | number): AxiosPromise<JobInfoVO> {
  return request({
    url: '/xxl-job/jobinfo/' + id,
    method: 'get'
  });
}

// 新增任务
export function addJobInfo(data: JobInfoForm) {
  return request({
    url: '/xxl-job/jobinfo/add',
    method: 'post',
    data: data
  });
}

// 修改任务
export function updateJobInfo(data: JobInfoForm) {
  return request({
    url: '/xxl-job/jobinfo/update',
    method: 'post',
    data: data
  });
}

// 删除任务
export function delJobInfo(id: string | number | (string | number)[]) {
  return request({
    url: '/xxl-job/jobinfo/remove',
    method: 'post',
    data: { id }
  });
}

// 启动任务
export function startJob(id: string | number) {
  return request({
    url: '/xxl-job/jobinfo/start',
    method: 'post',
    data: { id }
  });
}

// 停止任务
export function stopJob(id: string | number) {
  return request({
    url: '/xxl-job/jobinfo/stop',
    method: 'post',
    data: { id }
  });
}

// 触发一次任务
export function triggerJob(id: string | number, executorParam?: string, addressList?: string) {
  return request({
    url: '/xxl-job/jobinfo/trigger',
    method: 'post',
    data: { id, executorParam: executorParam || '', addressList: addressList || '' }
  });
}

// 获取执行器组列表
export function listJobGroup(): AxiosPromise<JobGroupVO[]> {
  return request({
    url: '/xxl-job/jobgroup/list',
    method: 'get'
  });
}

// 获取路由策略选项
export function getRouteStrategyOptions(): AxiosPromise<RouteStrategyOption[]> {
  return request({
    url: '/xxl-job/jobinfo/routeStrategy',
    method: 'get'
  });
}

// 获取阻塞处理策略选项
export function getBlockStrategyOptions(): AxiosPromise<BlockStrategyOption[]> {
  return request({
    url: '/xxl-job/jobinfo/blockStrategy',
    method: 'get'
  });
}

// 获取Glue类型选项
export function getGlueTypeOptions(): AxiosPromise<GlueTypeOption[]> {
  return request({
    url: '/xxl-job/jobinfo/glueType',
    method: 'get'
  });
}

// 获取Cron表达式下次执行时间
export function nextTriggerTime(scheduleType: string, scheduleConf: string): AxiosPromise<string[]> {
  return request({
    url: '/xxl-job/jobinfo/nextTriggerTime',
    method: 'get',
    params: { scheduleType, scheduleConf }
  });
}
