import axios from '../bc-libs/api.request'

const link_prefix = 'workflow/api/v1/base/'

/**
 * 发起流程方法
 * @param parmas  (processDefinitionKey|processDefinitionId  userId  userName 必须  其他都是流程变量)
 * @returns {*|void|ClientRequest|ClientHttp2Stream|never|AxiosPromise<any>}
 */
export const doApply = params => {
  return axios.request({
    url: 'workflow/processDefinition/start',
    data:params,
    method: 'post'
  })
}

/**
 * 分页查询方法
 * @param params
 * @returns {*|void|ClientRequest|ClientHttp2Stream|never|AxiosPromise<any>}
 */
export const getWorkflowUpcomingPage = params => {
  return axios.request({
    url: 'workflow/task/assignee/pageByLoginUser',
    params:params,
    method: 'get'
  })
}

/**
 * 分页查询流程实例
 * @param parmas
 * @returns {*|void|ClientRequest|ClientHttp2Stream|never|AxiosPromise<any>}
 */
export const getWorkflowPage = params => {
  return axios.request({
    url: 'workflow/processInstance/page',
    params:params,
    method: 'get'
  })
}

/**
 * 分页查询用户发起的流程
 * @param parmas
 * @returns {*|void|ClientRequest|ClientHttp2Stream|never|AxiosPromise<any>}
 */
export const getWorkflowApplyPage = params => {
  return axios.request({
    url: 'workflow/processInstance/pageByLoginUser',
    params:params,
    method: 'get'
  })
}

/**
 * 分页查询用户的操作历史任务
 * @param parmas
 * @returns {*|void|ClientRequest|ClientHttp2Stream|never|AxiosPromise<any>}
 */
export const getWorkflowDonePage = params => {
  return axios.request({
    url: 'workflow/task/his/pageByLoginUser',
    params:params,
    method: 'get'
  })
}

/**salesman
 * 分页查询用户发起的流程
 * @param parmas
 * @returns {*|void|ClientRequest|ClientHttp2Stream|never|AxiosPromise<any>}
 */
export const getWorkflowApplyList = params => {
  return axios.request({
    url: 'workflow/processInstance/listByLoginUser',
    params:params,
    method: 'get'
  })
}

/**
 * 查询用户的流程抄送
 * @param parmas
 * @returns {*|void|ClientRequest|ClientHttp2Stream|never|AxiosPromise<any>}
 */
export const getWorkflowCopyPage = (copyId, params) => {
  return axios.request({
    url: 'workflow/processInstance/copyPage/byLoginUser',
    params:params,
    method: 'get'
  })
}

/**
 * 根据参数查询列表
 * @param parmas
 * @returns {*|void|ClientRequest|ClientHttp2Stream|never|AxiosPromise<any>}
 */
export const getWorkflowUpcomingList = params => {
  return axios.request({
    url: 'api/v1/priceScheme/list',
    params:params,
    method: 'get'
  })
}

/**
 * 查询用户的操作历史任务
 * @param parmas
 * @returns {*|void|ClientRequest|ClientHttp2Stream|never|AxiosPromise<any>}
 */
export const getWorkflowDoneList = params => {
  return axios.request({
    url: 'workflow/his/'+params.userId,
    params:params,
    method: 'get'
  })
}

/**
 * 查询指定流程的历史任务--分页
 * @param parmas
 * @returns {*|void|ClientRequest|ClientHttp2Stream|never|AxiosPromise<any>}
 */
export const getWorkflowTaskPage = params => {
  return axios.request({
    url: 'workflow/task/hisPage',
    params:params,
    method: 'get'
  })
}

/**
 * 查询指定流程的历史任务
 * @param parmas
 * @returns {*|void|ClientRequest|ClientHttp2Stream|never|AxiosPromise<any>}
 */
export const getWorkflowTaskList = params => {
  return axios.request({
    url: 'workflow/task/hisList',
    params:params,
    method: 'get'
  })
}

/**
 * 查询指定任务的下一个用户任务的候选人
 * @param {processDefinitionKey: this.processDefinitionKey, taskId: this.taskId} 二者有其一即可
 * @returns {*|void|ClientRequest|ClientHttp2Stream|never|AxiosPromise<any>}
 */
export const getNextTaskAndCandidate = params => {
  return axios.request({
    url: 'workflow/processDefinition/nextTaskAndCandidate',
    params:params,
    method: 'get'
  })
}

/**
 * 校验判断指定的候选人规则组里面是否有符合条件的人
 * @param params 里面包含单位id,系统id,规则组id
 * @returns {AxiosPromise}
 */
export const checkHaveUserOfCandidate = params => {
  return axios.request({
    url: link_prefix+'workflowTaskConfigCandidate/checkHaveUserOfCandidate',
    params:params,
    method: 'get'
  })
}

/**
 * 查询指定任务(用户任务)的候选人
 * @param {processDefinitionKey: this.processDefinitionKey, taskId: this.taskId} 二者有其一即可
 * @returns {*|void|ClientRequest|ClientHttp2Stream|never|AxiosPromise<any>}
 */
export const getNowCandidate = taskId => {
  return axios.request({
    url: 'workflow/task/nowCandidate/'+taskId,
    method: 'get'
  })
}

/**
 * 查询指定任务可以退回的历史节点集合
 * @param {taskId: this.taskId} 二者有其一即可
 * @returns {*|void|ClientRequest|ClientHttp2Stream|never|AxiosPromise<any>}
 */
export const getBackSteps = taskId => {
  return axios.request({
    url: 'workflow/task/getCanBackStep/'+taskId,
    method: 'get'
  })
}

/**
 * 新增
 * @param params
 * @returns {Promise<any>}
 */
export const doApproval = (params) => {
  return new Promise((resolve, reject) => {
    axios.request({
      url: 'workflow/task/complete/'+params.taskId,
      method: 'POST',
      data: params,
    }).then(response => {
      resolve(response.data);
    }).catch(err => {
      reject(err)
    })
  })
}

/**
 * 新增
 * @param params
 * @returns {Promise<any>}
 */
export const doRevoke = (params) => {
  return new Promise((resolve, reject) => {
    axios.request({
      url: 'workflow/processInstance/revoke/'+params.id,
      method: 'POST',
      data: params,
    }).then(response => {
      resolve(response.data);
    }).catch(err => {
      reject(err)
    })
  })
}

/*/!**
 * 修改
 * @param params
 * @returns {Promise<any>}
 *!/
export const closeWorkflow = (params) => {
  return new Promise((resolve, reject) => {
    axios.request({
      url: 'api/v1/processInstance/merge',
      method: 'POST',
      data: params,
    }).then(response => {
      resolve(response.data);
    }).catch(err => {
      reject(err)
    })
  })
}*/

/**
 * 删除
 * @param id
 * @returns {Promise<any>}
 */
export const deleteWorkflow = (id) => {
  return new Promise((resolve, reject) => {
    axios.request({
      url: 'workflow/processInstance/delete/'+id,
      method: 'POST',
    }).then(response => {
      resolve(response.data);
    }).catch(err => {
      reject(err)
    })
  })
}

//------------------------------------------------------------------link相关 流程配置相关----------------------------------------------------------------------

/**
 * 根据任务的code查询绑定的流程
 * @param parmas
 * @returns {*|void|ClientRequest|ClientHttp2Stream|never|AxiosPromise<any>}
 */
export const getDefinitionByBusCode = (busCode) => {
  return axios.request({
    url: link_prefix+'workflowDefinitionBus/getDefinitionByBusCode/'+busCode,
    //params:{busCode: busCode},
    method: 'get'
  })
}

/**
 * 根据业务类型的code查询所有子节点与子任务 (工作台)
 * @param parmas
 * @returns {*|void|ClientRequest|ClientHttp2Stream|never|AxiosPromise<any>}
 */
export const getWorkbench = (groupCode) => {
  return axios.request({
    url: link_prefix+'workflowBus/getChildren/'+groupCode,
    method: 'get'
  })
}

/**
 * 根据参数查询流程的业务类型
 * @param parmas
 * @returns {*|void|ClientRequest|ClientHttp2Stream|never|AxiosPromise<any>}
 */
export const getWorkflowBusGroupList = (params) => {
  return axios.request({
    url: link_prefix+'workflowBusGroup/list/',
    params:params,
    method: 'get'
  })
}


//------------------------------------------------------草稿箱-------------------------------------------
/**
 * 分页查询方法
 * @param parmas
 * @returns {*|void|ClientRequest|ClientHttp2Stream|never|AxiosPromise<any>}
 */
export const getWorkflowDraftBoxPage = parmas => {
  return axios.request({
    url: link_prefix+'workflowDraftBox/page',
    params:parmas,
    method: 'get'
  })
}

/**
 * 根据参数查询列表
 * @param parmas
 * @returns {*|void|ClientRequest|ClientHttp2Stream|never|AxiosPromise<any>}
 */
export const getWorkflowDraftBoxList = parmas => {
  return axios.request({
    url: link_prefix+'workflowDraftBox/list',
    params:parmas,
    method: 'get'
  })
}

/**
 * 删除
 * @param id
 * @returns {Promise<any>}
 */
export const deleteWorkflowDraftBox = (id) => {
  return new Promise((resolve, reject) => {
    axios.request({
      url: link_prefix+'workflowDraftBox/delete/'+id,
      method: 'POST',
    }).then(response => {
      resolve(response.data);
    }).catch(err => {
      reject(err)
    })
  })
}



