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

export function FormSingleDetail(conf) {
    const dlgId = StringUtil.randomString(6);
    // 设置数据集id
    var detailDataset = conf.mainDataset;
    detailDataset.controlId += "_" + dlgId;

    var hcFormToolbar = false;
    if (conf.hcFormToolbar) {
        // 表单操作工具条
        hcFormToolbar = {
            controlName: "HcFormToolbar",
            controlId: "HcFormToolbar_" + dlgId,
            dataset: detailDataset.controlId,
            size: "small",
            addOne: "true",
            style: "padding:15px;text-align:right;"
        }
        // 应用工具条个性化配置
        Object.assign(hcFormToolbar, conf.hcFormToolbar || {});
    }

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

    // 系统表单自动填充字段
    var hcSysForm = false;
    if (conf.hcSysForm) {
        var hcSysForm = {
            controlName: "HcForm",
            controlId: "HcForm_sys_" + dlgId,
            dataset: detailDataset.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: "detail",
        height: function (parentHeight) {
            return parentHeight;
        },
    }, conf.birtWorkBook || {})
    birtWorkBook.children = [
        {
            controlName: "BirtSheet",
            controlId: "BirtSheet_" + dlgId,
            name: "sheet_" + dlgId,
            pageIndex: 0,
            dataSets: [detailDataset],
            children: [
                {
                    controlName: "BirtFormSheet",
                    controlId: "BirtFormSheet_" + dlgId,
                    children: []
                        .concat(hcFormToolbar ? hcFormToolbar : [])
                        .concat(hcForm)
                        .concat(hcSysForm ? hcSysForm : [])
                }
            ]
        }
    ];
    // 返回布局结构
    return birtWorkBook
}