/**
 * 保存工具条、查询方案、查询面板、工具条、网格、分页工具条，标准网络批量编辑页面
 * @param {*} saveDataset 保存数据集，命名规范 ds_[数组下标]_[对话框id]
 * @param {*} conf 组件定义配置项
 * @returns 
 */
import { StringUtil } from "@birt/funclib/StringUtil.js";
import { QueryStyleFactory } from "../query-panel/QueryStyleFactory";

export function TablePaggingSave(conf) {
    const dlgId = StringUtil.randomString(6);
    // 设置数据集id，分页
    let saveDataset = conf.mainDataset;
    saveDataset.controlId += "_" + dlgId;
    saveDataset._infcPagging = {
        _isPagging: true,
        returnCount: true
    }

    // 保存工具条
    let hcFormToolbar = {
        controlName: "HcFormToolbar",
        controlId: "HcFormToolbar_" + dlgId,
        dataset: saveDataset.controlId,
        size: "small",
        style: "height:30px;",
        children: []
    }
    // 应用表单个性化配置
    Object.assign(hcFormToolbar, conf.hcFormToolbar || {});

    // 查询面板
    let hcQueryPanel = false;
    if (conf.hcQueryPanel) {
        hcQueryPanel = {
            controlName: "HcQueryPanel",
            controlId: "HcQueryPanel_" + dlgId,
            dataset: saveDataset.controlId,
            children: [],
            style: "height:50px;"
        }
        // 应用查询面板个性化配置
        Object.assign(hcQueryPanel, QueryStyleFactory.buildForm(conf.hcQueryPanel) || {});
    }

    // 网格明细
    var hcTable = {
        controlName: "HcTable",
        controlId: "HcTable_" + dlgId,
        dataset: saveDataset.controlId,
        showAddBtn: false,
        height: function (parentHeight) {
            return parentHeight - 170;
        },
        children: [],
    };
    // 应用网格个性化配置
    Object.assign(hcTable, conf.hcTable || {});

    // 分页工具条
    var hcPagination = {
        controlName: "HcPagination",
        controlId: "HcPagination_" + dlgId,
        pageSizes: [10, 20, 50, 100],
        layout: "total, prev, pager, next, sizes, jumper",
        dataset: saveDataset.controlId,
    };

    // 应用报表模型
    var birtWorkBook = Object.assign({
        controlName: "BirtWorkBook",
        controlId: "BirtWorkBook_" + dlgId,
        showToolBar: false,
        totalPage: 1,
        renderType: "pages",
        height: function (parentHeight) {
            return parentHeight;
        },
    }, conf.birtWorkBook || {})
    // 添加布局结构
    birtWorkBook.children = [
        {
            controlName: "BirtSheet",
            controlId: "BirtSheet_" + dlgId,
            name: "sheet_" + dlgId,
            pageIndex: 0,
            dataSets: [saveDataset],
            children: [
                {
                    controlName: "BirtFormSheet",
                    controlId: "BirtFormSheet_" + dlgId,
                    children: []
                        .concat(hcQueryPanel ? hcQueryPanel : [])
                        .concat(hcFormToolbar)
                        .concat(hcTable)
                        .concat(hcPagination)
                }
            ]
        }
    ]
    return birtWorkBook
}