import request from '@/utils/request';

export async function getTodoTaskPage(query) {
  const res = await request({
    url: '/bpm/task/todo-page',
    method: 'get',
    params: query
  });
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

export async function getDoneTaskPage(query) {
  const res = await request({
    url: '/bpm/task/done-page',
    method: 'get',
    params: query
  });

  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

export function completeTask(data) {
  return request({
    url: '/bpm/task/complete',
    method: 'PUT',
    data: data
  });
}

export async function approveTask(data) {
  const res = await request({
    url: '/bpm/task/approve',
    method: 'PUT',
    data: data
  });
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

export function rejectTask(data) {
  return request({
    url: '/bpm/task/reject',
    method: 'PUT',
    data: data
  });
}
export function backTask(data) {
  return request({
    url: '/bpm/task/back',
    method: 'PUT',
    data: data
  });
}

export function updateTaskAssignee(data) {
  return request({
    url: '/bpm/task/update-assignee',
    method: 'PUT',
    data: data
  });
}

export async function getTaskListByProcessInstanceId(processInstanceId) {
  const res = await request({
    url:
      '/bpm/task/list-by-process-instance-id?processInstanceId=' +
      processInstanceId,
    method: 'get'
  });

  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

export function getReturnList(taskId) {
  return request({
    url: '/bpm/task/return-list?taskId=' + taskId,
    method: 'get'
  });
}

export function returnTask(data) {
  return request({
    url: '/bpm/task/return',
    method: 'PUT',
    data: data
  });
}

export function delegateTask(data) {
  return request({
    url: '/bpm/task/delegate',
    method: 'PUT',
    data: data
  });
}
