/**
 * 单一表单保存，页面模板
 * @param {*} saveDataset 保存数据集，命名规范 ds_[数组下标]_[对话框id]
 * @param {*} conf 组件定义配置项
 * @returns 
 */
import { StringUtil } from "@birt/funclib/StringUtil.js";
import { FormStyleFactory } from "../compose/FormStyleFactory.js";
import { TableStyleFactory } from "../compose/TableStyleFactory.js";

export function FormSingleSave(conf) {
    debugger
    const dlgId = StringUtil.randomString(6);
    // 设置数据集id
    var saveDataset = conf.mainDataset;
    // saveDataset.controlId += "_" + dlgId;
    // 表单操作工具条
    var hcFormToolbar = {
        controlName: "HcFormToolbar",
        controlId: "HcFormToolbar_" + dlgId,
        dataset: saveDataset.controlId,
        layout: ["add", "remove", "cancel", "save"],
        // addOne: true,
        size: "small",
        style: "padding:15px;"
    }
    // 应用工具条个性化配置
    Object.assign(hcFormToolbar, conf.hcFormToolbar || {});
    // 表单内容
    var hcForm = {
        controlName: "HcForm",
        controlId: "HcForm_" + dlgId,
        dataset: saveDataset.controlId,
        labelWidth: "80px",
        rules: {
        },
        children: [],
    };
    // 应用表单个性化配置
    let confForm = JSON.parse(JSON.stringify(conf.hcForm))
    Object.assign(hcForm, FormStyleFactory.buildForm(confForm) || {});

    // 冗余表格
    var hcTable = false;
    if (conf.hcTable) {
        // 表单网格明细
        hcTable = TableStyleFactory.buildTable(dlgId, saveDataset.controlId, conf);
    }

    // 系统表单自动填充字段
    var hcSysForm = false;
    if (conf.hcSysForm) {
        var hcSysForm = {
            controlName: "HcForm",
            controlId: "HcForm_sys_" + dlgId,
            dataset: saveDataset.controlId,
            labelWidth: "80px",
            rules: {
            },
            children: [],
        };
        // 应用系统字段表单个性化配置
        Object.assign(hcSysForm, FormStyleFactory.buildForm(conf.hcSysForm) || {});
    }

    // 应用报表模型
    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: [
                        hcFormToolbar,
                        hcForm
                    ]
                        .concat(hcTable ? hcTable : [])
                        .concat(hcSysForm ? hcSysForm : [])
                }
            ]
        }
    ];
    // 返回布局结构
    return birtWorkBook
}