/**
 * 主从表单，网格详情，页面模板
 * @param {*} mainDataset 主数据集，命名规范 ds_[数组下标]_[对话框id]
 * @param {*} subDataset 从数据集，命名规范 ds_[数组下标]_[对话框id]
 * @param {*} conf 组件定义配置项
 * @returns 
 */
import { StringUtil } from "@birt/funclib/StringUtil.js";
import { formStyle01 } from "@birt/templates/compose/data-list/form-style-01";
import { FormStyleFactory } from "../compose/FormStyleFactory";
import { TableStyleFactory } from "../compose/TableStyleFactory";

export function FormMainSubSave(conf) {
    debugger
    const dlgId = StringUtil.randomString(6);
    // 设置数据集id
    var mainDataset = conf.mainDataset
    var subDataset = conf.subDataset
    // mainDataset.controlId += "_" + dlgId;
    //  subDataset.controlId += "_sub_" + dlgId
    var hcFormToolbar = false;
    if (conf.hcFormToolbar) {
        // 表单操作工具条
        hcFormToolbar = {
            controlName: "HcFormToolbar",
            controlId: "HcFormToolbar_" + dlgId,
            dataset: mainDataset.controlId,
            size: "small",
            addOne: "true",
            style: "padding:15px;text-align:right;"
        }
        // 应用工具条个性化配置
        Object.assign(hcFormToolbar, conf.hcFormToolbar || {});
    }

    // 标题
    var hcTitle = false;
    if (conf.hcTitle) {
        hcTitle = {
            controlName: "HcTextLabel",
            controlId: "HcTextLabel_" + dlgId
        }
        // 应用标题个性化配置
        Object.assign(hcTitle, conf.hcTitle || {});
    }

    // 表单内容
    var hcForm = {
        controlName: "HcForm",
        controlId: "HcForm_" + dlgId,
        dataset: mainDataset.controlId,
        labelWidth: "80px",
        rules: {
        },
        children: [],
    };
    // 应用表单个性化配置
    Object.assign(hcForm, FormStyleFactory.buildForm(conf.hcForm) || {});

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

    // 网格明细详情内容
    var hcTableDetailForm = false;
    if (hcTable.detailForm) {
        hcTableDetailForm = {
            controlName: "HcForm",
            controlId: "HcForm_sub_" + dlgId,
            dataset: subDataset.controlId,
            // labelWidth: "80px",
            children: [
                formStyle01(
                    hcTable.detailForm.form,
                    hcTable.detailForm.cols
                )],
        };
    }
    // 系统表单自动填充字段
    var hcSysForm = false;
    if (conf.hcSysForm) {
        var hcSysForm = {
            controlName: "HcForm",
            controlId: "HcForm_sys_" + dlgId,
            dataset: mainDataset.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: [mainDataset, subDataset],
            children: [
                {
                    controlName: "BirtFormSheet",
                    controlId: "BirtFormSheet_" + dlgId,
                    children: []
                        .concat(hcFormToolbar ? hcFormToolbar : [])
                        .concat(hcForm)
                        .concat(hcTable)
                        .concat(hcTableDetailForm ? hcTableDetailForm : [])
                        .concat(hcSysForm ? hcSysForm : [])
                }
            ]
        }
    ];
    // 返回布局结构
    return birtWorkBook
}