import * as doJob from "/@/api/jsonflow/do-job";
import {useMessageBox} from "/@/hooks/message";
import {DIC_PROP} from "/@/flow/support/dict-prop";
import * as runFlow from "/@/api/jsonflow/run-flow";
import {appointUser} from "/@/api/jsonflow/do-job";

// 签收反签收任务
export function handleSignForJob(methods, $message, getDataList, row, type) {
    let obj = {id: row.id, signForType: type};
    if (type === '0') {
        // 判断反签收
        methods.onSignForJob(obj)
        return
    }
    doJob.signForJob(obj)
        .then(() => {
            $message.success('操作成功');
            getDataList();
        })
}

// 反签收任务
export function onSignForJob($message, flowJob, getDataList, obj) {
    let msg = '是否确认反签收当前任务, 将放回待认领列表?'
    useMessageBox()
        .confirm(msg)
        .then(() => {
            return doJob.signForJob(obj)
        }).then(() => {
        $message.success('操作成功')
        flowJob.delJobLen();
        getDataList();
    })
}

// 挂起激活任务
export function handleSuspension($message, getDataList, row, suspension) {
    if (suspension === '0') {
        row.suspension = suspension
        doJob.suspension(row).then(() => {
            $message.success('操作成功');
            getDataList();
        })
        return
    }
    // 增加挂起原因
    useMessageBox()
        .prompt('请输入挂起原因').then(({value}) => {
        row.suspension = suspension
        row.suspensionReason = value
        return doJob.suspension(row)
    }).then(() => {
        $message.success('操作成功');
        getDataList();
    })
}

// 提前结束流程
export function handleEarlyComplete($message, flowJob, getDataList, row) {
    useMessageBox().prompt('请输入提前结束流程理由')
        .then(({ value }) => {
            row.jobBtn = DIC_PROP.JOB_BTNS[16].value
            row.invalidReason = value
            row.flowStatus = '0'
            return runFlow.earlyComplete(row)
        }).then(() => {
        $message.success('操作成功')
        flowJob.delJobLen();
        getDataList();
    })
}

// 终止流程
export function handleTerminateFlow($message, flowJob, getDataList, row) {
    useMessageBox().prompt('请输入终止理由')
        .then(({ value }) => {
            row.jobBtn = DIC_PROP.JOB_BTNS[15].value
            row.invalidReason = value
            row.flowStatus = '0'
            return runFlow.terminateFlow(row)
        }).then(() => {
        $message.success('操作成功')
        flowJob.delJobLen();
        getDataList();
    })
}

// 作废流程
export function handleInvalidFlow($message, flowJob, getDataList, row) {
    useMessageBox().prompt('请输入作废理由')
        .then(({ value }) => {
            row.jobBtn = DIC_PROP.JOB_BTNS[17].value
            row.invalidReason = value
            row.flowStatus = '0'
            return runFlow.invalidFlow(row)
        }).then(() => {
        $message.success('操作成功')
        flowJob.delJobLen();
        getDataList();
    })
}

// 转办任务
export function onTurnRunJob($message, flowJob, getDataList, data, role) {
    useMessageBox()
        .confirm('是否确认转办名称为"' + data.currJob.jobName + '"的任务?')
        .then(() => {
            data.currJob.jobType = role.jobType
            data.currJob.roleId = role.roleId
            data.currJob.jobBtn = DIC_PROP.JOB_BTNS[14].value
            return doJob.turnRunJob(data.currJob)
        }).then(() => {
        $message.success('操作成功')
        flowJob.delJobLen();
        getDataList();
    })
}

// 指定任务参与者
export function onJobNextUserRole($message, items, getDataList, data) {
    useMessageBox()
        .confirm('是否确认指派该参与者?')
        .then(() => {
            data.currJob.nextUserRole = items[0]
            appointUser(data.currJob).then(() => {
                $message.success('操作成功');
                getDataList();
            })
        })
}
