import request, { tenantRequest } from '@/utils/request'
import {
  addScope,
  addUser,
  submit,
  del,
  send
} from './base'

export function getMyWorkflows (query) {
  const { pageNum: pageIndex, pageSize, ...params } = query
  return tenantRequest({
    url: '/workflow/calendarRelease/page',
    method: 'GET',
    params: addUser({
      pageIndex,
      pageSize,
      ...params
    })
  })
}

export function getPublishWorkflows (query) {
  const { pageNum: pageIndex, pageSize, ...params } = query
  return tenantRequest({
    url: '/workflow/calendarRelease/page',
    method: 'GET',
    params: addScope({
      pageIndex,
      pageSize,
      ...params
    })
  })
}

export function deleteWorkflow (workflowId) {
  return del({
    url: `/workflow/${workflowId}/delete`,
    method: 'DELETE'
  })
}

// 审核详情资源
export function getPublishWorkflowDetail (workflowId) {
  return request({
    url: `/workflow/${workflowId}/list`,
    method: 'GET'
  })
}

// 审核
export function reviewWorkflow (workflowId, data) {
  return send({
    url: `/workflow/calendarRelease/${workflowId}/reject`,
    method: 'POST',
    data
  })
}

// 流程重提交
export function calendarPublishRestart (workflowId, name) {
  return submit({
    url: `/workflow/${workflowId}/restart `,
    method: 'POST'
  }, name)
}

// 流程终止
export function calendarPublishStop (workflowId) {
  return request({
    url: `/workflow/${workflowId}/stop`,
    method: 'POST'
  })
}

// 单个流程-审核记录
export function viewHistory ({ workflowId }) {
  return request({
    url: `/workflow/${workflowId}/history `,
    method: 'GET'
  }).then(({ success, data }) => {
    if (success) {
      data = data.map((i, index) => { return { ...i, index } })
    }
    return { data }
  })
}
