/**
 * 左边树，右边网格明细查询
 * @param {*} saveDataset 保存数据集，命名规范 ds_[数组下标]_[对话框id]
 * @param {*} conf 组件定义配置项
 * @returns 
 */
 import { StringUtil } from "@birt/funclib/StringUtil.js";
 import { treeEditFilter } from "../compose/tree-edit-filter.jsx"
 import { FormStyleFactory } from "../compose/FormStyleFactory.js";
 import { BirtModelFactory } from "@birt/templates/BirtModelFactory.js";
 
 export function TreeTableQuery(conf) {
    debugger
     const dlgId = StringUtil.randomString(6);
     // 设置数据集id
     var saveDataset = conf.mainDataset
     saveDataset.controlId += "_" + dlgId;
     // 编辑过滤树
     let treeEdit = treeEditFilter(dlgId, saveDataset.controlId, conf.treeEditFilter);
    //  console.log('treeModel',treeEdit)
     let treeChildren = treeEdit.children[1].children
     treeChildren.map(item =>{
         debugger
         let birtModel = {
            birtModel: item.birtModel
         } 
         item.birtModel = BirtModelFactory.buildModel(birtModel)
        return item
     })
     let treeEditFilterInsc = treeEdit
    //  console.log('treeEditFilterInsc', treeEditFilterInsc)
     // 详情表单
     var hcForm = false;
     if (conf.hcForm) {
         hcForm = {
             controlName: "HcForm",
            //  controlId: "HcForm_" + dlgId,
             dataset: saveDataset.controlId,
             children: []
         }
         // 应用表单个性化配置
        let confForm = JSON.parse(JSON.stringify(conf.hcForm))
        Object.assign(hcForm, FormStyleFactory.buildForm(confForm) || {});
     }
     // 网格明细
     var hcTable = {
         controlName: "HcTable",
        //  controlId: "HcTable_" + dlgId,
         dataset: saveDataset.controlId,
         height: function (parentHeight) {
             return parentHeight - 170;
         },
         children: [],
     };
     
    // 应用网格个性化配置
    Object.assign(hcTable, conf.hcTable || {});
 
     // 应用报表模型
     var birtWorkBook = Object.assign({
         controlName: "BirtWorkBook",
         controlId: "BirtWorkBook_" + dlgId,
         showToolBar: false,
         totalPage: 1,
         renderType: "pages",
         height: function (parentHeight) {
             return parentHeight - 10;
         },
     }, conf.birtWorkBook || {})
     // 添加布局结构
     birtWorkBook.children = [
         {
             controlName: "BirtSheet",
             controlId: "BirtSheet_" + dlgId,
             name: "sheet_" + dlgId,
             pageIndex: 0,
             dataSets: [saveDataset],
             children: [
                 {
                     controlName: "BirtFormSheet",
                     controlId: "BirtFormSheet_" + dlgId,
                     children: [
                         {
                             controlName: "HcRow",
                             gutter: 10,
                             children: [
                                 treeEditFilterInsc,
                                 {
                                     controlName: "HcCol",
                                     controlId: "HcCol_01_right",
                                     span: 18,
                                     children: [].concat(hcForm ? hcForm : []).concat(hcTable)
                                 }
                             ]
                         }
                     ]
                 }
             ]
         }
     ]
     return birtWorkBook
 }