import {BirtDesignRender} from "zhc-birt-core";
// import BirtDesignRender from "@birt/components/Birt/DesignRender/BirtDesignRender";

export default {
    data() {
        return {
            designRender: []
        };
    },
    computed: {
        renderType() {
            return (this.conf && this.conf.renderType) || "pages";
        },
        // sheets() {
        //   let sheets = [];
        //   this.conf.children.forEach((child) => {
        //     if (child.controlName.indexOf("Sheet") != -1) {
        //       sheets.push(child);
        //     }
        //   });
        //   return sheets;
        // },
        curtSheet() {
            let sheets = this.getSheets();
            if (sheets.length > 0) {
                let pageCount = 0;
                for (var i = 0, l = sheets.length; i < l; i++) {
                    let curtSht = sheets[i];
                    pageCount += this.getSheetPageCount(curtSht);
                    if (this.curtPageIndex < pageCount) {
                        this._initDataSets(curtSht.dataSets);
                        if (this.isRenderSheet(curtSht)) {
                            return curtSht;
                        } else {
                            return this.loadDesignSheet(curtSht, i, pageCount - 1);
                        }
                    }
                }
            }
            return {};
        },
    },
    methods: {
        // 批量加载所有sheet页
        loadAllSheet() {
            let sheets = this.getSheets();
            if (sheets.length > 0) {
                let pageCount = 0;
                for (var i = 0, l = sheets.length; i < l; i++) {
                    let curtSht = sheets[i];
                    pageCount += this.getSheetPageCount(curtSht);
                    this._initDataSets(curtSht.dataSets);
                    if (!this.isRenderSheet(curtSht)) {
                        this.loadDesignSheet(curtSht, i, pageCount - 1);
                    }
                }
            }
        },
        // 加载设计时sheet
        loadDesignSheet(designSheet, i, pageIndex) {
            let option = { axios: this.conf.axios, pageIndex: pageIndex };
            let designRender = new BirtDesignRender(
                designSheet,
                this.dataset,
                option
            );
            designRender.__index = i;
            this.addDesignRender(designRender);
            return this.reSetRenderSheet(designRender);
        },
        // 添加设计时渲染对象
        addDesignRender(renderSheet) {
            this.designRender.push(renderSheet);
        },
        // 下推参数变化，重新渲染设计时报表
        reRenderDesignSheet() {
            this.designRender.forEach(dRender => {
                this.reSetRenderSheet(dRender);
            })
        },
        // 替换原有的设计时sheet或过期renderSheet,插入最新应用参数条件的renderSheet
        reSetRenderSheet(designRender) {
            let i = designRender.__index;
            let renderSheet = designRender.getBirtSheet();
            let delteSheet = this.conf.children.splice(i, 1, renderSheet);
            let oldSheetCount = delteSheet[0].children && delteSheet[0].children.length || 1;
            this.updatePagging(i + 1, renderSheet.children.length, oldSheetCount);
            return renderSheet;
        },
        // 获取一个sheet的分页数
        getSheetPageCount(sheet) {
            return this.isRenderSheet(sheet) ? sheet.children.length : 1;
        },
        // 判断是否运行时sheet
        isRenderSheet(sheet) {
            return sheet.controlName == "BirtSheet";
        },
        // 获取所有Sheet页，替换计算字段this.sheets
        getSheets() {
            let sheets = [];
            this.conf.children.forEach((child) => {
                if (child.controlName.indexOf("Sheet") != -1) {
                    sheets.push(child);
                }
            });
            return sheets;
        },
        // 根据当前页，获取当前sheet所对应layout页
        getCurtSheetConf() {
            if (this.curtSheet.children) {
                if (this.curtSheet.pageIndex === undefined) {
                    console.error("报表模型数据格式错误,sheet.pageIndex起始页未定义");
                }
                let i = this.curtPageIndex - this.curtSheet.pageIndex;
                return this.curtSheet.children[i];
            }
        },
        // 获取当前报表样式拼装
        getCurtSheetStyle() {
            return (
                (this.curtSheet.style || "") +
                `height:${this.autoHeight()}px;overflow:auto;`
            );
        },
        // 获取报表孩子节点集合
        getBirtCellChildren() {
            let curtSheetConf = this.getCurtSheetConf();
            return curtSheetConf ? [curtSheetConf] : [];
        },
        // 根据下标获取当前sheet对象
        getSheetByIndex(i) {
            let sheets = this.getSheets();
            if (sheets) {
                return sheets[i];
            }
        },
    }
}