
import axios from 'axios';

/**
 * 流程提交
 *
 * @param flowId  流程ID
 * @param title   流程标题：提交到下一节点审核人时他可看到的信息
 * @param comment 备注
 * @param variables 流程参数，用于做分支判断  JSON.stringify(row)
 * @param workId 工作流实例ID
 * @param trackId 工作流路径ID
 */
export async function workflowSubmit(flowId, title, comment, variables, workId, trackId){
    // 流程workId不为空，说明是驳回重新提交，不需要创建工作流
    let result = {}
    if (workId !== undefined && workId !== '') {
        // 先打开流程
        let url = 'workflow/task/open'
        let param = {
            'workId': workId,
            'trackId': trackId
        }
        await axios.post(url, param).then(async (response) => {
            if (response.data.code === 0) {
                // 执行提交
                let url = 'workflow/task/confirmSubmit'
                let param = {
                    'workId': workId,
                    'trackId': trackId
                }
                await axios.post(url, param).then(response => {
                    if (response.data.code === 0) {
                        if(response.data.data.status === 0){
                            result = {
                                status: 'success',
                                msg: response.data.data.operateMsg,
                                workId: response.data.data.workId,
                                trackId: response.data.data.trackId,
                            }
                        } else {
                            result = {
                                status: 'error',
                                msg: response.data.data.operateMsg,
                            }
                        }
                    } else {
                        result = {
                            status: 'error',
                            msg: response.data.msg,
                        }
                    }
                });
            } else {
                result = {
                    status: 'error',
                    msg: response.data.msg,
                }
            }
        });
    }
    else {
        let url = 'workflow/task/initSubmit'
        let param = {
            flowId: flowId,
            title: title,
            comment: comment,
            variables: variables
        }
        await axios.post(url, param).then(response => {
            if (response.data.code === 0) {
                if(response.data.data.status === 0){
                    result = {
                        status: 'success',
                        msg: response.data.data.operateMsg,
                        workId: response.data.data.workId,
                        trackId: response.data.data.trackId,
                    }
                } else {
                    result = {
                        status: 'error',
                        msg: response.data.data.operateMsg,
                    }
                }
            } else {
                result = {
                    status: 'error',
                    msg: response.data.msg,
                }
            }
        })
    }
    return result;
}

/**
 * 撤回流程
 *
 * @param workId 工作流实例ID
 * @param trackId 工作流路径ID
 */
export async function workflowCancel(workId, trackId) {
    let result = {}
    let url = 'workflow/task/getBack'
    let param = {
        'workId': workId,
        'trackId': trackId
    }
    await axios.post(url, param).then(response => {
        if (response.data.code === 0) {
            result =  {
                status: 'success',
                msg: response.data.msg,
            }
        } else {
            result = {
                status: 'error',
                msg: response.data.msg,
            }
        }
    });
    return result
}
