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

export function newInstance(saveDataset1, saveDataset2, conf) {
  const dlgId = StringUtil.randomString(6);
  // 设置数据集id
  saveDataset1.controlId += "_" + dlgId;
  saveDataset2.controlId += "_" + dlgId;
  // 表单操作工具条
  var hcFormToolbar = {
    controlName: "HcFormToolbar",
    controlId: "HcFormToolbar_" + dlgId,
    dataset: saveDataset2.controlId,
    layout: ["cancel", "save"],
    size: "small",
    style: "padding:15px;"
  }
  // 应用工具条个性化配置
  Object.assign(hcFormToolbar, conf.hcFormToolbar || {});
  // 表单内容
  var hcForm1 = {
    controlName: "HcForm",
    controlId: "HcForm1_" + dlgId,
    dataset: saveDataset1.controlId,
    labelWidth: "80px",
    rules: {},
    children: [],
  };
  var hcForm2 = {
    controlName: "HcForm",
    controlId: "HcForm2_" + dlgId,
    dataset: saveDataset2.controlId,
    labelWidth: "80px",
    rules: {},
    children: [],
  };
  // 应用表单个性化配置
  Object.assign(hcForm1, conf.hcForm[0] || {});
  Object.assign(hcForm2, conf.hcForm[1] || {});
  // 应用报表模型
  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: [saveDataset1,saveDataset2],
    children: [{
      controlName: "BirtFormSheet",
      controlId: "BirtFormSheet_" + dlgId,
      children: [
        hcFormToolbar,
        hcForm1,
        hcForm2
      ]
    }]
  }];
  // 返回布局结构
  return birtWorkBook
}
