/**
 * 流程页面上下文
 */

import { $CONST } from "@birt/funclib/ConstUtil";
import { FlowUtil } from "@birt/funclib/FlowUtil";
import { StoreUtil } from "@birt/funclib/StoreUtil";
import { isTrueVal } from "@birt/funclib/ExprUtil.js";

export default {
    data() {
        return {
            orderInfo: null,
            taskInfo: null
        };
    },
    computed: {
        // 流程单号
        fnumber() {
            return this.param[$CONST.FLOWNUMBER];
        },
        // 流程任务guid
        ftaskguid() {
            return this.param[$CONST.FLOWTASKGUID];
        },
        // 流程实例GUID
        forderguid() {
            return this.param[$CONST.FLOWORDERGUID]
        },
        // 流程状态
        flowstate() {
            return this.param[$CONST.FLOWSTATE]
        },
        // 流程功能路径
        fmodelpath() {
            return this.param[$CONST.FLOWMODELPATH]
        },
        // 流程标题
        ftitle() {
            return this.param[$CONST.FLOWTITLE]
        },
        // 下推流程版本
        fversion() {
            return this.param[$CONST.FLOWVERSION] || this.orderinfo && this.orderinfo.fversion;
        },
        // 流程task单据是否只读
        freadonly() {
            return isTrueVal(this.param[$CONST.FLOWREADONLY] || (this.taskInfo && this.taskInfo.readonly));;
        },
        // 流程是否有回退环节
        fhasback() {
            return isTrueVal(this.param[$CONST.FLOWHASBACK] || (this.taskInfo && this.taskInfo.hasBack));
        }
    },
    methods: {
        // 初始化流程页面上下文
        _initFlowContext() {
            if (this.ftaskguid) {
                this._initFlowContextByTask();
            } else if (this.fnumber) {
                this._initFlowContextByNumber();
            }
        },
        // 根据单号初始化流程上下文
        _initFlowContextByNumber() {
            var func = FlowUtil.openByNumber;
            var param = {
                request: this.$HI[$CONST.HIREQUEST],
                url: this.$HI[$CONST.HIURLOPNUM],
                fordernumber: this.fnumber,
            };
            this._requestFlowContext(func, param);
        },
        // 根据任务信息初始化流程上下文
        _initFlowContextByTask() {
            var func = FlowUtil.getFlowInfo;
            var param = {
                request: this.$HI[$CONST.HIREQUEST],
                url: this.$HI[$CONST.HIURLFLOWINFO],
                data: {
                    fmodelpath: this.fmodelpath,
                    fversion: this.fversion,
                    ftaskguid: this.ftaskguid,
                    flowstate: this.flowstate,
                    fbzid: this.userinfo.bzid,
                },
            };
            this._requestFlowContext(func, param);
        },
        // 根据参数请求流程上下文信息
        async _requestFlowContext(requestFunc, param) {
            let me = this;
            await requestFunc(param)
                .then(response => {
                    let outParam = response[$CONST.OUTPARAMETER];
                    if (outParam) {
                        me._setOrderInfo(outParam.order);
                        me._setTaskInfo(outParam.task);
                    }
                })
                .catch(error => {
                    console.log(error);
                });
        },
        // 获取流程数据集策略参数
        _getFlowDatasetPolicyParam(mPath, mFilePaths) {
            return {
                request: this.$HI[$CONST.HIREQUEST],
                url: this.$HI[$CONST.HIURLPOLICY],
                param: {
                    modelPath: mPath,
                    taskguid: this.ftaskguid,
                    modelFilePaths: JSON.stringify(mFilePaths)
                }
            };
        },
        // 请求流程数据集策略
        async _requestFlowDatasetPolicy(mPath, mFilePaths) {
            
            // 暂时这样，当为空时后台是否取首环节
            if (this.ftaskguid && !this.freadonly) {
                
                let param = this._getFlowDatasetPolicyParam(mPath, mFilePaths);
                let me = this;
                await StoreUtil.policy(param)
                    .then(response => {
                        let policys = response[$CONST.DATAPACK];
                        if (policys) {
                            me.setPolicys(policys);
                        }
                    })
                    .catch(error => {
                        console.log(error);
                    });
            }
        },
        // 设置当前流程整单信息
        _setOrderInfo(orderInfo) {
            // this.orderInfo = orderInfo;
            this.$set(this, "orderInfo", { ...orderInfo });
        },
        // 设置当前任务信息
        _setTaskInfo(taskInfo) {
            // this.taskInfo = taskInfo;
            this.$set(this, "taskInfo", { ...taskInfo });
        },
        // 设置任务guid
        setTaskGuid(guid) {
            this.$set(this.param, $CONST.FLOWTASKGUID, guid);
        },
        // 设置流程实例guid
        setOrderGuid(guid) {
            this.$set(this.param, $CONST.FLOWORDERGUID, guid);
        },
        // 判断当前页面上下文是否流程
        isFlow() {
            // let p = this.$route.path;
            // return p && p.indexOf($CONST.PAGEFLOW)
        }
    }
}