<template>
    <flow-resource-view :singleFlowObj="flowObj" :initMessageFunc="initMessageFunc" v-if="loadSuccess"
                        :flowObjByFlowId="flowObjByFlowId" :flowIds="flowIds">
    </flow-resource-view>
</template>

<script>
    export default {
        name: "flow-res-preview",
        components: {
            FlowResourceView: () => import("./flowResourceView")
        },
        props:{
            flowId: {type: String, required: true},
            defaultTab:{type: String, default: ""},
            operateRootArr:{type:Array, default:() => {return []}},
            dataPermis: {type: String, default: ""},
            initMessageFunc: {type: Function, default: null},
        },
        data(){
            return {
                flowObj: {},
                flowObjByFlowId: {},
                loadSuccess: false,
                flowIds: this.flowId,
            }
        },
        created() {
            this.init();
        },
        methods:{
            init() {
                if (!this.flowIds.includes(',')) {
                    this.flowObj["flowId"] = this.flowId;
                    this.FORM_GLOBAL.getPageInfoByFlowId(this.flowId).then(pageInfo => {
                        if (!pageInfo) {
                            this.$message.error("未查询到流程数据！");
                            return;
                        }
                        this.flowObj["formId"] = pageInfo.formId;
                        this.flowObj["flowId"] = pageInfo.flowId;
                        this.flowObj["tableViewId"] = pageInfo.tableViewId;
                        this.flowObj.operateArr = this.operateRootArr;
                        this.flowObj.dataPermis = this.dataPermis;
                        this.FORM_GLOBAL.getFormBase(pageInfo.formId).then(formBaseData => {
                            this.flowObj["sourceKey"] = formBaseData.sourceKey;
                            this.loadSuccess = true;
                        });
                    });
                }else {
                    // 初始化多种工作流信息
                    if (!this.flowIds) {
                        return this.$message.error("flowIds不能为空！");
                    }
                    let flowIdsArr = (this.flowIds || "").split(",").filter(item => item);

                    const asyncFunctions = flowIdsArr.map(flowId => (() => this.FORM_GLOBAL.getPageInfoByFlowId(flowId)));

                    Promise.all(asyncFunctions.map(func => func())).then(results => {
                        // 所有异步操作完成后的处理
                        if (results.length > 0) {
                            let pageInfo = results[0];
                            this.FORM_GLOBAL.getFormBase(pageInfo.formId).then(formBaseData => {
                                let sourceKey = formBaseData.sourceKey;
                                results.forEach(item => {
                                    this.flowObj = {};
                                    if (item) {
                                        this.flowObj["formId"] = item.formId;
                                        this.flowObj["flowId"] = item.flowId;
                                        this.flowObj["tableViewId"] = item.tableViewId;
                                        this.flowObj.operateArr = this.operateRootArr;
                                        this.flowObj.dataPermis = this.dataPermis;
                                        this.flowObj["sourceKey"] = sourceKey;
                                        this.flowObjByFlowId[item.flowId] = this.flowObj;
                                    }
                                });
                                this.loadSuccess = true;
                            });
                        }else {
                            this.loadSuccess = true;
                        }
                        // console.log(sourceKey, window.spcpDataObj, '111', results, this.flowObjByFlowId);
                    }).catch(error => {
                        // 任何一个Promise被reject都会进入这里
                        console.error(error);
                    });
                }
            },

            getCurrFormSourceKey() {
                // return new Promise(resolve => {
                //     this.axios.post(this.api.mixDataTool.getDataSoucreArr).then(res => {
                //         window.currentDataSourceKey = res.data[0].value;
                //         resolve();
                //     });
                // });
            },
        }
    }
</script>

<style scoped>

</style>