import request from '@/utils/request'

// 任务列表
export async function getJobPage(data) {
  const res = await request.post('/eam/job/page', data)
  if (res.data.code == 0) {
    return res.data.data
  }
  return Promise.reject(new Error(res.data.message))
}

// 任务详情
export async function getJob(id) {
  const res = await request.get('/eam/job/getById/' + id)
  if (res.data.code == 0) {
    return res.data.data
  }
  return Promise.reject(new Error(res.data.message))
}

// 新增任务
export async function createJob(data) {
  const res = await request.post('/eam/job/add', data)
  if (res.data.code == 0) {
    return res.data.data
  }
  return Promise.reject(new Error(res.data.message))
}

// 修改任务
export async function updateJob(data) {
  const res = await request.post('/eam/job/update', data)
  if (res.data.code == 0) {
    return res.data.data
  }
  return Promise.reject(new Error(res.data.message))
}

// 删除任务
export async function deleteJob(id) {
  const res = await request.delete('/eam/job/delete?id=' + id)
  if (res.data.code == 0) {
    return res.data.data
  }
  return Promise.reject(new Error(res.data.message))
}

// 批量删除任务
export async function batchDeleteJob(ids) {
    console.log('批量删除任务', ids)
  const res = await request.delete('/eam/job/batchDelete', { data: ids })
  if (res.data.code == 0) {
    return res.data.data
  }
  return Promise.reject(new Error(res.data.message))
}

// 导出任务
export async function exportJob(params) {
  const res = await request.get('/eam/job/export-excel', { params })
  if (res) {
    return res
  }
  return Promise.reject(new Error(res.message))
}

// 修改任务状态
export async function updateJobStatus(id, status) {
  const res = await request.put(`/eam/job/updateStatus?id=${id}&status=${status}`)
  if (res.data.code == 0) {
    return res.data.data
  }
  return Promise.reject(new Error(res.data.message))
}

// 立即执行任务
export async function runJob(id) {
  const res = await request.put('/eam/job/trigger?id=' + id)
  if (res.data.code == 0) {
    return res.data.data
  }
  return Promise.reject(new Error(res.data.message))
}

// 获取任务后续执行时间
export async function getJobNextTimes(id) {
  const res = await request.get('/eam/job/get_next_times?id=' + id)
  if (res.data.code == 0) {
    return res.data.data
  }
  return Promise.reject(new Error(res.data.message))
}
