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

export function newInstance(mainDataset, subDataset, conf) {
    const dlgId = StringUtil.randomString(6);
    // 设置数据集id (加上随机数，下推映射不到目标数据集)
    // mainDataset.controlId += "_" + dlgId;
    // subDataset.controlId += "_sub_" + dlgId

    // 表单操作工具条
    var hcFormToolbar = false;
    if (conf.hcFormToolbar) {
        hcFormToolbar = {
            controlName: "HcFormToolbar",
            controlId: "HcFormToolbar_" + dlgId,
            dataset: mainDataset.controlId,
            layout: ["cancel", "save"],
            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 = false;
    if (conf.hcForm) {
        hcForm = {
            controlName: "HcForm",
            controlId: "HcForm_" + dlgId,
            dataset: mainDataset.controlId,
            labelWidth: "80px",
            rules: {
            },
            children: [],
        };
        // 应用表单个性化配置
        Object.assign(hcForm, conf.hcForm || {});
    }

    // 表单网格明细
    if (!conf.hcTable) {
        conf.hcTable = {}
    }
    if (!(conf.hcTable.isEdit === false)) {
        conf.hcTable.isEdit = true;
    }
    var hcTable = editTable(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
                )],
        };
        // 应用表单个性化配置
        // Object.assign(hcTableDetailForm,  || {});
    }
    // 系统表单自动填充字段
    debugger
    var hcSysForm = false;
    if (conf.hcSysForm) {
        var hcSysForm = {
            controlName: "HcForm",
            controlId: "HcForm_sys_" + dlgId,
            dataset: mainDataset.controlId,
            labelWidth: "80px",
            rules: {
            },
            children: [],
        };
        // 应用系统字段表单个性化配置
        Object.assign(hcSysForm, conf.hcSysForm || {});
    }
    // 应用报表模型
    var birtWorkBook = Object.assign({
        controlName: "BirtWorkBook",
        controlId: "BirtWorkBook_" + dlgId,
        showToolBar: false,
        totalPage: 1,
        renderType: "pages",
        height: function (parentHeight) {
            return parentHeight - 150;
        },
    }, 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(hcTitle ? hcTitle : [])
                        .concat(hcForm ? hcForm : [])
                        .concat(hcTable ? hcTable : [])
                        .concat(hcTableDetailForm ? hcTableDetailForm : [])
                        .concat(hcSysForm ? hcSysForm : [])
                }
            ]
        }
    ];
    // 返回布局结构
    return birtWorkBook
}