import request from "@/utils/request"

// 审批流详情
export default {
    detail: function (taskId, processInsId) {
        return new Promise((resolve, reject) => {
            request({
                url: "/qdport-workflow/process/detail",
                method: "get",
                params: {
                    taskId,
                    processInsId
                }
            }).then(res => {
                if (res.code == 200) {
                    const { flow, process } = res.data;
                    let flows = [];
                    flow.forEach(item => {
                        item.class = (!item.endTime && item.historyActivityType !== "candidate") ? "finish" : "";
                        if (item.historyActivityType === "sequenceFlow") item.class = "lineWarn";
                        else if (!item.class && item.historyActivityType !== "candidate") item.class = "success";

                        if (item.comments && item.comments.find(c => c.action == "AddComment")) {
                            const { type, fullMessage } = item.comments.find(c => c.action == "AddComment");
                            if (type == "rollbackComment") {
                                item.class = "error";
                                item.desc = "驳回：" + fullMessage;
                            }
                            if (type == "comment") {
                                item.class = "success";
                                item.desc = "审批：" + fullMessage;
                            }
                        }

                        const index = flows.findIndex(f => f.historyActivityId === item.historyActivityId)
                        if (index !== -1) flows.splice(index, 1, item);
                        else flows.push(item);
                    });
                    flows = flows.filter(f => f.class != "lineWarn");

                    if (flows[flows.length - 1].historyActivityType != "endEvent") {
                        flows.push({
                            class: "",
                            historyActivityId: "Event_04wbnmn",
                            historyActivityType: "endEvent"
                        })
                    }

                    resolve({ process, flows });
                } else ElMessage.error(res.msg);
            }).catch(() => reject())
        })
    }
}